-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
plugin.py
645 lines (557 loc) · 22.2 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""Debugger Plugin."""
# Standard library imports
import os.path as osp
from typing import List
# Third-party imports
from qtpy.QtCore import Slot
# Local imports
from spyder.api.plugins import Plugins, SpyderDockablePlugin
from spyder.api.plugin_registration.decorators import (
on_plugin_available, on_plugin_teardown)
from spyder.api.shellconnect.mixins import ShellConnectPluginMixin
from spyder.api.translations import _
from spyder.config.manager import CONF
from spyder.plugins.debugger.confpage import DebuggerConfigPage
from spyder.plugins.debugger.utils.breakpointsmanager import (
BreakpointsManager, clear_all_breakpoints, clear_breakpoint)
from spyder.plugins.debugger.widgets.main_widget import (
DebuggerBreakpointActions, DebuggerWidget, DebuggerWidgetActions)
from spyder.plugins.editor.utils.editor import get_file_language
from spyder.plugins.editor.utils.languages import ALL_LANGUAGES
from spyder.plugins.ipythonconsole.api import IPythonConsolePyConfiguration
from spyder.plugins.mainmenu.api import ApplicationMenus, DebugMenuSections
from spyder.plugins.run.api import (
RunConfiguration, ExtendedRunExecutionParameters, RunExecutor, run_execute,
RunContext, RunResult)
from spyder.plugins.toolbar.api import ApplicationToolbars
from spyder.plugins.ipythonconsole.widgets.config import IPythonConfigOptions
from spyder.plugins.editor.api.run import CellRun, SelectionRun
class Debugger(SpyderDockablePlugin, ShellConnectPluginMixin, RunExecutor):
"""Debugger plugin."""
NAME = 'debugger'
REQUIRES = [Plugins.IPythonConsole, Plugins.Preferences, Plugins.Run]
OPTIONAL = [Plugins.Editor, Plugins.MainMenu, Plugins.VariableExplorer]
TABIFY = [Plugins.VariableExplorer, Plugins.Help]
WIDGET_CLASS = DebuggerWidget
CONF_SECTION = NAME
CONF_FILE = False
CONF_WIDGET_CLASS = DebuggerConfigPage
DISABLE_ACTIONS_WHEN_HIDDEN = False
# ---- SpyderDockablePlugin API
# ------------------------------------------------------------------------
@staticmethod
def get_name():
return _('Debugger')
@staticmethod
def get_description():
return _('View, explore and navigate stack frames while debugging.')
@classmethod
def get_icon(cls):
return cls.create_icon('debug')
def on_initialize(self):
widget = self.get_widget()
widget.sig_pdb_state_changed.connect(
self._update_current_codeeditor_pdb_state)
widget.sig_toggle_breakpoints.connect(self._set_or_clear_breakpoint)
widget.sig_toggle_conditional_breakpoints.connect(
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)
widget.sig_clear_breakpoint.connect(self.clear_breakpoint)
widget.sig_switch_to_plugin_requested.connect(self.switch_to_plugin)
self.python_editor_run_configuration = {
'origin': self.NAME,
'extension': 'py',
'contexts': [
{
'name': 'File'
},
{
'name': 'Cell'
},
{
'name': 'Selection'
},
]
}
self.ipython_editor_run_configuration = {
'origin': self.NAME,
'extension': 'ipy',
'contexts': [
{
'name': 'File'
},
{
'name': 'Cell'
},
{
'name': 'Selection'
},
]
}
self.executor_configuration = [
{
'input_extension': 'py',
'context': {
'name': 'File'
},
'output_formats': [],
'configuration_widget': IPythonConfigOptions,
'requires_cwd': True,
'priority': 10
},
{
'input_extension': 'ipy',
'context': {
'name': 'File'
},
'output_formats': [],
'configuration_widget': IPythonConfigOptions,
'requires_cwd': True,
'priority': 10
},
{
'input_extension': 'py',
'context': {
'name': 'Cell'
},
'output_formats': [],
'configuration_widget': None,
'requires_cwd': True,
'priority': 10
},
{
'input_extension': 'ipy',
'context': {
'name': 'Cell'
},
'output_formats': [],
'configuration_widget': None,
'requires_cwd': True,
'priority': 10
},
{
'input_extension': 'py',
'context': {
'name': 'Selection'
},
'output_formats': [],
'configuration_widget': None,
'requires_cwd': True,
'priority': 10
},
{
'input_extension': 'ipy',
'context': {
'name': 'Selection'
},
'output_formats': [],
'configuration_widget': None,
'requires_cwd': True,
'priority': 10
},
]
@on_plugin_available(plugin=Plugins.Run)
def on_run_available(self):
run = self.get_plugin(Plugins.Run)
run.register_executor_configuration(self, self.executor_configuration)
run.create_run_in_executor_button(
RunContext.File,
self.NAME,
text=_("&Debug file"),
tip=_("Debug file"),
icon=self.create_icon('debug'),
shortcut_context=self.NAME,
register_shortcut=True,
add_to_menu={
"menu": ApplicationMenus.Debug,
"section": DebugMenuSections.StartDebug,
"before_section": DebugMenuSections.ControlDebug
},
add_to_toolbar=ApplicationToolbars.Debug
)
run.create_run_in_executor_button(
RunContext.Cell,
self.NAME,
text=_("Debug cell"),
tip=_("Debug cell"),
icon=self.create_icon('debug_cell'),
shortcut_context=self.NAME,
register_shortcut=True,
add_to_menu={
"menu": ApplicationMenus.Debug,
"section": DebugMenuSections.StartDebug,
"before_section": DebugMenuSections.ControlDebug
},
add_to_toolbar=ApplicationToolbars.Debug
)
run.create_run_in_executor_button(
RunContext.Selection,
self.NAME,
text=_("Debug selection or current line"),
tip=_("Debug selection or current line"),
icon=self.create_icon('debug_selection'),
shortcut_context=self.NAME,
register_shortcut=True,
add_to_menu={
"menu": ApplicationMenus.Debug,
"section": DebugMenuSections.StartDebug,
"before_section": DebugMenuSections.ControlDebug
},
add_to_toolbar=ApplicationToolbars.Debug
)
@on_plugin_teardown(plugin=Plugins.Run)
def on_run_teardown(self):
run = self.get_plugin(Plugins.Run)
run.deregister_executor_configuration(
self, self.executor_configuration
)
run.destroy_run_in_executor_button(RunContext.File, self.NAME)
run.destroy_run_in_executor_button(RunContext.Cell, self.NAME)
run.destroy_run_in_executor_button(RunContext.Selection, self.NAME)
@on_plugin_available(plugin=Plugins.Preferences)
def on_preferences_available(self):
preferences = self.get_plugin(Plugins.Preferences)
preferences.register_plugin_preferences(self)
@on_plugin_teardown(plugin=Plugins.Preferences)
def on_preferences_teardown(self):
preferences = self.get_plugin(Plugins.Preferences)
preferences.deregister_plugin_preferences(self)
@on_plugin_available(plugin=Plugins.Editor)
def on_editor_available(self):
editor = self.get_plugin(Plugins.Editor)
widget = self.get_widget()
for run_config in [
self.python_editor_run_configuration,
self.ipython_editor_run_configuration
]:
editor.add_supported_run_configuration(run_config)
# The editor is available, connect signals.
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)
# Apply shortcuts to editor and add actions to pythonfile list
editor_shortcuts = [
DebuggerBreakpointActions.ToggleBreakpoint,
DebuggerBreakpointActions.ToggleConditionalBreakpoint,
DebuggerBreakpointActions.ShowBreakpointsTable,
]
for name in editor_shortcuts:
action = self.get_action(name)
# TODO: This should be handled differently?
CONF.config_shortcut(
action.trigger,
context=self.CONF_SECTION,
name=name,
parent=editor.get_widget()
)
editor.get_widget().pythonfile_dependent_actions += [action]
@on_plugin_teardown(plugin=Plugins.Editor)
def on_editor_teardown(self):
editor = self.get_plugin(Plugins.Editor)
widget = self.get_widget()
for run_config in [
self.python_editor_run_configuration,
self.ipython_editor_run_configuration
]:
editor.remove_supported_run_configuration(run_config)
widget.sig_edit_goto.disconnect(editor.load)
editor.sig_codeeditor_created.disconnect(self._add_codeeditor)
editor.sig_codeeditor_changed.disconnect(self._update_codeeditor)
editor.sig_codeeditor_deleted.disconnect(self._remove_codeeditor)
# Remove editor actions
editor_shortcuts = [
DebuggerBreakpointActions.ToggleBreakpoint,
DebuggerBreakpointActions.ToggleConditionalBreakpoint,
DebuggerBreakpointActions.ShowBreakpointsTable,
]
for name in editor_shortcuts:
action = self.get_action(name)
if action in editor.get_widget().pythonfile_dependent_actions:
editor.get_widget().pythonfile_dependent_actions.remove(action)
@on_plugin_available(plugin=Plugins.VariableExplorer)
def on_variable_explorer_available(self):
self.get_widget().sig_show_namespace.connect(
self._show_namespace_in_variable_explorer)
@on_plugin_teardown(plugin=Plugins.VariableExplorer)
def on_variable_explorer_teardown(self):
self.get_widget().sig_show_namespace.disconnect(
self._show_namespace_in_variable_explorer)
@on_plugin_available(plugin=Plugins.MainMenu)
def on_main_menu_available(self):
mainmenu = self.get_plugin(Plugins.MainMenu)
# ControlDebug section
for action in [DebuggerWidgetActions.Next,
DebuggerWidgetActions.Step,
DebuggerWidgetActions.Return,
DebuggerWidgetActions.Continue,
DebuggerWidgetActions.Stop]:
mainmenu.add_item_to_application_menu(
self.get_action(action),
menu_id=ApplicationMenus.Debug,
section=DebugMenuSections.ControlDebug,
before_section=DebugMenuSections.EditBreakpoints)
# Breakpoints section
for action in [DebuggerBreakpointActions.ToggleBreakpoint,
DebuggerBreakpointActions.ToggleConditionalBreakpoint,
DebuggerBreakpointActions.ClearAllBreakpoints,
DebuggerBreakpointActions.ShowBreakpointsTable]:
mainmenu.add_item_to_application_menu(
self.get_action(action),
menu_id=ApplicationMenus.Debug,
section=DebugMenuSections.EditBreakpoints)
@on_plugin_teardown(plugin=Plugins.MainMenu)
def on_main_menu_teardown(self):
mainmenu = self.get_plugin(Plugins.MainMenu)
names = [
DebuggerWidgetActions.Next,
DebuggerWidgetActions.Step,
DebuggerWidgetActions.Return,
DebuggerWidgetActions.Continue,
DebuggerWidgetActions.Stop,
DebuggerBreakpointActions.ToggleBreakpoint,
DebuggerBreakpointActions.ToggleConditionalBreakpoint,
DebuggerBreakpointActions.ClearAllBreakpoints,
DebuggerBreakpointActions.ShowBreakpointsTable,
]
for name in names:
mainmenu.remove_item_from_application_menu(
name,
menu_id=ApplicationMenus.Debug
)
# ---- 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
# 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.
This should only be called when there is a Variable explorer
"""
variable_explorer = self.get_plugin(Plugins.VariableExplorer)
if variable_explorer is None:
return
nsb = variable_explorer.get_widget_for_shellwidget(shellwidget)
nsb.process_remote_view(namespace)
def _is_python_editor(self, codeeditor):
"""Check if the editor is a python editor."""
if codeeditor.filename is None:
return False
txt = codeeditor.get_text_with_eol()
language = get_file_language(codeeditor.filename, txt)
return language.lower() in ALL_LANGUAGES["Python"]
def _connect_codeeditor(self, codeeditor):
"""Connect a code editor."""
codeeditor.breakpoints_manager = BreakpointsManager(codeeditor)
codeeditor.breakpoints_manager.sig_breakpoints_saved.connect(
self.get_widget().sig_breakpoints_saved)
def _disconnect_codeeditor(self, codeeditor):
"""Connect a code editor."""
codeeditor.breakpoints_manager.sig_breakpoints_saved.disconnect(
self.get_widget().sig_breakpoints_saved)
codeeditor.breakpoints_manager = None
@Slot(str)
def _filename_changed(self, filename):
"""Change filename."""
codeeditor = self._get_editor_for_filename(filename)
if codeeditor is None:
return
if codeeditor.breakpoints_manager is None:
# Was not a python editor
if self._is_python_editor(codeeditor):
self._connect_codeeditor(codeeditor)
else:
# Was a python editor
if self._is_python_editor(codeeditor):
codeeditor.breakpoints_manager.set_filename(filename)
else:
self._disconnect_codeeditor(codeeditor)
@Slot(object)
def _add_codeeditor(self, codeeditor):
"""
Add a new codeeditor.
"""
codeeditor.sig_filename_changed.connect(self._filename_changed)
codeeditor.breakpoints_manager = None
if self._is_python_editor(codeeditor):
self._connect_codeeditor(codeeditor)
@Slot(object)
def _remove_codeeditor(self, codeeditor):
"""
Remove a codeeditor.
"""
codeeditor.sig_filename_changed.disconnect(self._filename_changed)
if codeeditor.breakpoints_manager is not None:
self._disconnect_codeeditor(codeeditor)
@Slot(object)
def _update_codeeditor(self, codeeditor):
"""
Focus codeeditor has changed.
"""
if (
codeeditor.filename is None or
codeeditor.breakpoints_manager is None
):
return
# Update debugging state
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, filename, lineno)
@Slot(bool)
def _update_current_codeeditor_pdb_state(self, pdb_state):
"""
The pdb state has changed.
"""
try:
codeeditor = self._get_current_editor()
if codeeditor is None or codeeditor.breakpoints_manager is None:
return
filename, line_number = self.get_widget().get_pdb_last_step()
codeeditor.breakpoints_manager.update_pdb_state(
pdb_state, filename, line_number)
except RuntimeError:
pass
def _get_current_editor(self):
"""
Get current codeeditor.
"""
editor = self.get_plugin(Plugins.Editor)
if editor is None:
return None
return editor.get_current_editor()
def _get_editor_for_filename(self, filename):
"""Get editor for filename."""
editor = self.get_plugin(Plugins.Editor)
if editor is None:
return None
return editor.get_codeeditor_for_filename(filename)
def _get_current_editorstack(self):
"""
Get current editorstack.
"""
editor = self.get_plugin(Plugins.Editor)
if editor is None:
return None
return editor.get_current_editorstack()
@Slot()
def _set_or_clear_breakpoint(self):
"""Set/Clear breakpoint"""
codeeditor = self._get_current_editor()
if codeeditor is None or codeeditor.breakpoints_manager is None:
return
codeeditor.breakpoints_manager.toogle_breakpoint()
@Slot()
def _set_or_edit_conditional_breakpoint(self):
"""Set/Edit conditional breakpoint"""
codeeditor = self._get_current_editor()
if codeeditor is None or codeeditor.breakpoints_manager is None:
return
codeeditor.breakpoints_manager.toogle_breakpoint(
edit_condition=True)
# ---- Public API
# ------------------------------------------------------------------------
@Slot()
def clear_all_breakpoints(self):
"""Clear breakpoints in all files"""
clear_all_breakpoints()
self.get_widget().sig_breakpoints_saved.emit()
editorstack = self._get_current_editorstack()
if editorstack is not None:
for data in editorstack.data:
if data.editor.breakpoints_manager is not None:
data.editor.breakpoints_manager.clear_breakpoints()
@Slot(str, int)
def clear_breakpoint(self, filename, lineno):
"""Remove a single breakpoint"""
clear_breakpoint(filename, lineno)
self.get_widget().sig_breakpoints_saved.emit()
codeeditor = self._get_editor_for_filename(filename)
if codeeditor is None or codeeditor.breakpoints_manager is None:
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()
debugging = widget.get_pdb_state()
if not debugging:
return True
pdb_fname, __ = widget.get_pdb_last_step()
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()
return False
# ---- For execution
@run_execute(context=RunContext.File)
def debug_file(
self,
input: RunConfiguration,
conf: ExtendedRunExecutionParameters
) -> List[RunResult]:
console = self.get_plugin(Plugins.IPythonConsole)
if console is None:
return
exec_params = conf['params']
params: IPythonConsolePyConfiguration = exec_params['executor_params']
params["run_method"] = "debugfile"
console.exec_files(input, conf)
self.get_widget().set_pdb_take_focus(False)
@run_execute(context=RunContext.Cell)
def debug_cell(
self,
input: RunConfiguration,
conf: ExtendedRunExecutionParameters
) -> List[RunResult]:
console = self.get_plugin(Plugins.IPythonConsole)
if console is None:
return
run_input: CellRun = input['run_input']
if run_input['copy']:
code = run_input['cell']
if not code.strip():
# Empty cell
return
console.run_selection("%%debug\n" + code)
return
exec_params = conf['params']
params: IPythonConsolePyConfiguration = exec_params['executor_params']
params["run_method"] = "debugcell"
console.exec_cell(input, conf)
self.get_widget().set_pdb_take_focus(False)
@run_execute(context=RunContext.Selection)
def debug_selection(
self,
input: RunConfiguration,
conf: ExtendedRunExecutionParameters
) -> List[RunResult]:
console = self.get_plugin(Plugins.IPythonConsole)
if console is None:
return
run_input: SelectionRun = input['run_input']
code = run_input['selection']
if not code.strip():
# No selection
return
run_input['selection'] = "%%debug\n" + code
console.exec_selection(input, conf)
self.get_widget().set_pdb_take_focus(False)