Activate the interactive debugger. This magic command support two ways of activating debugger. One is to activate debugger before executing code. This way, you can set a break point, to step through the code from the point. You can use this mode by giving statements
(self, line="", cell=None, local_ns=None)
| 453 | @line_cell_magic |
| 454 | @needs_local_scope |
| 455 | def debug(self, line="", cell=None, local_ns=None): |
| 456 | """Activate the interactive debugger. |
| 457 | |
| 458 | This magic command support two ways of activating debugger. |
| 459 | One is to activate debugger before executing code. This way, you |
| 460 | can set a break point, to step through the code from the point. |
| 461 | You can use this mode by giving statements to execute and optionally |
| 462 | a breakpoint. |
| 463 | |
| 464 | The other one is to activate debugger in post-mortem mode. You can |
| 465 | activate this mode simply running %debug without any argument. |
| 466 | If an exception has just occurred, this lets you inspect its stack |
| 467 | frames interactively. Note that this will always work only on the last |
| 468 | traceback that occurred, so you must call this quickly after an |
| 469 | exception that you wish to inspect has fired, because if another one |
| 470 | occurs, it clobbers the previous one. |
| 471 | |
| 472 | If you want IPython to automatically do this on every exception, see |
| 473 | the %pdb magic for more details. |
| 474 | |
| 475 | .. versionchanged:: 7.3 |
| 476 | When running code, user variables are no longer expanded, |
| 477 | the magic line is always left unmodified. |
| 478 | |
| 479 | """ |
| 480 | args, extra = magic_arguments.parse_argstring(self.debug, line, partial=True) |
| 481 | |
| 482 | if not (args.breakpoint or extra or cell): |
| 483 | self._debug_post_mortem() |
| 484 | elif not (args.breakpoint or cell): |
| 485 | # If there is no breakpoints, the line is just code to execute |
| 486 | self._debug_exec(line, None, local_ns) |
| 487 | else: |
| 488 | # Here we try to reconstruct the code from the output of |
| 489 | # parse_argstring. This might not work if the code has spaces |
| 490 | # For example this fails for `print("a b")` |
| 491 | code = " ".join(extra) |
| 492 | if cell: |
| 493 | code += "\n" + cell |
| 494 | self._debug_exec(code, args.breakpoint, local_ns) |
| 495 | |
| 496 | def _debug_post_mortem(self): |
| 497 | self.shell.debugger(force=True) |
no test coverage detected