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