##// END OF EJS Templates
PR: Add needs_local_scope to debug magic (#13960)...
Matthias Bussonnier -
r28162:83f90a3f merge
parent child Browse files
Show More
@@ -422,7 +422,8 b' class ExecutionMagics(Magics):'
422 422 )
423 423 @no_var_expand
424 424 @line_cell_magic
425 def debug(self, line='', cell=None):
425 @needs_local_scope
426 def debug(self, line="", cell=None, local_ns=None):
426 427 """Activate the interactive debugger.
427 428
428 429 This magic command support two ways of activating debugger.
@@ -453,7 +454,7 b' class ExecutionMagics(Magics):'
453 454 self._debug_post_mortem()
454 455 elif not (args.breakpoint or cell):
455 456 # If there is no breakpoints, the line is just code to execute
456 self._debug_exec(line, None)
457 self._debug_exec(line, None, local_ns)
457 458 else:
458 459 # Here we try to reconstruct the code from the output of
459 460 # parse_argstring. This might not work if the code has spaces
@@ -461,18 +462,20 b' class ExecutionMagics(Magics):'
461 462 code = "\n".join(args.statement)
462 463 if cell:
463 464 code += "\n" + cell
464 self._debug_exec(code, args.breakpoint)
465 self._debug_exec(code, args.breakpoint, local_ns)
465 466
466 467 def _debug_post_mortem(self):
467 468 self.shell.debugger(force=True)
468 469
469 def _debug_exec(self, code, breakpoint):
470 def _debug_exec(self, code, breakpoint, local_ns=None):
470 471 if breakpoint:
471 472 (filename, bp_line) = breakpoint.rsplit(':', 1)
472 473 bp_line = int(bp_line)
473 474 else:
474 475 (filename, bp_line) = (None, None)
475 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
476 self._run_with_debugger(
477 code, self.shell.user_ns, filename, bp_line, local_ns=local_ns
478 )
476 479
477 480 @line_magic
478 481 def tb(self, s):
@@ -867,8 +870,9 b' class ExecutionMagics(Magics):'
867 870
868 871 return stats
869 872
870 def _run_with_debugger(self, code, code_ns, filename=None,
871 bp_line=None, bp_file=None):
873 def _run_with_debugger(
874 self, code, code_ns, filename=None, bp_line=None, bp_file=None, local_ns=None
875 ):
872 876 """
873 877 Run `code` in debugger with a break point.
874 878
@@ -885,6 +889,8 b' class ExecutionMagics(Magics):'
885 889 bp_file : str, optional
886 890 Path to the file in which break point is specified.
887 891 `filename` is used if not given.
892 local_ns : dict, optional
893 A local namespace in which `code` is executed.
888 894
889 895 Raises
890 896 ------
@@ -941,7 +947,7 b' class ExecutionMagics(Magics):'
941 947 while True:
942 948 try:
943 949 trace = sys.gettrace()
944 deb.run(code, code_ns)
950 deb.run(code, code_ns, local_ns)
945 951 except Restart:
946 952 print("Restarting")
947 953 if filename:
@@ -715,6 +715,7 b' def doctest_precision():'
715 715 Out[5]: '3.141593e+00'
716 716 """
717 717
718
718 719 def test_debug_magic():
719 720 """Test debugging a small code with %debug
720 721
@@ -727,6 +728,22 b' def test_debug_magic():'
727 728 In [2]:
728 729 """
729 730
731
732 def test_debug_magic_locals():
733 """Test debugging a small code with %debug with locals
734
735 In [1]: with PdbTestInput(['c']):
736 ...: def fun():
737 ...: res = 1
738 ...: %debug print(res)
739 ...: fun()
740 ...:
741 ...
742 ipdb> c
743 1
744 In [2]:
745 """
746
730 747 def test_psearch():
731 748 with tt.AssertPrints("dict.fromkeys"):
732 749 _ip.run_cell("dict.fr*?")
General Comments 0
You need to be logged in to leave comments. Login now