##// END OF EJS Templates
Remove unneeded `skip_doctest` decorator usages
Nikita Kniazev -
Show More
@@ -1,1109 +1,1106 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 Pdb debugger class.
4 4
5 5
6 6 This is an extension to PDB which adds a number of new features.
7 7 Note that there is also the `IPython.terminal.debugger` class which provides UI
8 8 improvements.
9 9
10 10 We also strongly recommend to use this via the `ipdb` package, which provides
11 11 extra configuration options.
12 12
13 13 Among other things, this subclass of PDB:
14 14 - supports many IPython magics like pdef/psource
15 15 - hide frames in tracebacks based on `__tracebackhide__`
16 16 - allows to skip frames based on `__debuggerskip__`
17 17
18 18 The skipping and hiding frames are configurable via the `skip_predicates`
19 19 command.
20 20
21 21 By default, frames from readonly files will be hidden, frames containing
22 22 ``__tracebackhide__=True`` will be hidden.
23 23
24 24 Frames containing ``__debuggerskip__`` will be stepped over, frames who's parent
25 25 frames value of ``__debuggerskip__`` is ``True`` will be skipped.
26 26
27 27 >>> def helpers_helper():
28 28 ... pass
29 29 ...
30 30 ... def helper_1():
31 31 ... print("don't step in me")
32 32 ... helpers_helpers() # will be stepped over unless breakpoint set.
33 33 ...
34 34 ...
35 35 ... def helper_2():
36 36 ... print("in me neither")
37 37 ...
38 38
39 39 One can define a decorator that wraps a function between the two helpers:
40 40
41 41 >>> def pdb_skipped_decorator(function):
42 42 ...
43 43 ...
44 44 ... def wrapped_fn(*args, **kwargs):
45 45 ... __debuggerskip__ = True
46 46 ... helper_1()
47 47 ... __debuggerskip__ = False
48 48 ... result = function(*args, **kwargs)
49 49 ... __debuggerskip__ = True
50 50 ... helper_2()
51 51 ... # setting __debuggerskip__ to False again is not necessary
52 52 ... return result
53 53 ...
54 54 ... return wrapped_fn
55 55
56 56 When decorating a function, ipdb will directly step into ``bar()`` by
57 57 default:
58 58
59 59 >>> @foo_decorator
60 60 ... def bar(x, y):
61 61 ... return x * y
62 62
63 63
64 64 You can toggle the behavior with
65 65
66 66 ipdb> skip_predicates debuggerskip false
67 67
68 68 or configure it in your ``.pdbrc``
69 69
70 70
71 71
72 72 License
73 73 -------
74 74
75 75 Modified from the standard pdb.Pdb class to avoid including readline, so that
76 76 the command line completion of other programs which include this isn't
77 77 damaged.
78 78
79 79 In the future, this class will be expanded with improvements over the standard
80 80 pdb.
81 81
82 82 The original code in this file is mainly lifted out of cmd.py in Python 2.2,
83 83 with minor changes. Licensing should therefore be under the standard Python
84 84 terms. For details on the PSF (Python Software Foundation) standard license,
85 85 see:
86 86
87 87 https://docs.python.org/2/license.html
88 88
89 89
90 90 All the changes since then are under the same license as IPython.
91 91
92 92 """
93 93
94 94 #*****************************************************************************
95 95 #
96 96 # This file is licensed under the PSF license.
97 97 #
98 98 # Copyright (C) 2001 Python Software Foundation, www.python.org
99 99 # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu>
100 100 #
101 101 #
102 102 #*****************************************************************************
103 103
104 104 import bdb
105 105 import functools
106 106 import inspect
107 107 import linecache
108 108 import sys
109 109 import warnings
110 110 import re
111 111 import os
112 112
113 113 from IPython import get_ipython
114 114 from IPython.utils import PyColorize
115 115 from IPython.utils import coloransi, py3compat
116 116 from IPython.core.excolors import exception_colors
117 from IPython.testing.skipdoctest import skip_doctest
118 117
119 118 # skip module docstests
120 119 __skip_doctest__ = True
121 120
122 121 prompt = 'ipdb> '
123 122
124 123 # We have to check this directly from sys.argv, config struct not yet available
125 124 from pdb import Pdb as OldPdb
126 125
127 126 # Allow the set_trace code to operate outside of an ipython instance, even if
128 127 # it does so with some limitations. The rest of this support is implemented in
129 128 # the Tracer constructor.
130 129
131 130 DEBUGGERSKIP = "__debuggerskip__"
132 131
133 132
134 133 def make_arrow(pad):
135 134 """generate the leading arrow in front of traceback or debugger"""
136 135 if pad >= 2:
137 136 return '-'*(pad-2) + '> '
138 137 elif pad == 1:
139 138 return '>'
140 139 return ''
141 140
142 141
143 142 def BdbQuit_excepthook(et, ev, tb, excepthook=None):
144 143 """Exception hook which handles `BdbQuit` exceptions.
145 144
146 145 All other exceptions are processed using the `excepthook`
147 146 parameter.
148 147 """
149 148 warnings.warn("`BdbQuit_excepthook` is deprecated since version 5.1",
150 149 DeprecationWarning, stacklevel=2)
151 150 if et == bdb.BdbQuit:
152 151 print('Exiting Debugger.')
153 152 elif excepthook is not None:
154 153 excepthook(et, ev, tb)
155 154 else:
156 155 # Backwards compatibility. Raise deprecation warning?
157 156 BdbQuit_excepthook.excepthook_ori(et, ev, tb)
158 157
159 158
160 159 def BdbQuit_IPython_excepthook(self, et, ev, tb, tb_offset=None):
161 160 warnings.warn(
162 161 "`BdbQuit_IPython_excepthook` is deprecated since version 5.1",
163 162 DeprecationWarning, stacklevel=2)
164 163 print('Exiting Debugger.')
165 164
166 165
167 166 class Tracer(object):
168 167 """
169 168 DEPRECATED
170 169
171 170 Class for local debugging, similar to pdb.set_trace.
172 171
173 172 Instances of this class, when called, behave like pdb.set_trace, but
174 173 providing IPython's enhanced capabilities.
175 174
176 175 This is implemented as a class which must be initialized in your own code
177 176 and not as a standalone function because we need to detect at runtime
178 177 whether IPython is already active or not. That detection is done in the
179 178 constructor, ensuring that this code plays nicely with a running IPython,
180 179 while functioning acceptably (though with limitations) if outside of it.
181 180 """
182 181
183 @skip_doctest
184 182 def __init__(self, colors=None):
185 183 """
186 184 DEPRECATED
187 185
188 186 Create a local debugger instance.
189 187
190 188 Parameters
191 189 ----------
192 190 colors : str, optional
193 191 The name of the color scheme to use, it must be one of IPython's
194 192 valid color schemes. If not given, the function will default to
195 193 the current IPython scheme when running inside IPython, and to
196 194 'NoColor' otherwise.
197 195
198 196 Examples
199 197 --------
200 198 ::
201 199
202 200 from IPython.core.debugger import Tracer; debug_here = Tracer()
203 201
204 202 Later in your code::
205 203
206 204 debug_here() # -> will open up the debugger at that point.
207 205
208 206 Once the debugger activates, you can use all of its regular commands to
209 207 step through code, set breakpoints, etc. See the pdb documentation
210 208 from the Python standard library for usage details.
211 209 """
212 210 warnings.warn("`Tracer` is deprecated since version 5.1, directly use "
213 211 "`IPython.core.debugger.Pdb.set_trace()`",
214 212 DeprecationWarning, stacklevel=2)
215 213
216 214 ip = get_ipython()
217 215 if ip is None:
218 216 # Outside of ipython, we set our own exception hook manually
219 217 sys.excepthook = functools.partial(BdbQuit_excepthook,
220 218 excepthook=sys.excepthook)
221 219 def_colors = 'NoColor'
222 220 else:
223 221 # In ipython, we use its custom exception handler mechanism
224 222 def_colors = ip.colors
225 223 ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
226 224
227 225 if colors is None:
228 226 colors = def_colors
229 227
230 228 # The stdlib debugger internally uses a modified repr from the `repr`
231 229 # module, that limits the length of printed strings to a hardcoded
232 230 # limit of 30 characters. That much trimming is too aggressive, let's
233 231 # at least raise that limit to 80 chars, which should be enough for
234 232 # most interactive uses.
235 233 try:
236 234 from reprlib import aRepr
237 235 aRepr.maxstring = 80
238 236 except:
239 237 # This is only a user-facing convenience, so any error we encounter
240 238 # here can be warned about but can be otherwise ignored. These
241 239 # printouts will tell us about problems if this API changes
242 240 import traceback
243 241 traceback.print_exc()
244 242
245 243 self.debugger = Pdb(colors)
246 244
247 245 def __call__(self):
248 246 """Starts an interactive debugger at the point where called.
249 247
250 248 This is similar to the pdb.set_trace() function from the std lib, but
251 249 using IPython's enhanced debugger."""
252 250
253 251 self.debugger.set_trace(sys._getframe().f_back)
254 252
255 253
256 254 RGX_EXTRA_INDENT = re.compile(r'(?<=\n)\s+')
257 255
258 256
259 257 def strip_indentation(multiline_string):
260 258 return RGX_EXTRA_INDENT.sub('', multiline_string)
261 259
262 260
263 261 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
264 262 """Make new_fn have old_fn's doc string. This is particularly useful
265 263 for the ``do_...`` commands that hook into the help system.
266 264 Adapted from from a comp.lang.python posting
267 265 by Duncan Booth."""
268 266 def wrapper(*args, **kw):
269 267 return new_fn(*args, **kw)
270 268 if old_fn.__doc__:
271 269 wrapper.__doc__ = strip_indentation(old_fn.__doc__) + additional_text
272 270 return wrapper
273 271
274 272
275 273 class Pdb(OldPdb):
276 274 """Modified Pdb class, does not load readline.
277 275
278 276 for a standalone version that uses prompt_toolkit, see
279 277 `IPython.terminal.debugger.TerminalPdb` and
280 278 `IPython.terminal.debugger.set_trace()`
281 279
282 280
283 281 This debugger can hide and skip frames that are tagged according to some predicates.
284 282 See the `skip_predicates` commands.
285 283
286 284 """
287 285
288 286 default_predicates = {
289 287 "tbhide": True,
290 288 "readonly": False,
291 289 "ipython_internal": True,
292 290 "debuggerskip": True,
293 291 }
294 292
295 293 def __init__(self, color_scheme=None, completekey=None,
296 294 stdin=None, stdout=None, context=5, **kwargs):
297 295 """Create a new IPython debugger.
298 296
299 297 Parameters
300 298 ----------
301 299 color_scheme : default None
302 300 Deprecated, do not use.
303 301 completekey : default None
304 302 Passed to pdb.Pdb.
305 303 stdin : default None
306 304 Passed to pdb.Pdb.
307 305 stdout : default None
308 306 Passed to pdb.Pdb.
309 307 context : int
310 308 Number of lines of source code context to show when
311 309 displaying stacktrace information.
312 310 **kwargs
313 311 Passed to pdb.Pdb.
314 312
315 313 Notes
316 314 -----
317 315 The possibilities are python version dependent, see the python
318 316 docs for more info.
319 317 """
320 318
321 319 # Parent constructor:
322 320 try:
323 321 self.context = int(context)
324 322 if self.context <= 0:
325 323 raise ValueError("Context must be a positive integer")
326 324 except (TypeError, ValueError) as e:
327 325 raise ValueError("Context must be a positive integer") from e
328 326
329 327 # `kwargs` ensures full compatibility with stdlib's `pdb.Pdb`.
330 328 OldPdb.__init__(self, completekey, stdin, stdout, **kwargs)
331 329
332 330 # IPython changes...
333 331 self.shell = get_ipython()
334 332
335 333 if self.shell is None:
336 334 save_main = sys.modules['__main__']
337 335 # No IPython instance running, we must create one
338 336 from IPython.terminal.interactiveshell import \
339 337 TerminalInteractiveShell
340 338 self.shell = TerminalInteractiveShell.instance()
341 339 # needed by any code which calls __import__("__main__") after
342 340 # the debugger was entered. See also #9941.
343 341 sys.modules["__main__"] = save_main
344 342
345 343 if color_scheme is not None:
346 344 warnings.warn(
347 345 "The `color_scheme` argument is deprecated since version 5.1",
348 346 DeprecationWarning, stacklevel=2)
349 347 else:
350 348 color_scheme = self.shell.colors
351 349
352 350 self.aliases = {}
353 351
354 352 # Create color table: we copy the default one from the traceback
355 353 # module and add a few attributes needed for debugging
356 354 self.color_scheme_table = exception_colors()
357 355
358 356 # shorthands
359 357 C = coloransi.TermColors
360 358 cst = self.color_scheme_table
361 359
362 360 cst['NoColor'].colors.prompt = C.NoColor
363 361 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
364 362 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
365 363
366 364 cst['Linux'].colors.prompt = C.Green
367 365 cst['Linux'].colors.breakpoint_enabled = C.LightRed
368 366 cst['Linux'].colors.breakpoint_disabled = C.Red
369 367
370 368 cst['LightBG'].colors.prompt = C.Blue
371 369 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
372 370 cst['LightBG'].colors.breakpoint_disabled = C.Red
373 371
374 372 cst['Neutral'].colors.prompt = C.Blue
375 373 cst['Neutral'].colors.breakpoint_enabled = C.LightRed
376 374 cst['Neutral'].colors.breakpoint_disabled = C.Red
377 375
378 376 # Add a python parser so we can syntax highlight source while
379 377 # debugging.
380 378 self.parser = PyColorize.Parser(style=color_scheme)
381 379 self.set_colors(color_scheme)
382 380
383 381 # Set the prompt - the default prompt is '(Pdb)'
384 382 self.prompt = prompt
385 383 self.skip_hidden = True
386 384 self.report_skipped = True
387 385
388 386 # list of predicates we use to skip frames
389 387 self._predicates = self.default_predicates
390 388
391 389 #
392 390 def set_colors(self, scheme):
393 391 """Shorthand access to the color table scheme selector method."""
394 392 self.color_scheme_table.set_active_scheme(scheme)
395 393 self.parser.style = scheme
396 394
397 395 def set_trace(self, frame=None):
398 396 if frame is None:
399 397 frame = sys._getframe().f_back
400 398 self.initial_frame = frame
401 399 return super().set_trace(frame)
402 400
403 401 def _hidden_predicate(self, frame):
404 402 """
405 403 Given a frame return whether it it should be hidden or not by IPython.
406 404 """
407 405
408 406 if self._predicates["readonly"]:
409 407 fname = frame.f_code.co_filename
410 408 # we need to check for file existence and interactively define
411 409 # function would otherwise appear as RO.
412 410 if os.path.isfile(fname) and not os.access(fname, os.W_OK):
413 411 return True
414 412
415 413 if self._predicates["tbhide"]:
416 414 if frame in (self.curframe, getattr(self, "initial_frame", None)):
417 415 return False
418 416 frame_locals = self._get_frame_locals(frame)
419 417 if "__tracebackhide__" not in frame_locals:
420 418 return False
421 419 return frame_locals["__tracebackhide__"]
422 420 return False
423 421
424 422 def hidden_frames(self, stack):
425 423 """
426 424 Given an index in the stack return whether it should be skipped.
427 425
428 426 This is used in up/down and where to skip frames.
429 427 """
430 428 # The f_locals dictionary is updated from the actual frame
431 429 # locals whenever the .f_locals accessor is called, so we
432 430 # avoid calling it here to preserve self.curframe_locals.
433 431 # Furthermore, there is no good reason to hide the current frame.
434 432 ip_hide = [self._hidden_predicate(s[0]) for s in stack]
435 433 ip_start = [i for i, s in enumerate(ip_hide) if s == "__ipython_bottom__"]
436 434 if ip_start and self._predicates["ipython_internal"]:
437 435 ip_hide = [h if i > ip_start[0] else True for (i, h) in enumerate(ip_hide)]
438 436 return ip_hide
439 437
440 438 def interaction(self, frame, traceback):
441 439 try:
442 440 OldPdb.interaction(self, frame, traceback)
443 441 except KeyboardInterrupt:
444 442 self.stdout.write("\n" + self.shell.get_exception_only())
445 443
446 444 def precmd(self, line):
447 445 """Perform useful escapes on the command before it is executed."""
448 446
449 447 if line.endswith("??"):
450 448 line = "pinfo2 " + line[:-2]
451 449 elif line.endswith("?"):
452 450 line = "pinfo " + line[:-1]
453 451
454 452 line = super().precmd(line)
455 453
456 454 return line
457 455
458 456 def new_do_frame(self, arg):
459 457 OldPdb.do_frame(self, arg)
460 458
461 459 def new_do_quit(self, arg):
462 460
463 461 if hasattr(self, 'old_all_completions'):
464 462 self.shell.Completer.all_completions = self.old_all_completions
465 463
466 464 return OldPdb.do_quit(self, arg)
467 465
468 466 do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit)
469 467
470 468 def new_do_restart(self, arg):
471 469 """Restart command. In the context of ipython this is exactly the same
472 470 thing as 'quit'."""
473 471 self.msg("Restart doesn't make sense here. Using 'quit' instead.")
474 472 return self.do_quit(arg)
475 473
476 474 def print_stack_trace(self, context=None):
477 475 Colors = self.color_scheme_table.active_colors
478 476 ColorsNormal = Colors.Normal
479 477 if context is None:
480 478 context = self.context
481 479 try:
482 480 context = int(context)
483 481 if context <= 0:
484 482 raise ValueError("Context must be a positive integer")
485 483 except (TypeError, ValueError) as e:
486 484 raise ValueError("Context must be a positive integer") from e
487 485 try:
488 486 skipped = 0
489 487 for hidden, frame_lineno in zip(self.hidden_frames(self.stack), self.stack):
490 488 if hidden and self.skip_hidden:
491 489 skipped += 1
492 490 continue
493 491 if skipped:
494 492 print(
495 493 f"{Colors.excName} [... skipping {skipped} hidden frame(s)]{ColorsNormal}\n"
496 494 )
497 495 skipped = 0
498 496 self.print_stack_entry(frame_lineno, context=context)
499 497 if skipped:
500 498 print(
501 499 f"{Colors.excName} [... skipping {skipped} hidden frame(s)]{ColorsNormal}\n"
502 500 )
503 501 except KeyboardInterrupt:
504 502 pass
505 503
506 504 def print_stack_entry(self, frame_lineno, prompt_prefix='\n-> ',
507 505 context=None):
508 506 if context is None:
509 507 context = self.context
510 508 try:
511 509 context = int(context)
512 510 if context <= 0:
513 511 raise ValueError("Context must be a positive integer")
514 512 except (TypeError, ValueError) as e:
515 513 raise ValueError("Context must be a positive integer") from e
516 514 print(self.format_stack_entry(frame_lineno, '', context), file=self.stdout)
517 515
518 516 # vds: >>
519 517 frame, lineno = frame_lineno
520 518 filename = frame.f_code.co_filename
521 519 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
522 520 # vds: <<
523 521
524 522 def _get_frame_locals(self, frame):
525 523 """ "
526 524 Accessing f_local of current frame reset the namespace, so we want to avoid
527 525 that or the following can happen
528 526
529 527 ipdb> foo
530 528 "old"
531 529 ipdb> foo = "new"
532 530 ipdb> foo
533 531 "new"
534 532 ipdb> where
535 533 ipdb> foo
536 534 "old"
537 535
538 536 So if frame is self.current_frame we instead return self.curframe_locals
539 537
540 538 """
541 539 if frame is self.curframe:
542 540 return self.curframe_locals
543 541 else:
544 542 return frame.f_locals
545 543
546 544 def format_stack_entry(self, frame_lineno, lprefix=': ', context=None):
547 545 if context is None:
548 546 context = self.context
549 547 try:
550 548 context = int(context)
551 549 if context <= 0:
552 550 print("Context must be a positive integer", file=self.stdout)
553 551 except (TypeError, ValueError):
554 552 print("Context must be a positive integer", file=self.stdout)
555 553
556 554 import reprlib
557 555
558 556 ret = []
559 557
560 558 Colors = self.color_scheme_table.active_colors
561 559 ColorsNormal = Colors.Normal
562 560 tpl_link = "%s%%s%s" % (Colors.filenameEm, ColorsNormal)
563 561 tpl_call = "%s%%s%s%%s%s" % (Colors.vName, Colors.valEm, ColorsNormal)
564 562 tpl_line = "%%s%s%%s %s%%s" % (Colors.lineno, ColorsNormal)
565 563 tpl_line_em = "%%s%s%%s %s%%s%s" % (Colors.linenoEm, Colors.line, ColorsNormal)
566 564
567 565 frame, lineno = frame_lineno
568 566
569 567 return_value = ''
570 568 loc_frame = self._get_frame_locals(frame)
571 569 if "__return__" in loc_frame:
572 570 rv = loc_frame["__return__"]
573 571 # return_value += '->'
574 572 return_value += reprlib.repr(rv) + "\n"
575 573 ret.append(return_value)
576 574
577 575 #s = filename + '(' + `lineno` + ')'
578 576 filename = self.canonic(frame.f_code.co_filename)
579 577 link = tpl_link % py3compat.cast_unicode(filename)
580 578
581 579 if frame.f_code.co_name:
582 580 func = frame.f_code.co_name
583 581 else:
584 582 func = "<lambda>"
585 583
586 584 call = ""
587 585 if func != "?":
588 586 if "__args__" in loc_frame:
589 587 args = reprlib.repr(loc_frame["__args__"])
590 588 else:
591 589 args = '()'
592 590 call = tpl_call % (func, args)
593 591
594 592 # The level info should be generated in the same format pdb uses, to
595 593 # avoid breaking the pdbtrack functionality of python-mode in *emacs.
596 594 if frame is self.curframe:
597 595 ret.append('> ')
598 596 else:
599 597 ret.append(" ")
600 598 ret.append("%s(%s)%s\n" % (link, lineno, call))
601 599
602 600 start = lineno - 1 - context//2
603 601 lines = linecache.getlines(filename)
604 602 start = min(start, len(lines) - context)
605 603 start = max(start, 0)
606 604 lines = lines[start : start + context]
607 605
608 606 for i, line in enumerate(lines):
609 607 show_arrow = start + 1 + i == lineno
610 608 linetpl = (frame is self.curframe or show_arrow) and tpl_line_em or tpl_line
611 609 ret.append(
612 610 self.__format_line(
613 611 linetpl, filename, start + 1 + i, line, arrow=show_arrow
614 612 )
615 613 )
616 614 return "".join(ret)
617 615
618 616 def __format_line(self, tpl_line, filename, lineno, line, arrow=False):
619 617 bp_mark = ""
620 618 bp_mark_color = ""
621 619
622 620 new_line, err = self.parser.format2(line, 'str')
623 621 if not err:
624 622 line = new_line
625 623
626 624 bp = None
627 625 if lineno in self.get_file_breaks(filename):
628 626 bps = self.get_breaks(filename, lineno)
629 627 bp = bps[-1]
630 628
631 629 if bp:
632 630 Colors = self.color_scheme_table.active_colors
633 631 bp_mark = str(bp.number)
634 632 bp_mark_color = Colors.breakpoint_enabled
635 633 if not bp.enabled:
636 634 bp_mark_color = Colors.breakpoint_disabled
637 635
638 636 numbers_width = 7
639 637 if arrow:
640 638 # This is the line with the error
641 639 pad = numbers_width - len(str(lineno)) - len(bp_mark)
642 640 num = '%s%s' % (make_arrow(pad), str(lineno))
643 641 else:
644 642 num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
645 643
646 644 return tpl_line % (bp_mark_color + bp_mark, num, line)
647 645
648 646 def print_list_lines(self, filename, first, last):
649 647 """The printing (as opposed to the parsing part of a 'list'
650 648 command."""
651 649 try:
652 650 Colors = self.color_scheme_table.active_colors
653 651 ColorsNormal = Colors.Normal
654 652 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
655 653 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
656 654 src = []
657 655 if filename == "<string>" and hasattr(self, "_exec_filename"):
658 656 filename = self._exec_filename
659 657
660 658 for lineno in range(first, last+1):
661 659 line = linecache.getline(filename, lineno)
662 660 if not line:
663 661 break
664 662
665 663 if lineno == self.curframe.f_lineno:
666 664 line = self.__format_line(
667 665 tpl_line_em, filename, lineno, line, arrow=True
668 666 )
669 667 else:
670 668 line = self.__format_line(
671 669 tpl_line, filename, lineno, line, arrow=False
672 670 )
673 671
674 672 src.append(line)
675 673 self.lineno = lineno
676 674
677 675 print(''.join(src), file=self.stdout)
678 676
679 677 except KeyboardInterrupt:
680 678 pass
681 679
682 680 def do_skip_predicates(self, args):
683 681 """
684 682 Turn on/off individual predicates as to whether a frame should be hidden/skip.
685 683
686 684 The global option to skip (or not) hidden frames is set with skip_hidden
687 685
688 686 To change the value of a predicate
689 687
690 688 skip_predicates key [true|false]
691 689
692 690 Call without arguments to see the current values.
693 691
694 692 To permanently change the value of an option add the corresponding
695 693 command to your ``~/.pdbrc`` file. If you are programmatically using the
696 694 Pdb instance you can also change the ``default_predicates`` class
697 695 attribute.
698 696 """
699 697 if not args.strip():
700 698 print("current predicates:")
701 699 for (p, v) in self._predicates.items():
702 700 print(" ", p, ":", v)
703 701 return
704 702 type_value = args.strip().split(" ")
705 703 if len(type_value) != 2:
706 704 print(
707 705 f"Usage: skip_predicates <type> <value>, with <type> one of {set(self._predicates.keys())}"
708 706 )
709 707 return
710 708
711 709 type_, value = type_value
712 710 if type_ not in self._predicates:
713 711 print(f"{type_!r} not in {set(self._predicates.keys())}")
714 712 return
715 713 if value.lower() not in ("true", "yes", "1", "no", "false", "0"):
716 714 print(
717 715 f"{value!r} is invalid - use one of ('true', 'yes', '1', 'no', 'false', '0')"
718 716 )
719 717 return
720 718
721 719 self._predicates[type_] = value.lower() in ("true", "yes", "1")
722 720 if not any(self._predicates.values()):
723 721 print(
724 722 "Warning, all predicates set to False, skip_hidden may not have any effects."
725 723 )
726 724
727 725 def do_skip_hidden(self, arg):
728 726 """
729 727 Change whether or not we should skip frames with the
730 728 __tracebackhide__ attribute.
731 729 """
732 730 if not arg.strip():
733 731 print(
734 732 f"skip_hidden = {self.skip_hidden}, use 'yes','no', 'true', or 'false' to change."
735 733 )
736 734 elif arg.strip().lower() in ("true", "yes"):
737 735 self.skip_hidden = True
738 736 elif arg.strip().lower() in ("false", "no"):
739 737 self.skip_hidden = False
740 738 if not any(self._predicates.values()):
741 739 print(
742 740 "Warning, all predicates set to False, skip_hidden may not have any effects."
743 741 )
744 742
745 743 def do_list(self, arg):
746 744 """Print lines of code from the current stack frame
747 745 """
748 746 self.lastcmd = 'list'
749 747 last = None
750 748 if arg:
751 749 try:
752 750 x = eval(arg, {}, {})
753 751 if type(x) == type(()):
754 752 first, last = x
755 753 first = int(first)
756 754 last = int(last)
757 755 if last < first:
758 756 # Assume it's a count
759 757 last = first + last
760 758 else:
761 759 first = max(1, int(x) - 5)
762 760 except:
763 761 print('*** Error in argument:', repr(arg), file=self.stdout)
764 762 return
765 763 elif self.lineno is None:
766 764 first = max(1, self.curframe.f_lineno - 5)
767 765 else:
768 766 first = self.lineno + 1
769 767 if last is None:
770 768 last = first + 10
771 769 self.print_list_lines(self.curframe.f_code.co_filename, first, last)
772 770
773 771 # vds: >>
774 772 lineno = first
775 773 filename = self.curframe.f_code.co_filename
776 774 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
777 775 # vds: <<
778 776
779 777 do_l = do_list
780 778
781 779 def getsourcelines(self, obj):
782 780 lines, lineno = inspect.findsource(obj)
783 781 if inspect.isframe(obj) and obj.f_globals is self._get_frame_locals(obj):
784 782 # must be a module frame: do not try to cut a block out of it
785 783 return lines, 1
786 784 elif inspect.ismodule(obj):
787 785 return lines, 1
788 786 return inspect.getblock(lines[lineno:]), lineno+1
789 787
790 788 def do_longlist(self, arg):
791 789 """Print lines of code from the current stack frame.
792 790
793 791 Shows more lines than 'list' does.
794 792 """
795 793 self.lastcmd = 'longlist'
796 794 try:
797 795 lines, lineno = self.getsourcelines(self.curframe)
798 796 except OSError as err:
799 797 self.error(err)
800 798 return
801 799 last = lineno + len(lines)
802 800 self.print_list_lines(self.curframe.f_code.co_filename, lineno, last)
803 801 do_ll = do_longlist
804 802
805 803 def do_debug(self, arg):
806 804 """debug code
807 805 Enter a recursive debugger that steps through the code
808 806 argument (which is an arbitrary expression or statement to be
809 807 executed in the current environment).
810 808 """
811 809 trace_function = sys.gettrace()
812 810 sys.settrace(None)
813 811 globals = self.curframe.f_globals
814 812 locals = self.curframe_locals
815 813 p = self.__class__(completekey=self.completekey,
816 814 stdin=self.stdin, stdout=self.stdout)
817 815 p.use_rawinput = self.use_rawinput
818 816 p.prompt = "(%s) " % self.prompt.strip()
819 817 self.message("ENTERING RECURSIVE DEBUGGER")
820 818 sys.call_tracing(p.run, (arg, globals, locals))
821 819 self.message("LEAVING RECURSIVE DEBUGGER")
822 820 sys.settrace(trace_function)
823 821 self.lastcmd = p.lastcmd
824 822
825 823 def do_pdef(self, arg):
826 824 """Print the call signature for any callable object.
827 825
828 826 The debugger interface to %pdef"""
829 827 namespaces = [
830 828 ("Locals", self.curframe_locals),
831 829 ("Globals", self.curframe.f_globals),
832 830 ]
833 831 self.shell.find_line_magic("pdef")(arg, namespaces=namespaces)
834 832
835 833 def do_pdoc(self, arg):
836 834 """Print the docstring for an object.
837 835
838 836 The debugger interface to %pdoc."""
839 837 namespaces = [
840 838 ("Locals", self.curframe_locals),
841 839 ("Globals", self.curframe.f_globals),
842 840 ]
843 841 self.shell.find_line_magic("pdoc")(arg, namespaces=namespaces)
844 842
845 843 def do_pfile(self, arg):
846 844 """Print (or run through pager) the file where an object is defined.
847 845
848 846 The debugger interface to %pfile.
849 847 """
850 848 namespaces = [
851 849 ("Locals", self.curframe_locals),
852 850 ("Globals", self.curframe.f_globals),
853 851 ]
854 852 self.shell.find_line_magic("pfile")(arg, namespaces=namespaces)
855 853
856 854 def do_pinfo(self, arg):
857 855 """Provide detailed information about an object.
858 856
859 857 The debugger interface to %pinfo, i.e., obj?."""
860 858 namespaces = [
861 859 ("Locals", self.curframe_locals),
862 860 ("Globals", self.curframe.f_globals),
863 861 ]
864 862 self.shell.find_line_magic("pinfo")(arg, namespaces=namespaces)
865 863
866 864 def do_pinfo2(self, arg):
867 865 """Provide extra detailed information about an object.
868 866
869 867 The debugger interface to %pinfo2, i.e., obj??."""
870 868 namespaces = [
871 869 ("Locals", self.curframe_locals),
872 870 ("Globals", self.curframe.f_globals),
873 871 ]
874 872 self.shell.find_line_magic("pinfo2")(arg, namespaces=namespaces)
875 873
876 874 def do_psource(self, arg):
877 875 """Print (or run through pager) the source code for an object."""
878 876 namespaces = [
879 877 ("Locals", self.curframe_locals),
880 878 ("Globals", self.curframe.f_globals),
881 879 ]
882 880 self.shell.find_line_magic("psource")(arg, namespaces=namespaces)
883 881
884 882 def do_where(self, arg):
885 883 """w(here)
886 884 Print a stack trace, with the most recent frame at the bottom.
887 885 An arrow indicates the "current frame", which determines the
888 886 context of most commands. 'bt' is an alias for this command.
889 887
890 888 Take a number as argument as an (optional) number of context line to
891 889 print"""
892 890 if arg:
893 891 try:
894 892 context = int(arg)
895 893 except ValueError as err:
896 894 self.error(err)
897 895 return
898 896 self.print_stack_trace(context)
899 897 else:
900 898 self.print_stack_trace()
901 899
902 900 do_w = do_where
903 901
904 902 def break_anywhere(self, frame):
905 903 """
906 904
907 905 _stop_in_decorator_internals is overly restrictive, as we may still want
908 906 to trace function calls, so we need to also update break_anywhere so
909 907 that is we don't `stop_here`, because of debugger skip, we may still
910 908 stop at any point inside the function
911 909
912 910 """
913 911
914 912 sup = super().break_anywhere(frame)
915 913 if sup:
916 914 return sup
917 915 if self._predicates["debuggerskip"]:
918 916 if DEBUGGERSKIP in frame.f_code.co_varnames:
919 917 return True
920 918 if frame.f_back and self._get_frame_locals(frame.f_back).get(DEBUGGERSKIP):
921 919 return True
922 920 return False
923 921
924 @skip_doctest
925 922 def _is_in_decorator_internal_and_should_skip(self, frame):
926 923 """
927 924 Utility to tell us whether we are in a decorator internal and should stop.
928 925
929 926
930 927
931 928 """
932 929
933 930 # if we are disabled don't skip
934 931 if not self._predicates["debuggerskip"]:
935 932 return False
936 933
937 934 # if frame is tagged, skip by default.
938 935 if DEBUGGERSKIP in frame.f_code.co_varnames:
939 936 return True
940 937
941 938 # if one of the parent frame value set to True skip as well.
942 939
943 940 cframe = frame
944 941 while getattr(cframe, "f_back", None):
945 942 cframe = cframe.f_back
946 943 if self._get_frame_locals(cframe).get(DEBUGGERSKIP):
947 944 return True
948 945
949 946 return False
950 947
951 948 def stop_here(self, frame):
952 949
953 950 if self._is_in_decorator_internal_and_should_skip(frame) is True:
954 951 return False
955 952
956 953 hidden = False
957 954 if self.skip_hidden:
958 955 hidden = self._hidden_predicate(frame)
959 956 if hidden:
960 957 if self.report_skipped:
961 958 Colors = self.color_scheme_table.active_colors
962 959 ColorsNormal = Colors.Normal
963 960 print(
964 961 f"{Colors.excName} [... skipped 1 hidden frame]{ColorsNormal}\n"
965 962 )
966 963 return super().stop_here(frame)
967 964
968 965 def do_up(self, arg):
969 966 """u(p) [count]
970 967 Move the current frame count (default one) levels up in the
971 968 stack trace (to an older frame).
972 969
973 970 Will skip hidden frames.
974 971 """
975 972 # modified version of upstream that skips
976 973 # frames with __tracebackhide__
977 974 if self.curindex == 0:
978 975 self.error("Oldest frame")
979 976 return
980 977 try:
981 978 count = int(arg or 1)
982 979 except ValueError:
983 980 self.error("Invalid frame count (%s)" % arg)
984 981 return
985 982 skipped = 0
986 983 if count < 0:
987 984 _newframe = 0
988 985 else:
989 986 counter = 0
990 987 hidden_frames = self.hidden_frames(self.stack)
991 988 for i in range(self.curindex - 1, -1, -1):
992 989 if hidden_frames[i] and self.skip_hidden:
993 990 skipped += 1
994 991 continue
995 992 counter += 1
996 993 if counter >= count:
997 994 break
998 995 else:
999 996 # if no break occurred.
1000 997 self.error(
1001 998 "all frames above hidden, use `skip_hidden False` to get get into those."
1002 999 )
1003 1000 return
1004 1001
1005 1002 Colors = self.color_scheme_table.active_colors
1006 1003 ColorsNormal = Colors.Normal
1007 1004 _newframe = i
1008 1005 self._select_frame(_newframe)
1009 1006 if skipped:
1010 1007 print(
1011 1008 f"{Colors.excName} [... skipped {skipped} hidden frame(s)]{ColorsNormal}\n"
1012 1009 )
1013 1010
1014 1011 def do_down(self, arg):
1015 1012 """d(own) [count]
1016 1013 Move the current frame count (default one) levels down in the
1017 1014 stack trace (to a newer frame).
1018 1015
1019 1016 Will skip hidden frames.
1020 1017 """
1021 1018 if self.curindex + 1 == len(self.stack):
1022 1019 self.error("Newest frame")
1023 1020 return
1024 1021 try:
1025 1022 count = int(arg or 1)
1026 1023 except ValueError:
1027 1024 self.error("Invalid frame count (%s)" % arg)
1028 1025 return
1029 1026 if count < 0:
1030 1027 _newframe = len(self.stack) - 1
1031 1028 else:
1032 1029 counter = 0
1033 1030 skipped = 0
1034 1031 hidden_frames = self.hidden_frames(self.stack)
1035 1032 for i in range(self.curindex + 1, len(self.stack)):
1036 1033 if hidden_frames[i] and self.skip_hidden:
1037 1034 skipped += 1
1038 1035 continue
1039 1036 counter += 1
1040 1037 if counter >= count:
1041 1038 break
1042 1039 else:
1043 1040 self.error(
1044 1041 "all frames below hidden, use `skip_hidden False` to get get into those."
1045 1042 )
1046 1043 return
1047 1044
1048 1045 Colors = self.color_scheme_table.active_colors
1049 1046 ColorsNormal = Colors.Normal
1050 1047 if skipped:
1051 1048 print(
1052 1049 f"{Colors.excName} [... skipped {skipped} hidden frame(s)]{ColorsNormal}\n"
1053 1050 )
1054 1051 _newframe = i
1055 1052
1056 1053 self._select_frame(_newframe)
1057 1054
1058 1055 do_d = do_down
1059 1056 do_u = do_up
1060 1057
1061 1058 def do_context(self, context):
1062 1059 """context number_of_lines
1063 1060 Set the number of lines of source code to show when displaying
1064 1061 stacktrace information.
1065 1062 """
1066 1063 try:
1067 1064 new_context = int(context)
1068 1065 if new_context <= 0:
1069 1066 raise ValueError()
1070 1067 self.context = new_context
1071 1068 except ValueError:
1072 1069 self.error("The 'context' command requires a positive integer argument.")
1073 1070
1074 1071
1075 1072 class InterruptiblePdb(Pdb):
1076 1073 """Version of debugger where KeyboardInterrupt exits the debugger altogether."""
1077 1074
1078 1075 def cmdloop(self, intro=None):
1079 1076 """Wrap cmdloop() such that KeyboardInterrupt stops the debugger."""
1080 1077 try:
1081 1078 return OldPdb.cmdloop(self, intro=intro)
1082 1079 except KeyboardInterrupt:
1083 1080 self.stop_here = lambda frame: False
1084 1081 self.do_quit("")
1085 1082 sys.settrace(None)
1086 1083 self.quitting = False
1087 1084 raise
1088 1085
1089 1086 def _cmdloop(self):
1090 1087 while True:
1091 1088 try:
1092 1089 # keyboard interrupts allow for an easy way to cancel
1093 1090 # the current command, so allow them during interactive input
1094 1091 self.allow_kbdint = True
1095 1092 self.cmdloop()
1096 1093 self.allow_kbdint = False
1097 1094 break
1098 1095 except KeyboardInterrupt:
1099 1096 self.message('--KeyboardInterrupt--')
1100 1097 raise
1101 1098
1102 1099
1103 1100 def set_trace(frame=None):
1104 1101 """
1105 1102 Start debugging from `frame`.
1106 1103
1107 1104 If frame is not specified, debugging starts from caller's frame.
1108 1105 """
1109 1106 Pdb().set_trace(frame or sys._getframe().f_back)
@@ -1,1517 +1,1516 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Implementation of execution-related magic functions."""
3 3
4 4 # Copyright (c) IPython Development Team.
5 5 # Distributed under the terms of the Modified BSD License.
6 6
7 7
8 8 import ast
9 9 import bdb
10 10 import builtins as builtin_mod
11 11 import gc
12 12 import itertools
13 13 import os
14 14 import shlex
15 15 import sys
16 16 import time
17 17 import timeit
18 18 import math
19 19 import re
20 20 from pdb import Restart
21 21
22 22 import cProfile as profile
23 23 import pstats
24 24
25 25 from IPython.core import oinspect
26 26 from IPython.core import magic_arguments
27 27 from IPython.core import page
28 28 from IPython.core.error import UsageError
29 29 from IPython.core.macro import Macro
30 30 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
31 31 line_cell_magic, on_off, needs_local_scope,
32 32 no_var_expand)
33 33 from IPython.testing.skipdoctest import skip_doctest
34 34 from IPython.utils.contexts import preserve_keys
35 35 from IPython.utils.capture import capture_output
36 36 from IPython.utils.ipstruct import Struct
37 37 from IPython.utils.module_paths import find_mod
38 38 from IPython.utils.path import get_py_filename, shellglob
39 39 from IPython.utils.timing import clock, clock2
40 40 from warnings import warn
41 41 from logging import error
42 42 from pathlib import Path
43 43 from io import StringIO
44 44 from pathlib import Path
45 45
46 46 if sys.version_info > (3,8):
47 47 from ast import Module
48 48 else :
49 49 # mock the new API, ignore second argument
50 50 # see https://github.com/ipython/ipython/issues/11590
51 51 from ast import Module as OriginalModule
52 52 Module = lambda nodelist, type_ignores: OriginalModule(nodelist)
53 53
54 54
55 55 #-----------------------------------------------------------------------------
56 56 # Magic implementation classes
57 57 #-----------------------------------------------------------------------------
58 58
59 59
60 60 class TimeitResult(object):
61 61 """
62 62 Object returned by the timeit magic with info about the run.
63 63
64 64 Contains the following attributes :
65 65
66 66 loops: (int) number of loops done per measurement
67 67 repeat: (int) number of times the measurement has been repeated
68 68 best: (float) best execution time / number
69 69 all_runs: (list of float) execution time of each run (in s)
70 70 compile_time: (float) time of statement compilation (s)
71 71
72 72 """
73 73 def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision):
74 74 self.loops = loops
75 75 self.repeat = repeat
76 76 self.best = best
77 77 self.worst = worst
78 78 self.all_runs = all_runs
79 79 self.compile_time = compile_time
80 80 self._precision = precision
81 81 self.timings = [ dt / self.loops for dt in all_runs]
82 82
83 83 @property
84 84 def average(self):
85 85 return math.fsum(self.timings) / len(self.timings)
86 86
87 87 @property
88 88 def stdev(self):
89 89 mean = self.average
90 90 return (math.fsum([(x - mean) ** 2 for x in self.timings]) / len(self.timings)) ** 0.5
91 91
92 92 def __str__(self):
93 93 pm = '+-'
94 94 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
95 95 try:
96 96 u'\xb1'.encode(sys.stdout.encoding)
97 97 pm = u'\xb1'
98 98 except:
99 99 pass
100 100 return (
101 101 u"{mean} {pm} {std} per loop (mean {pm} std. dev. of {runs} run{run_plural}, {loops} loop{loop_plural} each)"
102 102 .format(
103 103 pm = pm,
104 104 runs = self.repeat,
105 105 loops = self.loops,
106 106 loop_plural = "" if self.loops == 1 else "s",
107 107 run_plural = "" if self.repeat == 1 else "s",
108 108 mean = _format_time(self.average, self._precision),
109 109 std = _format_time(self.stdev, self._precision))
110 110 )
111 111
112 112 def _repr_pretty_(self, p , cycle):
113 113 unic = self.__str__()
114 114 p.text(u'<TimeitResult : '+unic+u'>')
115 115
116 116
117 117 class TimeitTemplateFiller(ast.NodeTransformer):
118 118 """Fill in the AST template for timing execution.
119 119
120 120 This is quite closely tied to the template definition, which is in
121 121 :meth:`ExecutionMagics.timeit`.
122 122 """
123 123 def __init__(self, ast_setup, ast_stmt):
124 124 self.ast_setup = ast_setup
125 125 self.ast_stmt = ast_stmt
126 126
127 127 def visit_FunctionDef(self, node):
128 128 "Fill in the setup statement"
129 129 self.generic_visit(node)
130 130 if node.name == "inner":
131 131 node.body[:1] = self.ast_setup.body
132 132
133 133 return node
134 134
135 135 def visit_For(self, node):
136 136 "Fill in the statement to be timed"
137 137 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
138 138 node.body = self.ast_stmt.body
139 139 return node
140 140
141 141
142 142 class Timer(timeit.Timer):
143 143 """Timer class that explicitly uses self.inner
144 144
145 145 which is an undocumented implementation detail of CPython,
146 146 not shared by PyPy.
147 147 """
148 148 # Timer.timeit copied from CPython 3.4.2
149 149 def timeit(self, number=timeit.default_number):
150 150 """Time 'number' executions of the main statement.
151 151
152 152 To be precise, this executes the setup statement once, and
153 153 then returns the time it takes to execute the main statement
154 154 a number of times, as a float measured in seconds. The
155 155 argument is the number of times through the loop, defaulting
156 156 to one million. The main statement, the setup statement and
157 157 the timer function to be used are passed to the constructor.
158 158 """
159 159 it = itertools.repeat(None, number)
160 160 gcold = gc.isenabled()
161 161 gc.disable()
162 162 try:
163 163 timing = self.inner(it, self.timer)
164 164 finally:
165 165 if gcold:
166 166 gc.enable()
167 167 return timing
168 168
169 169
170 170 @magics_class
171 171 class ExecutionMagics(Magics):
172 172 """Magics related to code execution, debugging, profiling, etc.
173 173
174 174 """
175 175
176 176 def __init__(self, shell):
177 177 super(ExecutionMagics, self).__init__(shell)
178 178 # Default execution function used to actually run user code.
179 179 self.default_runner = None
180 180
181 181 @skip_doctest
182 182 @no_var_expand
183 183 @line_cell_magic
184 184 def prun(self, parameter_s='', cell=None):
185 185
186 186 """Run a statement through the python code profiler.
187 187
188 188 Usage, in line mode:
189 189 %prun [options] statement
190 190
191 191 Usage, in cell mode:
192 192 %%prun [options] [statement]
193 193 code...
194 194 code...
195 195
196 196 In cell mode, the additional code lines are appended to the (possibly
197 197 empty) statement in the first line. Cell mode allows you to easily
198 198 profile multiline blocks without having to put them in a separate
199 199 function.
200 200
201 201 The given statement (which doesn't require quote marks) is run via the
202 202 python profiler in a manner similar to the profile.run() function.
203 203 Namespaces are internally managed to work correctly; profile.run
204 204 cannot be used in IPython because it makes certain assumptions about
205 205 namespaces which do not hold under IPython.
206 206
207 207 Options:
208 208
209 209 -l <limit>
210 210 you can place restrictions on what or how much of the
211 211 profile gets printed. The limit value can be:
212 212
213 213 * A string: only information for function names containing this string
214 214 is printed.
215 215
216 216 * An integer: only these many lines are printed.
217 217
218 218 * A float (between 0 and 1): this fraction of the report is printed
219 219 (for example, use a limit of 0.4 to see the topmost 40% only).
220 220
221 221 You can combine several limits with repeated use of the option. For
222 222 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
223 223 information about class constructors.
224 224
225 225 -r
226 226 return the pstats.Stats object generated by the profiling. This
227 227 object has all the information about the profile in it, and you can
228 228 later use it for further analysis or in other functions.
229 229
230 230 -s <key>
231 231 sort profile by given key. You can provide more than one key
232 232 by using the option several times: '-s key1 -s key2 -s key3...'. The
233 233 default sorting key is 'time'.
234 234
235 235 The following is copied verbatim from the profile documentation
236 236 referenced below:
237 237
238 238 When more than one key is provided, additional keys are used as
239 239 secondary criteria when the there is equality in all keys selected
240 240 before them.
241 241
242 242 Abbreviations can be used for any key names, as long as the
243 243 abbreviation is unambiguous. The following are the keys currently
244 244 defined:
245 245
246 246 ============ =====================
247 247 Valid Arg Meaning
248 248 ============ =====================
249 249 "calls" call count
250 250 "cumulative" cumulative time
251 251 "file" file name
252 252 "module" file name
253 253 "pcalls" primitive call count
254 254 "line" line number
255 255 "name" function name
256 256 "nfl" name/file/line
257 257 "stdname" standard name
258 258 "time" internal time
259 259 ============ =====================
260 260
261 261 Note that all sorts on statistics are in descending order (placing
262 262 most time consuming items first), where as name, file, and line number
263 263 searches are in ascending order (i.e., alphabetical). The subtle
264 264 distinction between "nfl" and "stdname" is that the standard name is a
265 265 sort of the name as printed, which means that the embedded line
266 266 numbers get compared in an odd way. For example, lines 3, 20, and 40
267 267 would (if the file names were the same) appear in the string order
268 268 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
269 269 line numbers. In fact, sort_stats("nfl") is the same as
270 270 sort_stats("name", "file", "line").
271 271
272 272 -T <filename>
273 273 save profile results as shown on screen to a text
274 274 file. The profile is still shown on screen.
275 275
276 276 -D <filename>
277 277 save (via dump_stats) profile statistics to given
278 278 filename. This data is in a format understood by the pstats module, and
279 279 is generated by a call to the dump_stats() method of profile
280 280 objects. The profile is still shown on screen.
281 281
282 282 -q
283 283 suppress output to the pager. Best used with -T and/or -D above.
284 284
285 285 If you want to run complete programs under the profiler's control, use
286 286 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
287 287 contains profiler specific options as described here.
288 288
289 289 You can read the complete documentation for the profile module with::
290 290
291 291 In [1]: import profile; profile.help()
292 292
293 293 .. versionchanged:: 7.3
294 294 User variables are no longer expanded,
295 295 the magic line is always left unmodified.
296 296
297 297 """
298 298 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
299 299 list_all=True, posix=False)
300 300 if cell is not None:
301 301 arg_str += '\n' + cell
302 302 arg_str = self.shell.transform_cell(arg_str)
303 303 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
304 304
305 305 def _run_with_profiler(self, code, opts, namespace):
306 306 """
307 307 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
308 308
309 309 Parameters
310 310 ----------
311 311 code : str
312 312 Code to be executed.
313 313 opts : Struct
314 314 Options parsed by `self.parse_options`.
315 315 namespace : dict
316 316 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
317 317
318 318 """
319 319
320 320 # Fill default values for unspecified options:
321 321 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
322 322
323 323 prof = profile.Profile()
324 324 try:
325 325 prof = prof.runctx(code, namespace, namespace)
326 326 sys_exit = ''
327 327 except SystemExit:
328 328 sys_exit = """*** SystemExit exception caught in code being profiled."""
329 329
330 330 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
331 331
332 332 lims = opts.l
333 333 if lims:
334 334 lims = [] # rebuild lims with ints/floats/strings
335 335 for lim in opts.l:
336 336 try:
337 337 lims.append(int(lim))
338 338 except ValueError:
339 339 try:
340 340 lims.append(float(lim))
341 341 except ValueError:
342 342 lims.append(lim)
343 343
344 344 # Trap output.
345 345 stdout_trap = StringIO()
346 346 stats_stream = stats.stream
347 347 try:
348 348 stats.stream = stdout_trap
349 349 stats.print_stats(*lims)
350 350 finally:
351 351 stats.stream = stats_stream
352 352
353 353 output = stdout_trap.getvalue()
354 354 output = output.rstrip()
355 355
356 356 if 'q' not in opts:
357 357 page.page(output)
358 358 print(sys_exit, end=' ')
359 359
360 360 dump_file = opts.D[0]
361 361 text_file = opts.T[0]
362 362 if dump_file:
363 363 prof.dump_stats(dump_file)
364 364 print(
365 365 f"\n*** Profile stats marshalled to file {repr(dump_file)}.{sys_exit}"
366 366 )
367 367 if text_file:
368 368 pfile = Path(text_file)
369 369 pfile.touch(exist_ok=True)
370 370 pfile.write_text(output)
371 371
372 372 print(
373 373 f"\n*** Profile printout saved to text file {repr(text_file)}.{sys_exit}"
374 374 )
375 375
376 376 if 'r' in opts:
377 377 return stats
378 378
379 379 return None
380 380
381 381 @line_magic
382 382 def pdb(self, parameter_s=''):
383 383 """Control the automatic calling of the pdb interactive debugger.
384 384
385 385 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
386 386 argument it works as a toggle.
387 387
388 388 When an exception is triggered, IPython can optionally call the
389 389 interactive pdb debugger after the traceback printout. %pdb toggles
390 390 this feature on and off.
391 391
392 392 The initial state of this feature is set in your configuration
393 393 file (the option is ``InteractiveShell.pdb``).
394 394
395 395 If you want to just activate the debugger AFTER an exception has fired,
396 396 without having to type '%pdb on' and rerunning your code, you can use
397 397 the %debug magic."""
398 398
399 399 par = parameter_s.strip().lower()
400 400
401 401 if par:
402 402 try:
403 403 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
404 404 except KeyError:
405 405 print ('Incorrect argument. Use on/1, off/0, '
406 406 'or nothing for a toggle.')
407 407 return
408 408 else:
409 409 # toggle
410 410 new_pdb = not self.shell.call_pdb
411 411
412 412 # set on the shell
413 413 self.shell.call_pdb = new_pdb
414 414 print('Automatic pdb calling has been turned',on_off(new_pdb))
415 415
416 @skip_doctest
417 416 @magic_arguments.magic_arguments()
418 417 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
419 418 help="""
420 419 Set break point at LINE in FILE.
421 420 """
422 421 )
423 422 @magic_arguments.argument('statement', nargs='*',
424 423 help="""
425 424 Code to run in debugger.
426 425 You can omit this in cell magic mode.
427 426 """
428 427 )
429 428 @no_var_expand
430 429 @line_cell_magic
431 430 def debug(self, line='', cell=None):
432 431 """Activate the interactive debugger.
433 432
434 433 This magic command support two ways of activating debugger.
435 434 One is to activate debugger before executing code. This way, you
436 435 can set a break point, to step through the code from the point.
437 436 You can use this mode by giving statements to execute and optionally
438 437 a breakpoint.
439 438
440 439 The other one is to activate debugger in post-mortem mode. You can
441 440 activate this mode simply running %debug without any argument.
442 441 If an exception has just occurred, this lets you inspect its stack
443 442 frames interactively. Note that this will always work only on the last
444 443 traceback that occurred, so you must call this quickly after an
445 444 exception that you wish to inspect has fired, because if another one
446 445 occurs, it clobbers the previous one.
447 446
448 447 If you want IPython to automatically do this on every exception, see
449 448 the %pdb magic for more details.
450 449
451 450 .. versionchanged:: 7.3
452 451 When running code, user variables are no longer expanded,
453 452 the magic line is always left unmodified.
454 453
455 454 """
456 455 args = magic_arguments.parse_argstring(self.debug, line)
457 456
458 457 if not (args.breakpoint or args.statement or cell):
459 458 self._debug_post_mortem()
460 459 elif not (args.breakpoint or cell):
461 460 # If there is no breakpoints, the line is just code to execute
462 461 self._debug_exec(line, None)
463 462 else:
464 463 # Here we try to reconstruct the code from the output of
465 464 # parse_argstring. This might not work if the code has spaces
466 465 # For example this fails for `print("a b")`
467 466 code = "\n".join(args.statement)
468 467 if cell:
469 468 code += "\n" + cell
470 469 self._debug_exec(code, args.breakpoint)
471 470
472 471 def _debug_post_mortem(self):
473 472 self.shell.debugger(force=True)
474 473
475 474 def _debug_exec(self, code, breakpoint):
476 475 if breakpoint:
477 476 (filename, bp_line) = breakpoint.rsplit(':', 1)
478 477 bp_line = int(bp_line)
479 478 else:
480 479 (filename, bp_line) = (None, None)
481 480 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
482 481
483 482 @line_magic
484 483 def tb(self, s):
485 484 """Print the last traceback.
486 485
487 486 Optionally, specify an exception reporting mode, tuning the
488 487 verbosity of the traceback. By default the currently-active exception
489 488 mode is used. See %xmode for changing exception reporting modes.
490 489
491 490 Valid modes: Plain, Context, Verbose, and Minimal.
492 491 """
493 492 interactive_tb = self.shell.InteractiveTB
494 493 if s:
495 494 # Switch exception reporting mode for this one call.
496 495 # Ensure it is switched back.
497 496 def xmode_switch_err(name):
498 497 warn('Error changing %s exception modes.\n%s' %
499 498 (name,sys.exc_info()[1]))
500 499
501 500 new_mode = s.strip().capitalize()
502 501 original_mode = interactive_tb.mode
503 502 try:
504 503 try:
505 504 interactive_tb.set_mode(mode=new_mode)
506 505 except Exception:
507 506 xmode_switch_err('user')
508 507 else:
509 508 self.shell.showtraceback()
510 509 finally:
511 510 interactive_tb.set_mode(mode=original_mode)
512 511 else:
513 512 self.shell.showtraceback()
514 513
515 514 @skip_doctest
516 515 @line_magic
517 516 def run(self, parameter_s='', runner=None,
518 517 file_finder=get_py_filename):
519 518 """Run the named file inside IPython as a program.
520 519
521 520 Usage::
522 521
523 522 %run [-n -i -e -G]
524 523 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
525 524 ( -m mod | filename ) [args]
526 525
527 526 The filename argument should be either a pure Python script (with
528 527 extension ``.py``), or a file with custom IPython syntax (such as
529 528 magics). If the latter, the file can be either a script with ``.ipy``
530 529 extension, or a Jupyter notebook with ``.ipynb`` extension. When running
531 530 a Jupyter notebook, the output from print statements and other
532 531 displayed objects will appear in the terminal (even matplotlib figures
533 532 will open, if a terminal-compliant backend is being used). Note that,
534 533 at the system command line, the ``jupyter run`` command offers similar
535 534 functionality for executing notebooks (albeit currently with some
536 535 differences in supported options).
537 536
538 537 Parameters after the filename are passed as command-line arguments to
539 538 the program (put in sys.argv). Then, control returns to IPython's
540 539 prompt.
541 540
542 541 This is similar to running at a system prompt ``python file args``,
543 542 but with the advantage of giving you IPython's tracebacks, and of
544 543 loading all variables into your interactive namespace for further use
545 544 (unless -p is used, see below).
546 545
547 546 The file is executed in a namespace initially consisting only of
548 547 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
549 548 sees its environment as if it were being run as a stand-alone program
550 549 (except for sharing global objects such as previously imported
551 550 modules). But after execution, the IPython interactive namespace gets
552 551 updated with all variables defined in the program (except for __name__
553 552 and sys.argv). This allows for very convenient loading of code for
554 553 interactive work, while giving each program a 'clean sheet' to run in.
555 554
556 555 Arguments are expanded using shell-like glob match. Patterns
557 556 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
558 557 tilde '~' will be expanded into user's home directory. Unlike
559 558 real shells, quotation does not suppress expansions. Use
560 559 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
561 560 To completely disable these expansions, you can use -G flag.
562 561
563 562 On Windows systems, the use of single quotes `'` when specifying
564 563 a file is not supported. Use double quotes `"`.
565 564
566 565 Options:
567 566
568 567 -n
569 568 __name__ is NOT set to '__main__', but to the running file's name
570 569 without extension (as python does under import). This allows running
571 570 scripts and reloading the definitions in them without calling code
572 571 protected by an ``if __name__ == "__main__"`` clause.
573 572
574 573 -i
575 574 run the file in IPython's namespace instead of an empty one. This
576 575 is useful if you are experimenting with code written in a text editor
577 576 which depends on variables defined interactively.
578 577
579 578 -e
580 579 ignore sys.exit() calls or SystemExit exceptions in the script
581 580 being run. This is particularly useful if IPython is being used to
582 581 run unittests, which always exit with a sys.exit() call. In such
583 582 cases you are interested in the output of the test results, not in
584 583 seeing a traceback of the unittest module.
585 584
586 585 -t
587 586 print timing information at the end of the run. IPython will give
588 587 you an estimated CPU time consumption for your script, which under
589 588 Unix uses the resource module to avoid the wraparound problems of
590 589 time.clock(). Under Unix, an estimate of time spent on system tasks
591 590 is also given (for Windows platforms this is reported as 0.0).
592 591
593 592 If -t is given, an additional ``-N<N>`` option can be given, where <N>
594 593 must be an integer indicating how many times you want the script to
595 594 run. The final timing report will include total and per run results.
596 595
597 596 For example (testing the script uniq_stable.py)::
598 597
599 598 In [1]: run -t uniq_stable
600 599
601 600 IPython CPU timings (estimated):
602 601 User : 0.19597 s.
603 602 System: 0.0 s.
604 603
605 604 In [2]: run -t -N5 uniq_stable
606 605
607 606 IPython CPU timings (estimated):
608 607 Total runs performed: 5
609 608 Times : Total Per run
610 609 User : 0.910862 s, 0.1821724 s.
611 610 System: 0.0 s, 0.0 s.
612 611
613 612 -d
614 613 run your program under the control of pdb, the Python debugger.
615 614 This allows you to execute your program step by step, watch variables,
616 615 etc. Internally, what IPython does is similar to calling::
617 616
618 617 pdb.run('execfile("YOURFILENAME")')
619 618
620 619 with a breakpoint set on line 1 of your file. You can change the line
621 620 number for this automatic breakpoint to be <N> by using the -bN option
622 621 (where N must be an integer). For example::
623 622
624 623 %run -d -b40 myscript
625 624
626 625 will set the first breakpoint at line 40 in myscript.py. Note that
627 626 the first breakpoint must be set on a line which actually does
628 627 something (not a comment or docstring) for it to stop execution.
629 628
630 629 Or you can specify a breakpoint in a different file::
631 630
632 631 %run -d -b myotherfile.py:20 myscript
633 632
634 633 When the pdb debugger starts, you will see a (Pdb) prompt. You must
635 634 first enter 'c' (without quotes) to start execution up to the first
636 635 breakpoint.
637 636
638 637 Entering 'help' gives information about the use of the debugger. You
639 638 can easily see pdb's full documentation with "import pdb;pdb.help()"
640 639 at a prompt.
641 640
642 641 -p
643 642 run program under the control of the Python profiler module (which
644 643 prints a detailed report of execution times, function calls, etc).
645 644
646 645 You can pass other options after -p which affect the behavior of the
647 646 profiler itself. See the docs for %prun for details.
648 647
649 648 In this mode, the program's variables do NOT propagate back to the
650 649 IPython interactive namespace (because they remain in the namespace
651 650 where the profiler executes them).
652 651
653 652 Internally this triggers a call to %prun, see its documentation for
654 653 details on the options available specifically for profiling.
655 654
656 655 There is one special usage for which the text above doesn't apply:
657 656 if the filename ends with .ipy[nb], the file is run as ipython script,
658 657 just as if the commands were written on IPython prompt.
659 658
660 659 -m
661 660 specify module name to load instead of script path. Similar to
662 661 the -m option for the python interpreter. Use this option last if you
663 662 want to combine with other %run options. Unlike the python interpreter
664 663 only source modules are allowed no .pyc or .pyo files.
665 664 For example::
666 665
667 666 %run -m example
668 667
669 668 will run the example module.
670 669
671 670 -G
672 671 disable shell-like glob expansion of arguments.
673 672
674 673 """
675 674
676 675 # Logic to handle issue #3664
677 676 # Add '--' after '-m <module_name>' to ignore additional args passed to a module.
678 677 if '-m' in parameter_s and '--' not in parameter_s:
679 678 argv = shlex.split(parameter_s, posix=(os.name == 'posix'))
680 679 for idx, arg in enumerate(argv):
681 680 if arg and arg.startswith('-') and arg != '-':
682 681 if arg == '-m':
683 682 argv.insert(idx + 2, '--')
684 683 break
685 684 else:
686 685 # Positional arg, break
687 686 break
688 687 parameter_s = ' '.join(shlex.quote(arg) for arg in argv)
689 688
690 689 # get arguments and set sys.argv for program to be run.
691 690 opts, arg_lst = self.parse_options(parameter_s,
692 691 'nidtN:b:pD:l:rs:T:em:G',
693 692 mode='list', list_all=1)
694 693 if "m" in opts:
695 694 modulename = opts["m"][0]
696 695 modpath = find_mod(modulename)
697 696 if modpath is None:
698 697 msg = '%r is not a valid modulename on sys.path'%modulename
699 698 raise Exception(msg)
700 699 arg_lst = [modpath] + arg_lst
701 700 try:
702 701 fpath = None # initialize to make sure fpath is in scope later
703 702 fpath = arg_lst[0]
704 703 filename = file_finder(fpath)
705 704 except IndexError as e:
706 705 msg = 'you must provide at least a filename.'
707 706 raise Exception(msg) from e
708 707 except IOError as e:
709 708 try:
710 709 msg = str(e)
711 710 except UnicodeError:
712 711 msg = e.message
713 712 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
714 713 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
715 714 raise Exception(msg) from e
716 715 except TypeError:
717 716 if fpath in sys.meta_path:
718 717 filename = ""
719 718 else:
720 719 raise
721 720
722 721 if filename.lower().endswith(('.ipy', '.ipynb')):
723 722 with preserve_keys(self.shell.user_ns, '__file__'):
724 723 self.shell.user_ns['__file__'] = filename
725 724 self.shell.safe_execfile_ipy(filename, raise_exceptions=True)
726 725 return
727 726
728 727 # Control the response to exit() calls made by the script being run
729 728 exit_ignore = 'e' in opts
730 729
731 730 # Make sure that the running script gets a proper sys.argv as if it
732 731 # were run from a system shell.
733 732 save_argv = sys.argv # save it for later restoring
734 733
735 734 if 'G' in opts:
736 735 args = arg_lst[1:]
737 736 else:
738 737 # tilde and glob expansion
739 738 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
740 739
741 740 sys.argv = [filename] + args # put in the proper filename
742 741
743 742 if 'n' in opts:
744 743 name = Path(filename).stem
745 744 else:
746 745 name = '__main__'
747 746
748 747 if 'i' in opts:
749 748 # Run in user's interactive namespace
750 749 prog_ns = self.shell.user_ns
751 750 __name__save = self.shell.user_ns['__name__']
752 751 prog_ns['__name__'] = name
753 752 main_mod = self.shell.user_module
754 753
755 754 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
756 755 # set the __file__ global in the script's namespace
757 756 # TK: Is this necessary in interactive mode?
758 757 prog_ns['__file__'] = filename
759 758 else:
760 759 # Run in a fresh, empty namespace
761 760
762 761 # The shell MUST hold a reference to prog_ns so after %run
763 762 # exits, the python deletion mechanism doesn't zero it out
764 763 # (leaving dangling references). See interactiveshell for details
765 764 main_mod = self.shell.new_main_mod(filename, name)
766 765 prog_ns = main_mod.__dict__
767 766
768 767 # pickle fix. See interactiveshell for an explanation. But we need to
769 768 # make sure that, if we overwrite __main__, we replace it at the end
770 769 main_mod_name = prog_ns['__name__']
771 770
772 771 if main_mod_name == '__main__':
773 772 restore_main = sys.modules['__main__']
774 773 else:
775 774 restore_main = False
776 775
777 776 # This needs to be undone at the end to prevent holding references to
778 777 # every single object ever created.
779 778 sys.modules[main_mod_name] = main_mod
780 779
781 780 if 'p' in opts or 'd' in opts:
782 781 if 'm' in opts:
783 782 code = 'run_module(modulename, prog_ns)'
784 783 code_ns = {
785 784 'run_module': self.shell.safe_run_module,
786 785 'prog_ns': prog_ns,
787 786 'modulename': modulename,
788 787 }
789 788 else:
790 789 if 'd' in opts:
791 790 # allow exceptions to raise in debug mode
792 791 code = 'execfile(filename, prog_ns, raise_exceptions=True)'
793 792 else:
794 793 code = 'execfile(filename, prog_ns)'
795 794 code_ns = {
796 795 'execfile': self.shell.safe_execfile,
797 796 'prog_ns': prog_ns,
798 797 'filename': get_py_filename(filename),
799 798 }
800 799
801 800 try:
802 801 stats = None
803 802 if 'p' in opts:
804 803 stats = self._run_with_profiler(code, opts, code_ns)
805 804 else:
806 805 if 'd' in opts:
807 806 bp_file, bp_line = parse_breakpoint(
808 807 opts.get('b', ['1'])[0], filename)
809 808 self._run_with_debugger(
810 809 code, code_ns, filename, bp_line, bp_file)
811 810 else:
812 811 if 'm' in opts:
813 812 def run():
814 813 self.shell.safe_run_module(modulename, prog_ns)
815 814 else:
816 815 if runner is None:
817 816 runner = self.default_runner
818 817 if runner is None:
819 818 runner = self.shell.safe_execfile
820 819
821 820 def run():
822 821 runner(filename, prog_ns, prog_ns,
823 822 exit_ignore=exit_ignore)
824 823
825 824 if 't' in opts:
826 825 # timed execution
827 826 try:
828 827 nruns = int(opts['N'][0])
829 828 if nruns < 1:
830 829 error('Number of runs must be >=1')
831 830 return
832 831 except (KeyError):
833 832 nruns = 1
834 833 self._run_with_timing(run, nruns)
835 834 else:
836 835 # regular execution
837 836 run()
838 837
839 838 if 'i' in opts:
840 839 self.shell.user_ns['__name__'] = __name__save
841 840 else:
842 841 # update IPython interactive namespace
843 842
844 843 # Some forms of read errors on the file may mean the
845 844 # __name__ key was never set; using pop we don't have to
846 845 # worry about a possible KeyError.
847 846 prog_ns.pop('__name__', None)
848 847
849 848 with preserve_keys(self.shell.user_ns, '__file__'):
850 849 self.shell.user_ns.update(prog_ns)
851 850 finally:
852 851 # It's a bit of a mystery why, but __builtins__ can change from
853 852 # being a module to becoming a dict missing some key data after
854 853 # %run. As best I can see, this is NOT something IPython is doing
855 854 # at all, and similar problems have been reported before:
856 855 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
857 856 # Since this seems to be done by the interpreter itself, the best
858 857 # we can do is to at least restore __builtins__ for the user on
859 858 # exit.
860 859 self.shell.user_ns['__builtins__'] = builtin_mod
861 860
862 861 # Ensure key global structures are restored
863 862 sys.argv = save_argv
864 863 if restore_main:
865 864 sys.modules['__main__'] = restore_main
866 865 if '__mp_main__' in sys.modules:
867 866 sys.modules['__mp_main__'] = restore_main
868 867 else:
869 868 # Remove from sys.modules the reference to main_mod we'd
870 869 # added. Otherwise it will trap references to objects
871 870 # contained therein.
872 871 del sys.modules[main_mod_name]
873 872
874 873 return stats
875 874
876 875 def _run_with_debugger(self, code, code_ns, filename=None,
877 876 bp_line=None, bp_file=None):
878 877 """
879 878 Run `code` in debugger with a break point.
880 879
881 880 Parameters
882 881 ----------
883 882 code : str
884 883 Code to execute.
885 884 code_ns : dict
886 885 A namespace in which `code` is executed.
887 886 filename : str
888 887 `code` is ran as if it is in `filename`.
889 888 bp_line : int, optional
890 889 Line number of the break point.
891 890 bp_file : str, optional
892 891 Path to the file in which break point is specified.
893 892 `filename` is used if not given.
894 893
895 894 Raises
896 895 ------
897 896 UsageError
898 897 If the break point given by `bp_line` is not valid.
899 898
900 899 """
901 900 deb = self.shell.InteractiveTB.pdb
902 901 if not deb:
903 902 self.shell.InteractiveTB.pdb = self.shell.InteractiveTB.debugger_cls()
904 903 deb = self.shell.InteractiveTB.pdb
905 904
906 905 # deb.checkline() fails if deb.curframe exists but is None; it can
907 906 # handle it not existing. https://github.com/ipython/ipython/issues/10028
908 907 if hasattr(deb, 'curframe'):
909 908 del deb.curframe
910 909
911 910 # reset Breakpoint state, which is moronically kept
912 911 # in a class
913 912 bdb.Breakpoint.next = 1
914 913 bdb.Breakpoint.bplist = {}
915 914 bdb.Breakpoint.bpbynumber = [None]
916 915 deb.clear_all_breaks()
917 916 if bp_line is not None:
918 917 # Set an initial breakpoint to stop execution
919 918 maxtries = 10
920 919 bp_file = bp_file or filename
921 920 checkline = deb.checkline(bp_file, bp_line)
922 921 if not checkline:
923 922 for bp in range(bp_line + 1, bp_line + maxtries + 1):
924 923 if deb.checkline(bp_file, bp):
925 924 break
926 925 else:
927 926 msg = ("\nI failed to find a valid line to set "
928 927 "a breakpoint\n"
929 928 "after trying up to line: %s.\n"
930 929 "Please set a valid breakpoint manually "
931 930 "with the -b option." % bp)
932 931 raise UsageError(msg)
933 932 # if we find a good linenumber, set the breakpoint
934 933 deb.do_break('%s:%s' % (bp_file, bp_line))
935 934
936 935 if filename:
937 936 # Mimic Pdb._runscript(...)
938 937 deb._wait_for_mainpyfile = True
939 938 deb.mainpyfile = deb.canonic(filename)
940 939
941 940 # Start file run
942 941 print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt)
943 942 try:
944 943 if filename:
945 944 # save filename so it can be used by methods on the deb object
946 945 deb._exec_filename = filename
947 946 while True:
948 947 try:
949 948 trace = sys.gettrace()
950 949 deb.run(code, code_ns)
951 950 except Restart:
952 951 print("Restarting")
953 952 if filename:
954 953 deb._wait_for_mainpyfile = True
955 954 deb.mainpyfile = deb.canonic(filename)
956 955 continue
957 956 else:
958 957 break
959 958 finally:
960 959 sys.settrace(trace)
961 960
962 961
963 962 except:
964 963 etype, value, tb = sys.exc_info()
965 964 # Skip three frames in the traceback: the %run one,
966 965 # one inside bdb.py, and the command-line typed by the
967 966 # user (run by exec in pdb itself).
968 967 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
969 968
970 969 @staticmethod
971 970 def _run_with_timing(run, nruns):
972 971 """
973 972 Run function `run` and print timing information.
974 973
975 974 Parameters
976 975 ----------
977 976 run : callable
978 977 Any callable object which takes no argument.
979 978 nruns : int
980 979 Number of times to execute `run`.
981 980
982 981 """
983 982 twall0 = time.perf_counter()
984 983 if nruns == 1:
985 984 t0 = clock2()
986 985 run()
987 986 t1 = clock2()
988 987 t_usr = t1[0] - t0[0]
989 988 t_sys = t1[1] - t0[1]
990 989 print("\nIPython CPU timings (estimated):")
991 990 print(" User : %10.2f s." % t_usr)
992 991 print(" System : %10.2f s." % t_sys)
993 992 else:
994 993 runs = range(nruns)
995 994 t0 = clock2()
996 995 for nr in runs:
997 996 run()
998 997 t1 = clock2()
999 998 t_usr = t1[0] - t0[0]
1000 999 t_sys = t1[1] - t0[1]
1001 1000 print("\nIPython CPU timings (estimated):")
1002 1001 print("Total runs performed:", nruns)
1003 1002 print(" Times : %10s %10s" % ('Total', 'Per run'))
1004 1003 print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
1005 1004 print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
1006 1005 twall1 = time.perf_counter()
1007 1006 print("Wall time: %10.2f s." % (twall1 - twall0))
1008 1007
1009 1008 @skip_doctest
1010 1009 @no_var_expand
1011 1010 @line_cell_magic
1012 1011 @needs_local_scope
1013 1012 def timeit(self, line='', cell=None, local_ns=None):
1014 1013 """Time execution of a Python statement or expression
1015 1014
1016 1015 Usage, in line mode:
1017 1016 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
1018 1017 or in cell mode:
1019 1018 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
1020 1019 code
1021 1020 code...
1022 1021
1023 1022 Time execution of a Python statement or expression using the timeit
1024 1023 module. This function can be used both as a line and cell magic:
1025 1024
1026 1025 - In line mode you can time a single-line statement (though multiple
1027 1026 ones can be chained with using semicolons).
1028 1027
1029 1028 - In cell mode, the statement in the first line is used as setup code
1030 1029 (executed but not timed) and the body of the cell is timed. The cell
1031 1030 body has access to any variables created in the setup code.
1032 1031
1033 1032 Options:
1034 1033 -n<N>: execute the given statement <N> times in a loop. If <N> is not
1035 1034 provided, <N> is determined so as to get sufficient accuracy.
1036 1035
1037 1036 -r<R>: number of repeats <R>, each consisting of <N> loops, and take the
1038 1037 best result.
1039 1038 Default: 7
1040 1039
1041 1040 -t: use time.time to measure the time, which is the default on Unix.
1042 1041 This function measures wall time.
1043 1042
1044 1043 -c: use time.clock to measure the time, which is the default on
1045 1044 Windows and measures wall time. On Unix, resource.getrusage is used
1046 1045 instead and returns the CPU user time.
1047 1046
1048 1047 -p<P>: use a precision of <P> digits to display the timing result.
1049 1048 Default: 3
1050 1049
1051 1050 -q: Quiet, do not print result.
1052 1051
1053 1052 -o: return a TimeitResult that can be stored in a variable to inspect
1054 1053 the result in more details.
1055 1054
1056 1055 .. versionchanged:: 7.3
1057 1056 User variables are no longer expanded,
1058 1057 the magic line is always left unmodified.
1059 1058
1060 1059 Examples
1061 1060 --------
1062 1061 ::
1063 1062
1064 1063 In [1]: %timeit pass
1065 1064 8.26 ns Β± 0.12 ns per loop (mean Β± std. dev. of 7 runs, 100000000 loops each)
1066 1065
1067 1066 In [2]: u = None
1068 1067
1069 1068 In [3]: %timeit u is None
1070 1069 29.9 ns Β± 0.643 ns per loop (mean Β± std. dev. of 7 runs, 10000000 loops each)
1071 1070
1072 1071 In [4]: %timeit -r 4 u == None
1073 1072
1074 1073 In [5]: import time
1075 1074
1076 1075 In [6]: %timeit -n1 time.sleep(2)
1077 1076
1078 1077
1079 1078 The times reported by %timeit will be slightly higher than those
1080 1079 reported by the timeit.py script when variables are accessed. This is
1081 1080 due to the fact that %timeit executes the statement in the namespace
1082 1081 of the shell, compared with timeit.py, which uses a single setup
1083 1082 statement to import function or create variables. Generally, the bias
1084 1083 does not matter as long as results from timeit.py are not mixed with
1085 1084 those from %timeit."""
1086 1085
1087 1086 opts, stmt = self.parse_options(
1088 1087 line, "n:r:tcp:qo", posix=False, strict=False, preserve_non_opts=True
1089 1088 )
1090 1089 if stmt == "" and cell is None:
1091 1090 return
1092 1091
1093 1092 timefunc = timeit.default_timer
1094 1093 number = int(getattr(opts, "n", 0))
1095 1094 default_repeat = 7 if timeit.default_repeat < 7 else timeit.default_repeat
1096 1095 repeat = int(getattr(opts, "r", default_repeat))
1097 1096 precision = int(getattr(opts, "p", 3))
1098 1097 quiet = 'q' in opts
1099 1098 return_result = 'o' in opts
1100 1099 if hasattr(opts, "t"):
1101 1100 timefunc = time.time
1102 1101 if hasattr(opts, "c"):
1103 1102 timefunc = clock
1104 1103
1105 1104 timer = Timer(timer=timefunc)
1106 1105 # this code has tight coupling to the inner workings of timeit.Timer,
1107 1106 # but is there a better way to achieve that the code stmt has access
1108 1107 # to the shell namespace?
1109 1108 transform = self.shell.transform_cell
1110 1109
1111 1110 if cell is None:
1112 1111 # called as line magic
1113 1112 ast_setup = self.shell.compile.ast_parse("pass")
1114 1113 ast_stmt = self.shell.compile.ast_parse(transform(stmt))
1115 1114 else:
1116 1115 ast_setup = self.shell.compile.ast_parse(transform(stmt))
1117 1116 ast_stmt = self.shell.compile.ast_parse(transform(cell))
1118 1117
1119 1118 ast_setup = self.shell.transform_ast(ast_setup)
1120 1119 ast_stmt = self.shell.transform_ast(ast_stmt)
1121 1120
1122 1121 # Check that these compile to valid Python code *outside* the timer func
1123 1122 # Invalid code may become valid when put inside the function & loop,
1124 1123 # which messes up error messages.
1125 1124 # https://github.com/ipython/ipython/issues/10636
1126 1125 self.shell.compile(ast_setup, "<magic-timeit-setup>", "exec")
1127 1126 self.shell.compile(ast_stmt, "<magic-timeit-stmt>", "exec")
1128 1127
1129 1128 # This codestring is taken from timeit.template - we fill it in as an
1130 1129 # AST, so that we can apply our AST transformations to the user code
1131 1130 # without affecting the timing code.
1132 1131 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
1133 1132 ' setup\n'
1134 1133 ' _t0 = _timer()\n'
1135 1134 ' for _i in _it:\n'
1136 1135 ' stmt\n'
1137 1136 ' _t1 = _timer()\n'
1138 1137 ' return _t1 - _t0\n')
1139 1138
1140 1139 timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template)
1141 1140 timeit_ast = ast.fix_missing_locations(timeit_ast)
1142 1141
1143 1142 # Track compilation time so it can be reported if too long
1144 1143 # Minimum time above which compilation time will be reported
1145 1144 tc_min = 0.1
1146 1145
1147 1146 t0 = clock()
1148 1147 code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec")
1149 1148 tc = clock()-t0
1150 1149
1151 1150 ns = {}
1152 1151 glob = self.shell.user_ns
1153 1152 # handles global vars with same name as local vars. We store them in conflict_globs.
1154 1153 conflict_globs = {}
1155 1154 if local_ns and cell is None:
1156 1155 for var_name, var_val in glob.items():
1157 1156 if var_name in local_ns:
1158 1157 conflict_globs[var_name] = var_val
1159 1158 glob.update(local_ns)
1160 1159
1161 1160 exec(code, glob, ns)
1162 1161 timer.inner = ns["inner"]
1163 1162
1164 1163 # This is used to check if there is a huge difference between the
1165 1164 # best and worst timings.
1166 1165 # Issue: https://github.com/ipython/ipython/issues/6471
1167 1166 if number == 0:
1168 1167 # determine number so that 0.2 <= total time < 2.0
1169 1168 for index in range(0, 10):
1170 1169 number = 10 ** index
1171 1170 time_number = timer.timeit(number)
1172 1171 if time_number >= 0.2:
1173 1172 break
1174 1173
1175 1174 all_runs = timer.repeat(repeat, number)
1176 1175 best = min(all_runs) / number
1177 1176 worst = max(all_runs) / number
1178 1177 timeit_result = TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1179 1178
1180 1179 # Restore global vars from conflict_globs
1181 1180 if conflict_globs:
1182 1181 glob.update(conflict_globs)
1183 1182
1184 1183 if not quiet :
1185 1184 # Check best timing is greater than zero to avoid a
1186 1185 # ZeroDivisionError.
1187 1186 # In cases where the slowest timing is lesser than a microsecond
1188 1187 # we assume that it does not really matter if the fastest
1189 1188 # timing is 4 times faster than the slowest timing or not.
1190 1189 if worst > 4 * best and best > 0 and worst > 1e-6:
1191 1190 print("The slowest run took %0.2f times longer than the "
1192 1191 "fastest. This could mean that an intermediate result "
1193 1192 "is being cached." % (worst / best))
1194 1193
1195 1194 print( timeit_result )
1196 1195
1197 1196 if tc > tc_min:
1198 1197 print("Compiler time: %.2f s" % tc)
1199 1198 if return_result:
1200 1199 return timeit_result
1201 1200
1202 1201 @skip_doctest
1203 1202 @no_var_expand
1204 1203 @needs_local_scope
1205 1204 @line_cell_magic
1206 1205 def time(self,line='', cell=None, local_ns=None):
1207 1206 """Time execution of a Python statement or expression.
1208 1207
1209 1208 The CPU and wall clock times are printed, and the value of the
1210 1209 expression (if any) is returned. Note that under Win32, system time
1211 1210 is always reported as 0, since it can not be measured.
1212 1211
1213 1212 This function can be used both as a line and cell magic:
1214 1213
1215 1214 - In line mode you can time a single-line statement (though multiple
1216 1215 ones can be chained with using semicolons).
1217 1216
1218 1217 - In cell mode, you can time the cell body (a directly
1219 1218 following statement raises an error).
1220 1219
1221 1220 This function provides very basic timing functionality. Use the timeit
1222 1221 magic for more control over the measurement.
1223 1222
1224 1223 .. versionchanged:: 7.3
1225 1224 User variables are no longer expanded,
1226 1225 the magic line is always left unmodified.
1227 1226
1228 1227 Examples
1229 1228 --------
1230 1229 ::
1231 1230
1232 1231 In [1]: %time 2**128
1233 1232 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1234 1233 Wall time: 0.00
1235 1234 Out[1]: 340282366920938463463374607431768211456L
1236 1235
1237 1236 In [2]: n = 1000000
1238 1237
1239 1238 In [3]: %time sum(range(n))
1240 1239 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1241 1240 Wall time: 1.37
1242 1241 Out[3]: 499999500000L
1243 1242
1244 1243 In [4]: %time print 'hello world'
1245 1244 hello world
1246 1245 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1247 1246 Wall time: 0.00
1248 1247
1249 1248
1250 1249 .. note::
1251 1250 The time needed by Python to compile the given expression will be
1252 1251 reported if it is more than 0.1s.
1253 1252
1254 1253 In the example below, the actual exponentiation is done by Python
1255 1254 at compilation time, so while the expression can take a noticeable
1256 1255 amount of time to compute, that time is purely due to the
1257 1256 compilation::
1258 1257
1259 1258 In [5]: %time 3**9999;
1260 1259 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1261 1260 Wall time: 0.00 s
1262 1261
1263 1262 In [6]: %time 3**999999;
1264 1263 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1265 1264 Wall time: 0.00 s
1266 1265 Compiler : 0.78 s
1267 1266 """
1268 1267 # fail immediately if the given expression can't be compiled
1269 1268
1270 1269 if line and cell:
1271 1270 raise UsageError("Can't use statement directly after '%%time'!")
1272 1271
1273 1272 if cell:
1274 1273 expr = self.shell.transform_cell(cell)
1275 1274 else:
1276 1275 expr = self.shell.transform_cell(line)
1277 1276
1278 1277 # Minimum time above which parse time will be reported
1279 1278 tp_min = 0.1
1280 1279
1281 1280 t0 = clock()
1282 1281 expr_ast = self.shell.compile.ast_parse(expr)
1283 1282 tp = clock()-t0
1284 1283
1285 1284 # Apply AST transformations
1286 1285 expr_ast = self.shell.transform_ast(expr_ast)
1287 1286
1288 1287 # Minimum time above which compilation time will be reported
1289 1288 tc_min = 0.1
1290 1289
1291 1290 expr_val=None
1292 1291 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1293 1292 mode = 'eval'
1294 1293 source = '<timed eval>'
1295 1294 expr_ast = ast.Expression(expr_ast.body[0].value)
1296 1295 else:
1297 1296 mode = 'exec'
1298 1297 source = '<timed exec>'
1299 1298 # multi-line %%time case
1300 1299 if len(expr_ast.body) > 1 and isinstance(expr_ast.body[-1], ast.Expr):
1301 1300 expr_val= expr_ast.body[-1]
1302 1301 expr_ast = expr_ast.body[:-1]
1303 1302 expr_ast = Module(expr_ast, [])
1304 1303 expr_val = ast.Expression(expr_val.value)
1305 1304
1306 1305 t0 = clock()
1307 1306 code = self.shell.compile(expr_ast, source, mode)
1308 1307 tc = clock()-t0
1309 1308
1310 1309 # skew measurement as little as possible
1311 1310 glob = self.shell.user_ns
1312 1311 wtime = time.time
1313 1312 # time execution
1314 1313 wall_st = wtime()
1315 1314 if mode=='eval':
1316 1315 st = clock2()
1317 1316 try:
1318 1317 out = eval(code, glob, local_ns)
1319 1318 except:
1320 1319 self.shell.showtraceback()
1321 1320 return
1322 1321 end = clock2()
1323 1322 else:
1324 1323 st = clock2()
1325 1324 try:
1326 1325 exec(code, glob, local_ns)
1327 1326 out=None
1328 1327 # multi-line %%time case
1329 1328 if expr_val is not None:
1330 1329 code_2 = self.shell.compile(expr_val, source, 'eval')
1331 1330 out = eval(code_2, glob, local_ns)
1332 1331 except:
1333 1332 self.shell.showtraceback()
1334 1333 return
1335 1334 end = clock2()
1336 1335
1337 1336 wall_end = wtime()
1338 1337 # Compute actual times and report
1339 1338 wall_time = wall_end-wall_st
1340 1339 cpu_user = end[0]-st[0]
1341 1340 cpu_sys = end[1]-st[1]
1342 1341 cpu_tot = cpu_user+cpu_sys
1343 1342 # On windows cpu_sys is always zero, so no new information to the next print
1344 1343 if sys.platform != 'win32':
1345 1344 print("CPU times: user %s, sys: %s, total: %s" % \
1346 1345 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)))
1347 1346 print("Wall time: %s" % _format_time(wall_time))
1348 1347 if tc > tc_min:
1349 1348 print("Compiler : %s" % _format_time(tc))
1350 1349 if tp > tp_min:
1351 1350 print("Parser : %s" % _format_time(tp))
1352 1351 return out
1353 1352
1354 1353 @skip_doctest
1355 1354 @line_magic
1356 1355 def macro(self, parameter_s=''):
1357 1356 """Define a macro for future re-execution. It accepts ranges of history,
1358 1357 filenames or string objects.
1359 1358
1360 1359 Usage:\\
1361 1360 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1362 1361
1363 1362 Options:
1364 1363
1365 1364 -r: use 'raw' input. By default, the 'processed' history is used,
1366 1365 so that magics are loaded in their transformed version to valid
1367 1366 Python. If this option is given, the raw input as typed at the
1368 1367 command line is used instead.
1369 1368
1370 1369 -q: quiet macro definition. By default, a tag line is printed
1371 1370 to indicate the macro has been created, and then the contents of
1372 1371 the macro are printed. If this option is given, then no printout
1373 1372 is produced once the macro is created.
1374 1373
1375 1374 This will define a global variable called `name` which is a string
1376 1375 made of joining the slices and lines you specify (n1,n2,... numbers
1377 1376 above) from your input history into a single string. This variable
1378 1377 acts like an automatic function which re-executes those lines as if
1379 1378 you had typed them. You just type 'name' at the prompt and the code
1380 1379 executes.
1381 1380
1382 1381 The syntax for indicating input ranges is described in %history.
1383 1382
1384 1383 Note: as a 'hidden' feature, you can also use traditional python slice
1385 1384 notation, where N:M means numbers N through M-1.
1386 1385
1387 1386 For example, if your history contains (print using %hist -n )::
1388 1387
1389 1388 44: x=1
1390 1389 45: y=3
1391 1390 46: z=x+y
1392 1391 47: print x
1393 1392 48: a=5
1394 1393 49: print 'x',x,'y',y
1395 1394
1396 1395 you can create a macro with lines 44 through 47 (included) and line 49
1397 1396 called my_macro with::
1398 1397
1399 1398 In [55]: %macro my_macro 44-47 49
1400 1399
1401 1400 Now, typing `my_macro` (without quotes) will re-execute all this code
1402 1401 in one pass.
1403 1402
1404 1403 You don't need to give the line-numbers in order, and any given line
1405 1404 number can appear multiple times. You can assemble macros with any
1406 1405 lines from your input history in any order.
1407 1406
1408 1407 The macro is a simple object which holds its value in an attribute,
1409 1408 but IPython's display system checks for macros and executes them as
1410 1409 code instead of printing them when you type their name.
1411 1410
1412 1411 You can view a macro's contents by explicitly printing it with::
1413 1412
1414 1413 print macro_name
1415 1414
1416 1415 """
1417 1416 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1418 1417 if not args: # List existing macros
1419 1418 return sorted(k for k,v in self.shell.user_ns.items() if isinstance(v, Macro))
1420 1419 if len(args) == 1:
1421 1420 raise UsageError(
1422 1421 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1423 1422 name, codefrom = args[0], " ".join(args[1:])
1424 1423
1425 1424 #print 'rng',ranges # dbg
1426 1425 try:
1427 1426 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1428 1427 except (ValueError, TypeError) as e:
1429 1428 print(e.args[0])
1430 1429 return
1431 1430 macro = Macro(lines)
1432 1431 self.shell.define_macro(name, macro)
1433 1432 if not ( 'q' in opts) :
1434 1433 print('Macro `%s` created. To execute, type its name (without quotes).' % name)
1435 1434 print('=== Macro contents: ===')
1436 1435 print(macro, end=' ')
1437 1436
1438 1437 @magic_arguments.magic_arguments()
1439 1438 @magic_arguments.argument('output', type=str, default='', nargs='?',
1440 1439 help="""The name of the variable in which to store output.
1441 1440 This is a utils.io.CapturedIO object with stdout/err attributes
1442 1441 for the text of the captured output.
1443 1442
1444 1443 CapturedOutput also has a show() method for displaying the output,
1445 1444 and __call__ as well, so you can use that to quickly display the
1446 1445 output.
1447 1446
1448 1447 If unspecified, captured output is discarded.
1449 1448 """
1450 1449 )
1451 1450 @magic_arguments.argument('--no-stderr', action="store_true",
1452 1451 help="""Don't capture stderr."""
1453 1452 )
1454 1453 @magic_arguments.argument('--no-stdout', action="store_true",
1455 1454 help="""Don't capture stdout."""
1456 1455 )
1457 1456 @magic_arguments.argument('--no-display', action="store_true",
1458 1457 help="""Don't capture IPython's rich display."""
1459 1458 )
1460 1459 @cell_magic
1461 1460 def capture(self, line, cell):
1462 1461 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1463 1462 args = magic_arguments.parse_argstring(self.capture, line)
1464 1463 out = not args.no_stdout
1465 1464 err = not args.no_stderr
1466 1465 disp = not args.no_display
1467 1466 with capture_output(out, err, disp) as io:
1468 1467 self.shell.run_cell(cell)
1469 1468 if args.output:
1470 1469 self.shell.user_ns[args.output] = io
1471 1470
1472 1471 def parse_breakpoint(text, current_file):
1473 1472 '''Returns (file, line) for file:line and (current_file, line) for line'''
1474 1473 colon = text.find(':')
1475 1474 if colon == -1:
1476 1475 return current_file, int(text)
1477 1476 else:
1478 1477 return text[:colon], int(text[colon+1:])
1479 1478
1480 1479 def _format_time(timespan, precision=3):
1481 1480 """Formats the timespan in a human readable form"""
1482 1481
1483 1482 if timespan >= 60.0:
1484 1483 # we have more than a minute, format that in a human readable form
1485 1484 # Idea from http://snipplr.com/view/5713/
1486 1485 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1487 1486 time = []
1488 1487 leftover = timespan
1489 1488 for suffix, length in parts:
1490 1489 value = int(leftover / length)
1491 1490 if value > 0:
1492 1491 leftover = leftover % length
1493 1492 time.append(u'%s%s' % (str(value), suffix))
1494 1493 if leftover < 1:
1495 1494 break
1496 1495 return " ".join(time)
1497 1496
1498 1497
1499 1498 # Unfortunately the unicode 'micro' symbol can cause problems in
1500 1499 # certain terminals.
1501 1500 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1502 1501 # Try to prevent crashes by being more secure than it needs to
1503 1502 # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set.
1504 1503 units = [u"s", u"ms",u'us',"ns"] # the save value
1505 1504 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1506 1505 try:
1507 1506 u'\xb5'.encode(sys.stdout.encoding)
1508 1507 units = [u"s", u"ms",u'\xb5s',"ns"]
1509 1508 except:
1510 1509 pass
1511 1510 scaling = [1, 1e3, 1e6, 1e9]
1512 1511
1513 1512 if timespan > 0.0:
1514 1513 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1515 1514 else:
1516 1515 order = 3
1517 1516 return u"%.*g %s" % (precision, timespan * scaling[order], units[order])
@@ -1,859 +1,856 b''
1 1 """Implementation of magic functions for interaction with the OS.
2 2
3 3 Note: this module is named 'osm' instead of 'os' to avoid a collision with the
4 4 builtin.
5 5 """
6 6 # Copyright (c) IPython Development Team.
7 7 # Distributed under the terms of the Modified BSD License.
8 8
9 9 import io
10 10 import os
11 11 import re
12 12 import sys
13 13 from pprint import pformat
14 14
15 15 from IPython.core import magic_arguments
16 16 from IPython.core import oinspect
17 17 from IPython.core import page
18 18 from IPython.core.alias import AliasError, Alias
19 19 from IPython.core.error import UsageError
20 20 from IPython.core.magic import (
21 21 Magics, compress_dhist, magics_class, line_magic, cell_magic, line_cell_magic
22 22 )
23 23 from IPython.testing.skipdoctest import skip_doctest
24 24 from IPython.utils.openpy import source_to_unicode
25 25 from IPython.utils.process import abbrev_cwd
26 26 from IPython.utils.terminal import set_term_title
27 27 from traitlets import Bool
28 28 from warnings import warn
29 29
30 30
31 31 @magics_class
32 32 class OSMagics(Magics):
33 33 """Magics to interact with the underlying OS (shell-type functionality).
34 34 """
35 35
36 36 cd_force_quiet = Bool(False,
37 37 help="Force %cd magic to be quiet even if -q is not passed."
38 38 ).tag(config=True)
39 39
40 40 def __init__(self, shell=None, **kwargs):
41 41
42 42 # Now define isexec in a cross platform manner.
43 43 self.is_posix = False
44 44 self.execre = None
45 45 if os.name == 'posix':
46 46 self.is_posix = True
47 47 else:
48 48 try:
49 49 winext = os.environ['pathext'].replace(';','|').replace('.','')
50 50 except KeyError:
51 51 winext = 'exe|com|bat|py'
52 52 try:
53 53 self.execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
54 54 except re.error:
55 55 warn("Seems like your pathext environmental "
56 56 "variable is malformed. Please check it to "
57 57 "enable a proper handle of file extensions "
58 58 "managed for your system")
59 59 winext = 'exe|com|bat|py'
60 60 self.execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
61 61
62 62 # call up the chain
63 63 super().__init__(shell=shell, **kwargs)
64 64
65 65
66 @skip_doctest
67 66 def _isexec_POSIX(self, file):
68 67 """
69 68 Test for executable on a POSIX system
70 69 """
71 70 if os.access(file.path, os.X_OK):
72 71 # will fail on maxOS if access is not X_OK
73 72 return file.is_file()
74 73 return False
75 74
76 75
77 76
78 @skip_doctest
79 77 def _isexec_WIN(self, file):
80 78 """
81 79 Test for executable file on non POSIX system
82 80 """
83 81 return file.is_file() and self.execre.match(file.name) is not None
84 82
85 @skip_doctest
86 83 def isexec(self, file):
87 84 """
88 85 Test for executable file on non POSIX system
89 86 """
90 87 if self.is_posix:
91 88 return self._isexec_POSIX(file)
92 89 else:
93 90 return self._isexec_WIN(file)
94 91
95 92
96 93 @skip_doctest
97 94 @line_magic
98 95 def alias(self, parameter_s=''):
99 96 """Define an alias for a system command.
100 97
101 98 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
102 99
103 100 Then, typing 'alias_name params' will execute the system command 'cmd
104 101 params' (from your underlying operating system).
105 102
106 103 Aliases have lower precedence than magic functions and Python normal
107 104 variables, so if 'foo' is both a Python variable and an alias, the
108 105 alias can not be executed until 'del foo' removes the Python variable.
109 106
110 107 You can use the %l specifier in an alias definition to represent the
111 108 whole line when the alias is called. For example::
112 109
113 110 In [2]: alias bracket echo "Input in brackets: <%l>"
114 111 In [3]: bracket hello world
115 112 Input in brackets: <hello world>
116 113
117 114 You can also define aliases with parameters using %s specifiers (one
118 115 per parameter)::
119 116
120 117 In [1]: alias parts echo first %s second %s
121 118 In [2]: %parts A B
122 119 first A second B
123 120 In [3]: %parts A
124 121 Incorrect number of arguments: 2 expected.
125 122 parts is an alias to: 'echo first %s second %s'
126 123
127 124 Note that %l and %s are mutually exclusive. You can only use one or
128 125 the other in your aliases.
129 126
130 127 Aliases expand Python variables just like system calls using ! or !!
131 128 do: all expressions prefixed with '$' get expanded. For details of
132 129 the semantic rules, see PEP-215:
133 130 http://www.python.org/peps/pep-0215.html. This is the library used by
134 131 IPython for variable expansion. If you want to access a true shell
135 132 variable, an extra $ is necessary to prevent its expansion by
136 133 IPython::
137 134
138 135 In [6]: alias show echo
139 136 In [7]: PATH='A Python string'
140 137 In [8]: show $PATH
141 138 A Python string
142 139 In [9]: show $$PATH
143 140 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
144 141
145 142 You can use the alias facility to access all of $PATH. See the %rehashx
146 143 function, which automatically creates aliases for the contents of your
147 144 $PATH.
148 145
149 146 If called with no parameters, %alias prints the current alias table
150 147 for your system. For posix systems, the default aliases are 'cat',
151 148 'cp', 'mv', 'rm', 'rmdir', and 'mkdir', and other platform-specific
152 149 aliases are added. For windows-based systems, the default aliases are
153 150 'copy', 'ddir', 'echo', 'ls', 'ldir', 'mkdir', 'ren', and 'rmdir'.
154 151
155 152 You can see the definition of alias by adding a question mark in the
156 153 end::
157 154
158 155 In [1]: cat?
159 156 Repr: <alias cat for 'cat'>"""
160 157
161 158 par = parameter_s.strip()
162 159 if not par:
163 160 aliases = sorted(self.shell.alias_manager.aliases)
164 161 # stored = self.shell.db.get('stored_aliases', {} )
165 162 # for k, v in stored:
166 163 # atab.append(k, v[0])
167 164
168 165 print("Total number of aliases:", len(aliases))
169 166 sys.stdout.flush()
170 167 return aliases
171 168
172 169 # Now try to define a new one
173 170 try:
174 171 alias,cmd = par.split(None, 1)
175 172 except TypeError:
176 173 print(oinspect.getdoc(self.alias))
177 174 return
178 175
179 176 try:
180 177 self.shell.alias_manager.define_alias(alias, cmd)
181 178 except AliasError as e:
182 179 print(e)
183 180 # end magic_alias
184 181
185 182 @line_magic
186 183 def unalias(self, parameter_s=''):
187 184 """Remove an alias"""
188 185
189 186 aname = parameter_s.strip()
190 187 try:
191 188 self.shell.alias_manager.undefine_alias(aname)
192 189 except ValueError as e:
193 190 print(e)
194 191 return
195 192
196 193 stored = self.shell.db.get('stored_aliases', {} )
197 194 if aname in stored:
198 195 print("Removing %stored alias",aname)
199 196 del stored[aname]
200 197 self.shell.db['stored_aliases'] = stored
201 198
202 199 @line_magic
203 200 def rehashx(self, parameter_s=''):
204 201 """Update the alias table with all executable files in $PATH.
205 202
206 203 rehashx explicitly checks that every entry in $PATH is a file
207 204 with execute access (os.X_OK).
208 205
209 206 Under Windows, it checks executability as a match against a
210 207 '|'-separated string of extensions, stored in the IPython config
211 208 variable win_exec_ext. This defaults to 'exe|com|bat'.
212 209
213 210 This function also resets the root module cache of module completer,
214 211 used on slow filesystems.
215 212 """
216 213 from IPython.core.alias import InvalidAliasError
217 214
218 215 # for the benefit of module completer in ipy_completers.py
219 216 del self.shell.db['rootmodules_cache']
220 217
221 218 path = [os.path.abspath(os.path.expanduser(p)) for p in
222 219 os.environ.get('PATH','').split(os.pathsep)]
223 220
224 221 syscmdlist = []
225 222 savedir = os.getcwd()
226 223
227 224 # Now walk the paths looking for executables to alias.
228 225 try:
229 226 # write the whole loop for posix/Windows so we don't have an if in
230 227 # the innermost part
231 228 if self.is_posix:
232 229 for pdir in path:
233 230 try:
234 231 os.chdir(pdir)
235 232 except OSError:
236 233 continue
237 234
238 235 # for python 3.6+ rewrite to: with os.scandir(pdir) as dirlist:
239 236 dirlist = os.scandir(path=pdir)
240 237 for ff in dirlist:
241 238 if self.isexec(ff):
242 239 fname = ff.name
243 240 try:
244 241 # Removes dots from the name since ipython
245 242 # will assume names with dots to be python.
246 243 if not self.shell.alias_manager.is_alias(fname):
247 244 self.shell.alias_manager.define_alias(
248 245 fname.replace('.',''), fname)
249 246 except InvalidAliasError:
250 247 pass
251 248 else:
252 249 syscmdlist.append(fname)
253 250 else:
254 251 no_alias = Alias.blacklist
255 252 for pdir in path:
256 253 try:
257 254 os.chdir(pdir)
258 255 except OSError:
259 256 continue
260 257
261 258 # for python 3.6+ rewrite to: with os.scandir(pdir) as dirlist:
262 259 dirlist = os.scandir(pdir)
263 260 for ff in dirlist:
264 261 fname = ff.name
265 262 base, ext = os.path.splitext(fname)
266 263 if self.isexec(ff) and base.lower() not in no_alias:
267 264 if ext.lower() == '.exe':
268 265 fname = base
269 266 try:
270 267 # Removes dots from the name since ipython
271 268 # will assume names with dots to be python.
272 269 self.shell.alias_manager.define_alias(
273 270 base.lower().replace('.',''), fname)
274 271 except InvalidAliasError:
275 272 pass
276 273 syscmdlist.append(fname)
277 274
278 275 self.shell.db['syscmdlist'] = syscmdlist
279 276 finally:
280 277 os.chdir(savedir)
281 278
282 279 @skip_doctest
283 280 @line_magic
284 281 def pwd(self, parameter_s=''):
285 282 """Return the current working directory path.
286 283
287 284 Examples
288 285 --------
289 286 ::
290 287
291 288 In [9]: pwd
292 289 Out[9]: '/home/tsuser/sprint/ipython'
293 290 """
294 291 try:
295 292 return os.getcwd()
296 293 except FileNotFoundError as e:
297 294 raise UsageError("CWD no longer exists - please use %cd to change directory.") from e
298 295
299 296 @skip_doctest
300 297 @line_magic
301 298 def cd(self, parameter_s=''):
302 299 """Change the current working directory.
303 300
304 301 This command automatically maintains an internal list of directories
305 302 you visit during your IPython session, in the variable ``_dh``. The
306 303 command :magic:`%dhist` shows this history nicely formatted. You can
307 304 also do ``cd -<tab>`` to see directory history conveniently.
308 305 Usage:
309 306
310 307 - ``cd 'dir'``: changes to directory 'dir'.
311 308 - ``cd -``: changes to the last visited directory.
312 309 - ``cd -<n>``: changes to the n-th directory in the directory history.
313 310 - ``cd --foo``: change to directory that matches 'foo' in history
314 311 - ``cd -b <bookmark_name>``: jump to a bookmark set by %bookmark
315 312 - Hitting a tab key after ``cd -b`` allows you to tab-complete
316 313 bookmark names.
317 314
318 315 .. note::
319 316 ``cd <bookmark_name>`` is enough if there is no directory
320 317 ``<bookmark_name>``, but a bookmark with the name exists.
321 318
322 319
323 320 Options:
324 321
325 322 -q Be quiet. Do not print the working directory after the
326 323 cd command is executed. By default IPython's cd
327 324 command does print this directory, since the default
328 325 prompts do not display path information.
329 326
330 327 .. note::
331 328 Note that ``!cd`` doesn't work for this purpose because the shell
332 329 where ``!command`` runs is immediately discarded after executing
333 330 'command'.
334 331
335 332
336 333 Examples
337 334 --------
338 335 ::
339 336
340 337 In [10]: cd parent/child
341 338 /home/tsuser/parent/child
342 339 """
343 340
344 341 try:
345 342 oldcwd = os.getcwd()
346 343 except FileNotFoundError:
347 344 # Happens if the CWD has been deleted.
348 345 oldcwd = None
349 346
350 347 numcd = re.match(r'(-)(\d+)$',parameter_s)
351 348 # jump in directory history by number
352 349 if numcd:
353 350 nn = int(numcd.group(2))
354 351 try:
355 352 ps = self.shell.user_ns['_dh'][nn]
356 353 except IndexError:
357 354 print('The requested directory does not exist in history.')
358 355 return
359 356 else:
360 357 opts = {}
361 358 elif parameter_s.startswith('--'):
362 359 ps = None
363 360 fallback = None
364 361 pat = parameter_s[2:]
365 362 dh = self.shell.user_ns['_dh']
366 363 # first search only by basename (last component)
367 364 for ent in reversed(dh):
368 365 if pat in os.path.basename(ent) and os.path.isdir(ent):
369 366 ps = ent
370 367 break
371 368
372 369 if fallback is None and pat in ent and os.path.isdir(ent):
373 370 fallback = ent
374 371
375 372 # if we have no last part match, pick the first full path match
376 373 if ps is None:
377 374 ps = fallback
378 375
379 376 if ps is None:
380 377 print("No matching entry in directory history")
381 378 return
382 379 else:
383 380 opts = {}
384 381
385 382
386 383 else:
387 384 opts, ps = self.parse_options(parameter_s, 'qb', mode='string')
388 385 # jump to previous
389 386 if ps == '-':
390 387 try:
391 388 ps = self.shell.user_ns['_dh'][-2]
392 389 except IndexError as e:
393 390 raise UsageError('%cd -: No previous directory to change to.') from e
394 391 # jump to bookmark if needed
395 392 else:
396 393 if not os.path.isdir(ps) or 'b' in opts:
397 394 bkms = self.shell.db.get('bookmarks', {})
398 395
399 396 if ps in bkms:
400 397 target = bkms[ps]
401 398 print('(bookmark:%s) -> %s' % (ps, target))
402 399 ps = target
403 400 else:
404 401 if 'b' in opts:
405 402 raise UsageError("Bookmark '%s' not found. "
406 403 "Use '%%bookmark -l' to see your bookmarks." % ps)
407 404
408 405 # at this point ps should point to the target dir
409 406 if ps:
410 407 try:
411 408 os.chdir(os.path.expanduser(ps))
412 409 if hasattr(self.shell, 'term_title') and self.shell.term_title:
413 410 set_term_title(self.shell.term_title_format.format(cwd=abbrev_cwd()))
414 411 except OSError:
415 412 print(sys.exc_info()[1])
416 413 else:
417 414 cwd = os.getcwd()
418 415 dhist = self.shell.user_ns['_dh']
419 416 if oldcwd != cwd:
420 417 dhist.append(cwd)
421 418 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
422 419
423 420 else:
424 421 os.chdir(self.shell.home_dir)
425 422 if hasattr(self.shell, 'term_title') and self.shell.term_title:
426 423 set_term_title(self.shell.term_title_format.format(cwd="~"))
427 424 cwd = os.getcwd()
428 425 dhist = self.shell.user_ns['_dh']
429 426
430 427 if oldcwd != cwd:
431 428 dhist.append(cwd)
432 429 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
433 430 if not 'q' in opts and not self.cd_force_quiet and self.shell.user_ns['_dh']:
434 431 print(self.shell.user_ns['_dh'][-1])
435 432
436 433 @line_magic
437 434 def env(self, parameter_s=''):
438 435 """Get, set, or list environment variables.
439 436
440 437 Usage:\\
441 438
442 439 :``%env``: lists all environment variables/values
443 440 :``%env var``: get value for var
444 441 :``%env var val``: set value for var
445 442 :``%env var=val``: set value for var
446 443 :``%env var=$val``: set value for var, using python expansion if possible
447 444 """
448 445 if parameter_s.strip():
449 446 split = '=' if '=' in parameter_s else ' '
450 447 bits = parameter_s.split(split)
451 448 if len(bits) == 1:
452 449 key = parameter_s.strip()
453 450 if key in os.environ:
454 451 return os.environ[key]
455 452 else:
456 453 err = "Environment does not have key: {0}".format(key)
457 454 raise UsageError(err)
458 455 if len(bits) > 1:
459 456 return self.set_env(parameter_s)
460 457 env = dict(os.environ)
461 458 # hide likely secrets when printing the whole environment
462 459 for key in list(env):
463 460 if any(s in key.lower() for s in ('key', 'token', 'secret')):
464 461 env[key] = '<hidden>'
465 462
466 463 return env
467 464
468 465 @line_magic
469 466 def set_env(self, parameter_s):
470 467 """Set environment variables. Assumptions are that either "val" is a
471 468 name in the user namespace, or val is something that evaluates to a
472 469 string.
473 470
474 471 Usage:\\
475 472 %set_env var val: set value for var
476 473 %set_env var=val: set value for var
477 474 %set_env var=$val: set value for var, using python expansion if possible
478 475 """
479 476 split = '=' if '=' in parameter_s else ' '
480 477 bits = parameter_s.split(split, 1)
481 478 if not parameter_s.strip() or len(bits)<2:
482 479 raise UsageError("usage is 'set_env var=val'")
483 480 var = bits[0].strip()
484 481 val = bits[1].strip()
485 482 if re.match(r'.*\s.*', var):
486 483 # an environment variable with whitespace is almost certainly
487 484 # not what the user intended. what's more likely is the wrong
488 485 # split was chosen, ie for "set_env cmd_args A=B", we chose
489 486 # '=' for the split and should have chosen ' '. to get around
490 487 # this, users should just assign directly to os.environ or use
491 488 # standard magic {var} expansion.
492 489 err = "refusing to set env var with whitespace: '{0}'"
493 490 err = err.format(val)
494 491 raise UsageError(err)
495 492 os.environ[var] = val
496 493 print('env: {0}={1}'.format(var,val))
497 494
498 495 @line_magic
499 496 def pushd(self, parameter_s=''):
500 497 """Place the current dir on stack and change directory.
501 498
502 499 Usage:\\
503 500 %pushd ['dirname']
504 501 """
505 502
506 503 dir_s = self.shell.dir_stack
507 504 tgt = os.path.expanduser(parameter_s)
508 505 cwd = os.getcwd().replace(self.shell.home_dir,'~')
509 506 if tgt:
510 507 self.cd(parameter_s)
511 508 dir_s.insert(0,cwd)
512 509 return self.shell.run_line_magic('dirs', '')
513 510
514 511 @line_magic
515 512 def popd(self, parameter_s=''):
516 513 """Change to directory popped off the top of the stack.
517 514 """
518 515 if not self.shell.dir_stack:
519 516 raise UsageError("%popd on empty stack")
520 517 top = self.shell.dir_stack.pop(0)
521 518 self.cd(top)
522 519 print("popd ->",top)
523 520
524 521 @line_magic
525 522 def dirs(self, parameter_s=''):
526 523 """Return the current directory stack."""
527 524
528 525 return self.shell.dir_stack
529 526
530 527 @line_magic
531 528 def dhist(self, parameter_s=''):
532 529 """Print your history of visited directories.
533 530
534 531 %dhist -> print full history\\
535 532 %dhist n -> print last n entries only\\
536 533 %dhist n1 n2 -> print entries between n1 and n2 (n2 not included)\\
537 534
538 535 This history is automatically maintained by the %cd command, and
539 536 always available as the global list variable _dh. You can use %cd -<n>
540 537 to go to directory number <n>.
541 538
542 539 Note that most of time, you should view directory history by entering
543 540 cd -<TAB>.
544 541
545 542 """
546 543
547 544 dh = self.shell.user_ns['_dh']
548 545 if parameter_s:
549 546 try:
550 547 args = map(int,parameter_s.split())
551 548 except:
552 549 self.arg_err(self.dhist)
553 550 return
554 551 if len(args) == 1:
555 552 ini,fin = max(len(dh)-(args[0]),0),len(dh)
556 553 elif len(args) == 2:
557 554 ini,fin = args
558 555 fin = min(fin, len(dh))
559 556 else:
560 557 self.arg_err(self.dhist)
561 558 return
562 559 else:
563 560 ini,fin = 0,len(dh)
564 561 print('Directory history (kept in _dh)')
565 562 for i in range(ini, fin):
566 563 print("%d: %s" % (i, dh[i]))
567 564
568 565 @skip_doctest
569 566 @line_magic
570 567 def sc(self, parameter_s=''):
571 568 """Shell capture - run shell command and capture output (DEPRECATED use !).
572 569
573 570 DEPRECATED. Suboptimal, retained for backwards compatibility.
574 571
575 572 You should use the form 'var = !command' instead. Example:
576 573
577 574 "%sc -l myfiles = ls ~" should now be written as
578 575
579 576 "myfiles = !ls ~"
580 577
581 578 myfiles.s, myfiles.l and myfiles.n still apply as documented
582 579 below.
583 580
584 581 --
585 582 %sc [options] varname=command
586 583
587 584 IPython will run the given command using commands.getoutput(), and
588 585 will then update the user's interactive namespace with a variable
589 586 called varname, containing the value of the call. Your command can
590 587 contain shell wildcards, pipes, etc.
591 588
592 589 The '=' sign in the syntax is mandatory, and the variable name you
593 590 supply must follow Python's standard conventions for valid names.
594 591
595 592 (A special format without variable name exists for internal use)
596 593
597 594 Options:
598 595
599 596 -l: list output. Split the output on newlines into a list before
600 597 assigning it to the given variable. By default the output is stored
601 598 as a single string.
602 599
603 600 -v: verbose. Print the contents of the variable.
604 601
605 602 In most cases you should not need to split as a list, because the
606 603 returned value is a special type of string which can automatically
607 604 provide its contents either as a list (split on newlines) or as a
608 605 space-separated string. These are convenient, respectively, either
609 606 for sequential processing or to be passed to a shell command.
610 607
611 608 For example::
612 609
613 610 # Capture into variable a
614 611 In [1]: sc a=ls *py
615 612
616 613 # a is a string with embedded newlines
617 614 In [2]: a
618 615 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
619 616
620 617 # which can be seen as a list:
621 618 In [3]: a.l
622 619 Out[3]: ['setup.py', 'win32_manual_post_install.py']
623 620
624 621 # or as a whitespace-separated string:
625 622 In [4]: a.s
626 623 Out[4]: 'setup.py win32_manual_post_install.py'
627 624
628 625 # a.s is useful to pass as a single command line:
629 626 In [5]: !wc -l $a.s
630 627 146 setup.py
631 628 130 win32_manual_post_install.py
632 629 276 total
633 630
634 631 # while the list form is useful to loop over:
635 632 In [6]: for f in a.l:
636 633 ...: !wc -l $f
637 634 ...:
638 635 146 setup.py
639 636 130 win32_manual_post_install.py
640 637
641 638 Similarly, the lists returned by the -l option are also special, in
642 639 the sense that you can equally invoke the .s attribute on them to
643 640 automatically get a whitespace-separated string from their contents::
644 641
645 642 In [7]: sc -l b=ls *py
646 643
647 644 In [8]: b
648 645 Out[8]: ['setup.py', 'win32_manual_post_install.py']
649 646
650 647 In [9]: b.s
651 648 Out[9]: 'setup.py win32_manual_post_install.py'
652 649
653 650 In summary, both the lists and strings used for output capture have
654 651 the following special attributes::
655 652
656 653 .l (or .list) : value as list.
657 654 .n (or .nlstr): value as newline-separated string.
658 655 .s (or .spstr): value as space-separated string.
659 656 """
660 657
661 658 opts,args = self.parse_options(parameter_s, 'lv')
662 659 # Try to get a variable name and command to run
663 660 try:
664 661 # the variable name must be obtained from the parse_options
665 662 # output, which uses shlex.split to strip options out.
666 663 var,_ = args.split('=', 1)
667 664 var = var.strip()
668 665 # But the command has to be extracted from the original input
669 666 # parameter_s, not on what parse_options returns, to avoid the
670 667 # quote stripping which shlex.split performs on it.
671 668 _,cmd = parameter_s.split('=', 1)
672 669 except ValueError:
673 670 var,cmd = '',''
674 671 # If all looks ok, proceed
675 672 split = 'l' in opts
676 673 out = self.shell.getoutput(cmd, split=split)
677 674 if 'v' in opts:
678 675 print('%s ==\n%s' % (var, pformat(out)))
679 676 if var:
680 677 self.shell.user_ns.update({var:out})
681 678 else:
682 679 return out
683 680
684 681 @line_cell_magic
685 682 def sx(self, line='', cell=None):
686 683 """Shell execute - run shell command and capture output (!! is short-hand).
687 684
688 685 %sx command
689 686
690 687 IPython will run the given command using commands.getoutput(), and
691 688 return the result formatted as a list (split on '\\n'). Since the
692 689 output is _returned_, it will be stored in ipython's regular output
693 690 cache Out[N] and in the '_N' automatic variables.
694 691
695 692 Notes:
696 693
697 694 1) If an input line begins with '!!', then %sx is automatically
698 695 invoked. That is, while::
699 696
700 697 !ls
701 698
702 699 causes ipython to simply issue system('ls'), typing::
703 700
704 701 !!ls
705 702
706 703 is a shorthand equivalent to::
707 704
708 705 %sx ls
709 706
710 707 2) %sx differs from %sc in that %sx automatically splits into a list,
711 708 like '%sc -l'. The reason for this is to make it as easy as possible
712 709 to process line-oriented shell output via further python commands.
713 710 %sc is meant to provide much finer control, but requires more
714 711 typing.
715 712
716 713 3) Just like %sc -l, this is a list with special attributes:
717 714 ::
718 715
719 716 .l (or .list) : value as list.
720 717 .n (or .nlstr): value as newline-separated string.
721 718 .s (or .spstr): value as whitespace-separated string.
722 719
723 720 This is very useful when trying to use such lists as arguments to
724 721 system commands."""
725 722
726 723 if cell is None:
727 724 # line magic
728 725 return self.shell.getoutput(line)
729 726 else:
730 727 opts,args = self.parse_options(line, '', 'out=')
731 728 output = self.shell.getoutput(cell)
732 729 out_name = opts.get('out', opts.get('o'))
733 730 if out_name:
734 731 self.shell.user_ns[out_name] = output
735 732 else:
736 733 return output
737 734
738 735 system = line_cell_magic('system')(sx)
739 736 bang = cell_magic('!')(sx)
740 737
741 738 @line_magic
742 739 def bookmark(self, parameter_s=''):
743 740 """Manage IPython's bookmark system.
744 741
745 742 %bookmark <name> - set bookmark to current dir
746 743 %bookmark <name> <dir> - set bookmark to <dir>
747 744 %bookmark -l - list all bookmarks
748 745 %bookmark -d <name> - remove bookmark
749 746 %bookmark -r - remove all bookmarks
750 747
751 748 You can later on access a bookmarked folder with::
752 749
753 750 %cd -b <name>
754 751
755 752 or simply '%cd <name>' if there is no directory called <name> AND
756 753 there is such a bookmark defined.
757 754
758 755 Your bookmarks persist through IPython sessions, but they are
759 756 associated with each profile."""
760 757
761 758 opts,args = self.parse_options(parameter_s,'drl',mode='list')
762 759 if len(args) > 2:
763 760 raise UsageError("%bookmark: too many arguments")
764 761
765 762 bkms = self.shell.db.get('bookmarks',{})
766 763
767 764 if 'd' in opts:
768 765 try:
769 766 todel = args[0]
770 767 except IndexError as e:
771 768 raise UsageError(
772 769 "%bookmark -d: must provide a bookmark to delete") from e
773 770 else:
774 771 try:
775 772 del bkms[todel]
776 773 except KeyError as e:
777 774 raise UsageError(
778 775 "%%bookmark -d: Can't delete bookmark '%s'" % todel) from e
779 776
780 777 elif 'r' in opts:
781 778 bkms = {}
782 779 elif 'l' in opts:
783 780 bks = sorted(bkms)
784 781 if bks:
785 782 size = max(map(len, bks))
786 783 else:
787 784 size = 0
788 785 fmt = '%-'+str(size)+'s -> %s'
789 786 print('Current bookmarks:')
790 787 for bk in bks:
791 788 print(fmt % (bk, bkms[bk]))
792 789 else:
793 790 if not args:
794 791 raise UsageError("%bookmark: You must specify the bookmark name")
795 792 elif len(args)==1:
796 793 bkms[args[0]] = os.getcwd()
797 794 elif len(args)==2:
798 795 bkms[args[0]] = args[1]
799 796 self.shell.db['bookmarks'] = bkms
800 797
801 798 @line_magic
802 799 def pycat(self, parameter_s=''):
803 800 """Show a syntax-highlighted file through a pager.
804 801
805 802 This magic is similar to the cat utility, but it will assume the file
806 803 to be Python source and will show it with syntax highlighting.
807 804
808 805 This magic command can either take a local filename, an url,
809 806 an history range (see %history) or a macro as argument.
810 807
811 808 If no parameter is given, prints out history of current session up to
812 809 this point. ::
813 810
814 811 %pycat myscript.py
815 812 %pycat 7-27
816 813 %pycat myMacro
817 814 %pycat http://www.example.com/myscript.py
818 815 """
819 816 try:
820 817 cont = self.shell.find_user_code(parameter_s, skip_encoding_cookie=False)
821 818 except (ValueError, IOError):
822 819 print("Error: no such file, variable, URL, history range or macro")
823 820 return
824 821
825 822 page.page(self.shell.pycolorize(source_to_unicode(cont)))
826 823
827 824 @magic_arguments.magic_arguments()
828 825 @magic_arguments.argument(
829 826 '-a', '--append', action='store_true', default=False,
830 827 help='Append contents of the cell to an existing file. '
831 828 'The file will be created if it does not exist.'
832 829 )
833 830 @magic_arguments.argument(
834 831 'filename', type=str,
835 832 help='file to write'
836 833 )
837 834 @cell_magic
838 835 def writefile(self, line, cell):
839 836 """Write the contents of the cell to a file.
840 837
841 838 The file will be overwritten unless the -a (--append) flag is specified.
842 839 """
843 840 args = magic_arguments.parse_argstring(self.writefile, line)
844 841 if re.match(r'^(\'.*\')|(".*")$', args.filename):
845 842 filename = os.path.expanduser(args.filename[1:-1])
846 843 else:
847 844 filename = os.path.expanduser(args.filename)
848 845
849 846 if os.path.exists(filename):
850 847 if args.append:
851 848 print("Appending to %s" % filename)
852 849 else:
853 850 print("Overwriting %s" % filename)
854 851 else:
855 852 print("Writing %s" % filename)
856 853
857 854 mode = 'a' if args.append else 'w'
858 855 with io.open(filename, mode, encoding='utf-8') as f:
859 856 f.write(cell)
General Comments 0
You need to be logged in to leave comments. Login now