Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Move debug functions from IPython plugin to debugger plugin #19265

Merged
merged 8 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions spyder/plugins/debugger/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

"""Debugger Plugin."""

# Standard library imports
import os.path as osp

# Third-party imports
from qtpy.QtCore import Slot

Expand Down Expand Up @@ -63,6 +66,9 @@ def on_initialize(self):
self._set_or_edit_conditional_breakpoint)
widget.sig_clear_all_breakpoints.connect(self.clear_all_breakpoints)

widget.sig_load_pdb_file.connect(
self._load_pdb_file_in_editor)

@on_plugin_available(plugin=Plugins.Preferences)
def on_preferences_available(self):
preferences = self.get_plugin(Plugins.Preferences)
Expand All @@ -79,7 +85,7 @@ def on_editor_available(self):
widget = self.get_widget()

# The editor is available, connect signals.
widget.edit_goto.connect(editor.load)
widget.sig_edit_goto.connect(editor.load)
editor.sig_codeeditor_created.connect(self._add_codeeditor)
editor.sig_codeeditor_changed.connect(self._update_codeeditor)
editor.sig_codeeditor_deleted.connect(self._remove_codeeditor)
Expand Down Expand Up @@ -112,7 +118,7 @@ def on_editor_teardown(self):
editor = self.get_plugin(Plugins.Editor)
widget = self.get_widget()

widget.edit_goto.disconnect(editor.load)
widget.sig_edit_goto.disconnect(editor.load)

editor.sig_codeeditor_created.disconnect(self._add_codeeditor)
editor.sig_codeeditor_changed.disconnect(self._update_codeeditor)
Expand Down Expand Up @@ -192,6 +198,15 @@ def on_main_menu_teardown(self):

# ---- Private API
# ------------------------------------------------------------------------
def _load_pdb_file_in_editor(self, fname, lineno):
"""Load file using processevents."""
editor = self.get_plugin(Plugins.Editor)
if editor is None:
return
# Prevent keyboard input from accidentally entering the
impact27 marked this conversation as resolved.
Show resolved Hide resolved
# editor during repeated, rapid entry of debugging commands.
editor.load(fname, lineno, processevents=False)

def _show_namespace_in_variable_explorer(self, namespace, shellwidget):
"""
Find the right variable explorer widget and show the namespace.
Expand Down Expand Up @@ -272,24 +287,23 @@ def _update_codeeditor(self, codeeditor):
):
return
# Update debugging state
impact27 marked this conversation as resolved.
Show resolved Hide resolved
ipyconsole = self.get_plugin(Plugins.IPythonConsole)
if ipyconsole is None:
return
pdb_state = ipyconsole.get_pdb_state()
pdb_last_step = ipyconsole.get_pdb_last_step()
widget = self.get_widget()
pdb_state = widget.get_pdb_state()
filename, lineno = widget.get_pdb_last_step()
codeeditor.breakpoints_manager.update_pdb_state(
pdb_state, pdb_last_step)
pdb_state, filename, lineno)

@Slot(bool, dict)
def _update_current_codeeditor_pdb_state(self, pdb_state, pdb_last_step):
@Slot(bool, str, int)
def _update_current_codeeditor_pdb_state(
self, pdb_state, filename, line_number):
"""
The pdb state has changed.
"""
codeeditor = self._get_current_editor()
if codeeditor is None or codeeditor.breakpoints_manager is None:
return
codeeditor.breakpoints_manager.update_pdb_state(
pdb_state, pdb_last_step)
pdb_state, filename, line_number)

def _get_current_editor(self):
"""
Expand Down Expand Up @@ -382,3 +396,25 @@ def clear_breakpoint(self, filename, lineno):
return None

codeeditor.breakpoints_manager.toogle_breakpoint(lineno)

def can_close_file(self, filename=None):
"""
Check if a file can be closed taking into account debugging state.
"""
if not self.get_conf('pdb_prevent_closing'):
return True
widget = self.get_widget()
impact27 marked this conversation as resolved.
Show resolved Hide resolved

debugging = widget.get_pdb_state()
if not debugging:
return True

pdb_fname, _ = widget.get_pdb_last_step()
impact27 marked this conversation as resolved.
Show resolved Hide resolved

