Skip to content

Commit

Permalink
Merge branch 'magics_runfile' into improve_namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Peter committed Mar 31, 2023
2 parents ee83722 + 9ab8aca commit 0fe4899
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
24 changes: 3 additions & 21 deletions spyder_kernels/customize/code_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,33 +299,15 @@ def _debugger_exec(self, filename, continue_if_has_breakpoints):
return

session = self.shell.pdb_session
sys.settrace(None)

# Create child debugger
debugger = SpyderPdb(
completekey=session.completekey,
stdin=session.stdin, stdout=session.stdout)
debugger.use_rawinput = session.use_rawinput
debugger.prompt = "(%s) " % session.prompt.strip()
debugger.set_remote_filename(filename)
debugger.continue_if_has_breakpoints = continue_if_has_breakpoints
with session.recursive_debugger() as debugger:
debugger.set_remote_filename(filename)
debugger.continue_if_has_breakpoints = continue_if_has_breakpoints

try:
def debug_exec(code, glob, loc):
return sys.call_tracing(debugger.run, (code, glob, loc))

# Enter recursive debugger
yield debug_exec
finally:
# Reset parent debugger
sys.settrace(session.trace_dispatch)
session.lastcmd = debugger.lastcmd

# Reset _previous_step so that get_pdb_state() notifies Spyder about
# a changed debugger position. The reset is required because the
# recursive debugger might change the position, but the parent
# debugger (self) is not aware of this.
session._previous_step = None

@contextmanager
def _profile_exec(self):
Expand Down
42 changes: 37 additions & 5 deletions spyder_kernels/customize/spyderpdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ast
import bdb
import builtins
from contextlib import contextmanager
import logging
import os
import sys
Expand Down Expand Up @@ -603,12 +604,43 @@ def do_debug(self, arg):
argument (which is an arbitrary expression or statement to be
executed in the current environment).
"""
with self.recursive_debugger() as debugger:
self.message("ENTERING RECURSIVE DEBUGGER")
try:
globals = self.curframe.f_globals
locals = self.curframe_locals
return sys.call_tracing(debugger.run, (arg, globals, locals))
except Exception:
exc_info = sys.exc_info()[:2]
self.error(
traceback.format_exception_only(*exc_info)[-1].strip())
finally:
self.message("LEAVING RECURSIVE DEBUGGER")

@contextmanager
def recursive_debugger(self):
"""Get a recursive debugger."""
# Save and restore tracing function
trace_function = sys.gettrace()
sys.settrace(None)

# Create child debugger
debugger = self.__class__(
completekey=self.completekey,
stdin=self.stdin, stdout=self.stdout)
debugger.prompt = "(%s) " % self.prompt.strip()
try:
super(SpyderPdb, self).do_debug(arg)
except Exception:
exc_info = sys.exc_info()[:2]
self.error(
traceback.format_exception_only(*exc_info)[-1].strip())
yield debugger
finally:
# Reset parent debugger
sys.settrace(trace_function)
self.lastcmd = debugger.lastcmd

# Reset _previous_step so that get_pdb_state() notifies Spyder about
# a changed debugger position. The reset is required because the
# recursive debugger might change the position, but the parent
# debugger (self) is not aware of this.
self._previous_step = None

def user_return(self, frame, return_value):
"""This function is called when a return trap is set here."""
Expand Down

0 comments on commit 0fe4899

Please sign in to comment.