##// 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 @no_var_expand
423 @no_var_expand
424 @line_cell_magic
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 """Activate the interactive debugger.
427 """Activate the interactive debugger.
427
428
428 This magic command support two ways of activating debugger.
429 This magic command support two ways of activating debugger.
@@ -453,7 +454,7 b' class ExecutionMagics(Magics):'
453 self._debug_post_mortem()
454 self._debug_post_mortem()
454 elif not (args.breakpoint or cell):
455 elif not (args.breakpoint or cell):
455 # If there is no breakpoints, the line is just code to execute
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 else:
458 else:
458 # Here we try to reconstruct the code from the output of
459 # Here we try to reconstruct the code from the output of
459 # parse_argstring. This might not work if the code has spaces
460 # parse_argstring. This might not work if the code has spaces
@@ -461,18 +462,20 b' class ExecutionMagics(Magics):'
461 code = "\n".join(args.statement)
462 code = "\n".join(args.statement)
462 if cell:
463 if cell:
463 code += "\n" + cell
464 code += "\n" + cell
464 self._debug_exec(code, args.breakpoint)
465 self._debug_exec(code, args.breakpoint, local_ns)
465
466
466 def _debug_post_mortem(self):
467 def _debug_post_mortem(self):
467 self.shell.debugger(force=True)
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 if breakpoint:
471 if breakpoint:
471 (filename, bp_line) = breakpoint.rsplit(':', 1)
472 (filename, bp_line) = breakpoint.rsplit(':', 1)
472 bp_line = int(bp_line)
473 bp_line = int(bp_line)
473 else:
474 else:
474 (filename, bp_line) = (None, None)
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 @line_magic
480 @line_magic
478 def tb(self, s):
481 def tb(self, s):
@@ -867,8 +870,9 b' class ExecutionMagics(Magics):'
867
870
868 return stats
871 return stats
869
872
870 def _run_with_debugger(self, code, code_ns, filename=None,
873 def _run_with_debugger(
871 bp_line=None, bp_file=None):
874 self, code, code_ns, filename=None, bp_line=None, bp_file=None, local_ns=None
875 ):
872 """
876 """
873 Run `code` in debugger with a break point.
877 Run `code` in debugger with a break point.
874
878
@@ -885,6 +889,8 b' class ExecutionMagics(Magics):'
885 bp_file : str, optional
889 bp_file : str, optional
886 Path to the file in which break point is specified.
890 Path to the file in which break point is specified.
887 `filename` is used if not given.
891 `filename` is used if not given.
892 local_ns : dict, optional
893 A local namespace in which `code` is executed.
888
894
889 Raises
895 Raises
890 ------
896 ------
@@ -941,7 +947,7 b' class ExecutionMagics(Magics):'
941 while True:
947 while True:
942 try:
948 try:
943 trace = sys.gettrace()
949 trace = sys.gettrace()
944 deb.run(code, code_ns)
950 deb.run(code, code_ns, local_ns)
945 except Restart:
951 except Restart:
946 print("Restarting")
952 print("Restarting")
947 if filename:
953 if filename:
@@ -715,6 +715,7 b' def doctest_precision():'
715 Out[5]: '3.141593e+00'
715 Out[5]: '3.141593e+00'
716 """
716 """
717
717
718
718 def test_debug_magic():
719 def test_debug_magic():
719 """Test debugging a small code with %debug
720 """Test debugging a small code with %debug
720
721
@@ -727,6 +728,22 b' def test_debug_magic():'
727 In [2]:
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 def test_psearch():
747 def test_psearch():
731 with tt.AssertPrints("dict.fromkeys"):
748 with tt.AssertPrints("dict.fromkeys"):
732 _ip.run_cell("dict.fr*?")
749 _ip.run_cell("dict.fr*?")
General Comments 0
You need to be logged in to leave comments. Login now