if pdb_fname and filename:
if osp.normcase(pdb_fname) == osp.normcase(filename):
widget.print_debug_file_msg()
return False
return True
widget.print_debug_file_msg()
impact27 marked this conversation as resolved.
Show resolved Hide resolved
return False
22 changes: 10 additions & 12 deletions spyder/plugins/debugger/utils/breakpointsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,21 @@ def __init__(self, editor):
# Update breakpoints if the number of lines in the file changes
editor.blockCountChanged.connect(self.breakpoints_changed)

def update_pdb_state(self, state, last_step):
def update_pdb_state(self, state, filename, line_number):
"""
Enable/disable debugging actions and handle pdb state change.

Update debugger panel state.
"""
if state and "fname" in last_step:
fname = last_step["fname"]
line = last_step["lineno"]
if (
fname
and self.filename
and osp.normcase(fname) == osp.normcase(self.filename)
):
self.debugger_panel.start_clean()
self.debugger_panel.set_current_line_arrow(line)
return
if (
state
and filename
and self.filename
and osp.normcase(filename) == osp.normcase(self.filename)
):
self.debugger_panel.start_clean()
self.debugger_panel.set_current_line_arrow(line_number)
return
impact27 marked this conversation as resolved.
Show resolved Hide resolved
self.debugger_panel.stop_clean()

def set_filename(self, filename):
Expand Down
59 changes: 55 additions & 4 deletions spyder/plugins/debugger/widgets/framesbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,46 @@ class FramesBrowser(QWidget, SpyderWidgetMixin):
CONF_SECTION = 'debugger'

# Signals
edit_goto = Signal((str, int, str), (str, int, str, bool))
sig_show_namespace = Signal(dict)
sig_edit_goto = Signal(str, int, str)
"""
This signal will request to open a file in a given row and column
using a code editor.

Parameters
----------
path: str
Path to file.
row: int
Cursor starting row position.
word: str
Word to select on given row.
"""
sig_show_namespace = Signal(dict, object)
impact27 marked this conversation as resolved.
Show resolved Hide resolved
"""
Show the namespace

Parameters
----------
namespace: dict
A namespace view created by spyder_kernels
shellwidget: ShellWidget
The shellwidget the request originated from
"""
sig_update_actions_requested = Signal()
"""Update the actions"""
sig_hide_finder_requested = Signal()
"""Hide the finder widget"""
sig_load_pdb_file = Signal(str, int)
"""
Called when pdb reaches a new line
impact27 marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
filename: str
The filename the debugger stepped in
line_number: int
The line number the debugger stepped in
"""

def __init__(self, parent, shellwidget, color_scheme):
QWidget.__init__(self, parent)
Expand All @@ -63,6 +99,17 @@ def __init__(self, parent, shellwidget, color_scheme):
self.pdb_curindex = None
self._pdb_state = []

def pdb_has_stopped(self, fname, lineno):
"""Handle pdb has stopped"""
# this will set the focus to the editor
self.sig_load_pdb_file.emit(fname, lineno)
if self.shellwidget._pdb_focus_to_editor:
# Focus to editor will be requested each time
self.shellwidget._pdb_focus_to_editor = False
else:
# take back focus
self.shellwidget._control.setFocus()

def set_context_menu(self, context_menu, empty_context_menu):
"""Set the context menus."""
self.results_browser.menu = context_menu
Expand Down Expand Up @@ -93,9 +140,9 @@ def setup(self):
return

self.results_browser = ResultsBrowser(self, self.color_scheme)
self.results_browser.sig_edit_goto.connect(self.edit_goto)
self.results_browser.sig_edit_goto.connect(self.sig_edit_goto)
self.results_browser.sig_show_namespace.connect(
self.sig_show_namespace)
self._show_namespace)

self.finder = FinderWidget(self)
self.finder.sig_find_text.connect(self.do_find)
Expand All @@ -110,6 +157,10 @@ def setup(self):
layout.addWidget(self.finder)
self.setLayout(layout)

def _show_namespace(self, namespace):
"""Request for the given namespace to be shown in variable explorer."""
impact27 marked this conversation as resolved.
Show resolved Hide resolved
self.sig_show_namespace.emit(namespace, self.shellwidget)

def _show_frames(self, frames, title, state):
"""Set current frames"""
# Reset defaults
Expand Down
Loading