ยปCore Development>Code coverage>Lib/idlelib/debugger.py

Python code coverage for Lib/idlelib/debugger.py

#countcontent
1n/aimport bdb
2n/aimport os
3n/a
4n/afrom tkinter import *
5n/afrom tkinter.ttk import Scrollbar
6n/a
7n/afrom idlelib import macosx
8n/afrom idlelib.scrolledlist import ScrolledList
9n/afrom idlelib.windows import ListedToplevel
10n/a
11n/a
12n/aclass Idb(bdb.Bdb):
13n/a
14n/a def __init__(self, gui):
15n/a self.gui = gui
16n/a bdb.Bdb.__init__(self)
17n/a
18n/a def user_line(self, frame):
19n/a if self.in_rpc_code(frame):
20n/a self.set_step()
21n/a return
22n/a message = self.__frame2message(frame)
23n/a try:
24n/a self.gui.interaction(message, frame)
25n/a except TclError: # When closing debugger window with [x] in 3.x
26n/a pass
27n/a
28n/a def user_exception(self, frame, info):
29n/a if self.in_rpc_code(frame):
30n/a self.set_step()
31n/a return
32n/a message = self.__frame2message(frame)
33n/a self.gui.interaction(message, frame, info)
34n/a
35n/a def in_rpc_code(self, frame):
36n/a if frame.f_code.co_filename.count('rpc.py'):
37n/a return True
38n/a else:
39n/a prev_frame = frame.f_back
40n/a prev_name = prev_frame.f_code.co_filename
41n/a if 'idlelib' in prev_name and 'debugger' in prev_name:
42n/a # catch both idlelib/debugger.py and idlelib/debugger_r.py
43n/a # on both posix and windows
44n/a return False
45n/a return self.in_rpc_code(prev_frame)
46n/a
47n/a def __frame2message(self, frame):
48n/a code = frame.f_code
49n/a filename = code.co_filename
50n/a lineno = frame.f_lineno
51n/a basename = os.path.basename(filename)
52n/a message = "%s:%s" % (basename, lineno)
53n/a if code.co_name != "?":
54n/a message = "%s: %s()" % (message, code.co_name)
55n/a return message
56n/a
57n/a
58n/aclass Debugger:
59n/a
60n/a vstack = vsource = vlocals = vglobals = None
61n/a
62n/a def __init__(self, pyshell, idb=None):
63n/a if idb is None:
64n/a idb = Idb(self)
65n/a self.pyshell = pyshell
66n/a self.idb = idb
67n/a self.frame = None
68n/a self.make_gui()
69n/a self.interacting = 0
70n/a self.nesting_level = 0
71n/a
72n/a def run(self, *args):
73n/a # Deal with the scenario where we've already got a program running
74n/a # in the debugger and we want to start another. If that is the case,
75n/a # our second 'run' was invoked from an event dispatched not from
76n/a # the main event loop, but from the nested event loop in 'interaction'
77n/a # below. So our stack looks something like this:
78n/a # outer main event loop
79n/a # run()
80n/a # <running program with traces>
81n/a # callback to debugger's interaction()
82n/a # nested event loop
83n/a # run() for second command
84n/a #
85n/a # This kind of nesting of event loops causes all kinds of problems
86n/a # (see e.g. issue #24455) especially when dealing with running as a
87n/a # subprocess, where there's all kinds of extra stuff happening in
88n/a # there - insert a traceback.print_stack() to check it out.
89n/a #
90n/a # By this point, we've already called restart_subprocess() in
91n/a # ScriptBinding. However, we also need to unwind the stack back to
92n/a # that outer event loop. To accomplish this, we:
93n/a # - return immediately from the nested run()
94n/a # - abort_loop ensures the nested event loop will terminate
95n/a # - the debugger's interaction routine completes normally
96n/a # - the restart_subprocess() will have taken care of stopping
97n/a # the running program, which will also let the outer run complete
98n/a #
99n/a # That leaves us back at the outer main event loop, at which point our
100n/a # after event can fire, and we'll come back to this routine with a
101n/a # clean stack.
102n/a if self.nesting_level > 0:
103n/a self.abort_loop()
104n/a self.root.after(100, lambda: self.run(*args))
105n/a return
106n/a try:
107n/a self.interacting = 1
108n/a return self.idb.run(*args)
109n/a finally:
110n/a self.interacting = 0
111n/a
112n/a def close(self, event=None):
113n/a try:
114n/a self.quit()
115n/a except Exception:
116n/a pass
117n/a if self.interacting:
118n/a self.top.bell()
119n/a return
120n/a if self.stackviewer:
121n/a self.stackviewer.close(); self.stackviewer = None
122n/a # Clean up pyshell if user clicked debugger control close widget.
123n/a # (Causes a harmless extra cycle through close_debugger() if user
124n/a # toggled debugger from pyshell Debug menu)
125n/a self.pyshell.close_debugger()
126n/a # Now close the debugger control window....
127n/a self.top.destroy()
128n/a
129n/a def make_gui(self):
130n/a pyshell = self.pyshell
131n/a self.flist = pyshell.flist
132n/a self.root = root = pyshell.root
133n/a self.top = top = ListedToplevel(root)
134n/a self.top.wm_title("Debug Control")
135n/a self.top.wm_iconname("Debug")
136n/a top.wm_protocol("WM_DELETE_WINDOW", self.close)
137n/a self.top.bind("<Escape>", self.close)
138n/a #
139n/a self.bframe = bframe = Frame(top)
140n/a self.bframe.pack(anchor="w")
141n/a self.buttons = bl = []
142n/a #
143n/a self.bcont = b = Button(bframe, text="Go", command=self.cont)
144n/a bl.append(b)
145n/a self.bstep = b = Button(bframe, text="Step", command=self.step)
146n/a bl.append(b)
147n/a self.bnext = b = Button(bframe, text="Over", command=self.next)
148n/a bl.append(b)
149n/a self.bret = b = Button(bframe, text="Out", command=self.ret)
150n/a bl.append(b)
151n/a self.bret = b = Button(bframe, text="Quit", command=self.quit)
152n/a bl.append(b)
153n/a #
154n/a for b in bl:
155n/a b.configure(state="disabled")
156n/a b.pack(side="left")
157n/a #
158n/a self.cframe = cframe = Frame(bframe)
159n/a self.cframe.pack(side="left")
160n/a #
161n/a if not self.vstack:
162n/a self.__class__.vstack = BooleanVar(top)
163n/a self.vstack.set(1)
164n/a self.bstack = Checkbutton(cframe,
165n/a text="Stack", command=self.show_stack, variable=self.vstack)
166n/a self.bstack.grid(row=0, column=0)
167n/a if not self.vsource:
168n/a self.__class__.vsource = BooleanVar(top)
169n/a self.bsource = Checkbutton(cframe,
170n/a text="Source", command=self.show_source, variable=self.vsource)
171n/a self.bsource.grid(row=0, column=1)
172n/a if not self.vlocals:
173n/a self.__class__.vlocals = BooleanVar(top)
174n/a self.vlocals.set(1)
175n/a self.blocals = Checkbutton(cframe,
176n/a text="Locals", command=self.show_locals, variable=self.vlocals)
177n/a self.blocals.grid(row=1, column=0)
178n/a if not self.vglobals:
179n/a self.__class__.vglobals = BooleanVar(top)
180n/a self.bglobals = Checkbutton(cframe,
181n/a text="Globals", command=self.show_globals, variable=self.vglobals)
182n/a self.bglobals.grid(row=1, column=1)
183n/a #
184n/a self.status = Label(top, anchor="w")
185n/a self.status.pack(anchor="w")
186n/a self.error = Label(top, anchor="w")
187n/a self.error.pack(anchor="w", fill="x")
188n/a self.errorbg = self.error.cget("background")
189n/a #
190n/a self.fstack = Frame(top, height=1)
191n/a self.fstack.pack(expand=1, fill="both")
192n/a self.flocals = Frame(top)
193n/a self.flocals.pack(expand=1, fill="both")
194n/a self.fglobals = Frame(top, height=1)
195n/a self.fglobals.pack(expand=1, fill="both")
196n/a #
197n/a if self.vstack.get():
198n/a self.show_stack()
199n/a if self.vlocals.get():
200n/a self.show_locals()
201n/a if self.vglobals.get():
202n/a self.show_globals()
203n/a
204n/a def interaction(self, message, frame, info=None):
205n/a self.frame = frame
206n/a self.status.configure(text=message)
207n/a #
208n/a if info:
209n/a type, value, tb = info
210n/a try:
211n/a m1 = type.__name__
212n/a except AttributeError:
213n/a m1 = "%s" % str(type)
214n/a if value is not None:
215n/a try:
216n/a m1 = "%s: %s" % (m1, str(value))
217n/a except:
218n/a pass
219n/a bg = "yellow"
220n/a else:
221n/a m1 = ""
222n/a tb = None
223n/a bg = self.errorbg
224n/a self.error.configure(text=m1, background=bg)
225n/a #
226n/a sv = self.stackviewer
227n/a if sv:
228n/a stack, i = self.idb.get_stack(self.frame, tb)
229n/a sv.load_stack(stack, i)
230n/a #
231n/a self.show_variables(1)
232n/a #
233n/a if self.vsource.get():
234n/a self.sync_source_line()
235n/a #
236n/a for b in self.buttons:
237n/a b.configure(state="normal")
238n/a #
239n/a self.top.wakeup()
240n/a # Nested main loop: Tkinter's main loop is not reentrant, so use
241n/a # Tcl's vwait facility, which reenters the event loop until an
242n/a # event handler sets the variable we're waiting on
243n/a self.nesting_level += 1
244n/a self.root.tk.call('vwait', '::idledebugwait')
245n/a self.nesting_level -= 1
246n/a #
247n/a for b in self.buttons:
248n/a b.configure(state="disabled")
249n/a self.status.configure(text="")
250n/a self.error.configure(text="", background=self.errorbg)
251n/a self.frame = None
252n/a
253n/a def sync_source_line(self):
254n/a frame = self.frame
255n/a if not frame:
256n/a return
257n/a filename, lineno = self.__frame2fileline(frame)
258n/a if filename[:1] + filename[-1:] != "<>" and os.path.exists(filename):
259n/a self.flist.gotofileline(filename, lineno)
260n/a
261n/a def __frame2fileline(self, frame):
262n/a code = frame.f_code
263n/a filename = code.co_filename
264n/a lineno = frame.f_lineno
265n/a return filename, lineno
266n/a
267n/a def cont(self):
268n/a self.idb.set_continue()
269n/a self.abort_loop()
270n/a
271n/a def step(self):
272n/a self.idb.set_step()
273n/a self.abort_loop()
274n/a
275n/a def next(self):
276n/a self.idb.set_next(self.frame)
277n/a self.abort_loop()
278n/a
279n/a def ret(self):
280n/a self.idb.set_return(self.frame)
281n/a self.abort_loop()
282n/a
283n/a def quit(self):
284n/a self.idb.set_quit()
285n/a self.abort_loop()
286n/a
287n/a def abort_loop(self):
288n/a self.root.tk.call('set', '::idledebugwait', '1')
289n/a
290n/a stackviewer = None
291n/a
292n/a def show_stack(self):
293n/a if not self.stackviewer and self.vstack.get():
294n/a self.stackviewer = sv = StackViewer(self.fstack, self.flist, self)
295n/a if self.frame:
296n/a stack, i = self.idb.get_stack(self.frame, None)
297n/a sv.load_stack(stack, i)
298n/a else:
299n/a sv = self.stackviewer
300n/a if sv and not self.vstack.get():
301n/a self.stackviewer = None
302n/a sv.close()
303n/a self.fstack['height'] = 1
304n/a
305n/a def show_source(self):
306n/a if self.vsource.get():
307n/a self.sync_source_line()
308n/a
309n/a def show_frame(self, stackitem):
310n/a self.frame = stackitem[0] # lineno is stackitem[1]
311n/a self.show_variables()
312n/a
313n/a localsviewer = None
314n/a globalsviewer = None
315n/a
316n/a def show_locals(self):
317n/a lv = self.localsviewer
318n/a if self.vlocals.get():
319n/a if not lv:
320n/a self.localsviewer = NamespaceViewer(self.flocals, "Locals")
321n/a else:
322n/a if lv:
323n/a self.localsviewer = None
324n/a lv.close()
325n/a self.flocals['height'] = 1
326n/a self.show_variables()
327n/a
328n/a def show_globals(self):
329n/a gv = self.globalsviewer
330n/a if self.vglobals.get():
331n/a if not gv:
332n/a self.globalsviewer = NamespaceViewer(self.fglobals, "Globals")
333n/a else:
334n/a if gv:
335n/a self.globalsviewer = None
336n/a gv.close()
337n/a self.fglobals['height'] = 1
338n/a self.show_variables()
339n/a
340n/a def show_variables(self, force=0):
341n/a lv = self.localsviewer
342n/a gv = self.globalsviewer
343n/a frame = self.frame
344n/a if not frame:
345n/a ldict = gdict = None
346n/a else:
347n/a ldict = frame.f_locals
348n/a gdict = frame.f_globals
349n/a if lv and gv and ldict is gdict:
350n/a ldict = None
351n/a if lv:
352n/a lv.load_dict(ldict, force, self.pyshell.interp.rpcclt)
353n/a if gv:
354n/a gv.load_dict(gdict, force, self.pyshell.interp.rpcclt)
355n/a
356n/a def set_breakpoint_here(self, filename, lineno):
357n/a self.idb.set_break(filename, lineno)
358n/a
359n/a def clear_breakpoint_here(self, filename, lineno):
360n/a self.idb.clear_break(filename, lineno)
361n/a
362n/a def clear_file_breaks(self, filename):
363n/a self.idb.clear_all_file_breaks(filename)
364n/a
365n/a def load_breakpoints(self):
366n/a "Load PyShellEditorWindow breakpoints into subprocess debugger"
367n/a for editwin in self.pyshell.flist.inversedict:
368n/a filename = editwin.io.filename
369n/a try:
370n/a for lineno in editwin.breakpoints:
371n/a self.set_breakpoint_here(filename, lineno)
372n/a except AttributeError:
373n/a continue
374n/a
375n/aclass StackViewer(ScrolledList):
376n/a
377n/a def __init__(self, master, flist, gui):
378n/a if macosx.isAquaTk():
379n/a # At least on with the stock AquaTk version on OSX 10.4 you'll
380n/a # get a shaking GUI that eventually kills IDLE if the width
381n/a # argument is specified.
382n/a ScrolledList.__init__(self, master)
383n/a else:
384n/a ScrolledList.__init__(self, master, width=80)
385n/a self.flist = flist
386n/a self.gui = gui
387n/a self.stack = []
388n/a
389n/a def load_stack(self, stack, index=None):
390n/a self.stack = stack
391n/a self.clear()
392n/a for i in range(len(stack)):
393n/a frame, lineno = stack[i]
394n/a try:
395n/a modname = frame.f_globals["__name__"]
396n/a except:
397n/a modname = "?"
398n/a code = frame.f_code
399n/a filename = code.co_filename
400n/a funcname = code.co_name
401n/a import linecache
402n/a sourceline = linecache.getline(filename, lineno)
403n/a sourceline = sourceline.strip()
404n/a if funcname in ("?", "", None):
405n/a item = "%s, line %d: %s" % (modname, lineno, sourceline)
406n/a else:
407n/a item = "%s.%s(), line %d: %s" % (modname, funcname,
408n/a lineno, sourceline)
409n/a if i == index:
410n/a item = "> " + item
411n/a self.append(item)
412n/a if index is not None:
413n/a self.select(index)
414n/a
415n/a def popup_event(self, event):
416n/a "override base method"
417n/a if self.stack:
418n/a return ScrolledList.popup_event(self, event)
419n/a
420n/a def fill_menu(self):
421n/a "override base method"
422n/a menu = self.menu
423n/a menu.add_command(label="Go to source line",
424n/a command=self.goto_source_line)
425n/a menu.add_command(label="Show stack frame",
426n/a command=self.show_stack_frame)
427n/a
428n/a def on_select(self, index):
429n/a "override base method"
430n/a if 0 <= index < len(self.stack):
431n/a self.gui.show_frame(self.stack[index])
432n/a
433n/a def on_double(self, index):
434n/a "override base method"
435n/a self.show_source(index)
436n/a
437n/a def goto_source_line(self):
438n/a index = self.listbox.index("active")
439n/a self.show_source(index)
440n/a
441n/a def show_stack_frame(self):
442n/a index = self.listbox.index("active")
443n/a if 0 <= index < len(self.stack):
444n/a self.gui.show_frame(self.stack[index])
445n/a
446n/a def show_source(self, index):
447n/a if not (0 <= index < len(self.stack)):
448n/a return
449n/a frame, lineno = self.stack[index]
450n/a code = frame.f_code
451n/a filename = code.co_filename
452n/a if os.path.isfile(filename):
453n/a edit = self.flist.open(filename)
454n/a if edit:
455n/a edit.gotoline(lineno)
456n/a
457n/a
458n/aclass NamespaceViewer:
459n/a
460n/a def __init__(self, master, title, dict=None):
461n/a width = 0
462n/a height = 40
463n/a if dict:
464n/a height = 20*len(dict) # XXX 20 == observed height of Entry widget
465n/a self.master = master
466n/a self.title = title
467n/a import reprlib
468n/a self.repr = reprlib.Repr()
469n/a self.repr.maxstring = 60
470n/a self.repr.maxother = 60
471n/a self.frame = frame = Frame(master)
472n/a self.frame.pack(expand=1, fill="both")
473n/a self.label = Label(frame, text=title, borderwidth=2, relief="groove")
474n/a self.label.pack(fill="x")
475n/a self.vbar = vbar = Scrollbar(frame, name="vbar")
476n/a vbar.pack(side="right", fill="y")
477n/a self.canvas = canvas = Canvas(frame,
478n/a height=min(300, max(40, height)),
479n/a scrollregion=(0, 0, width, height))
480n/a canvas.pack(side="left", fill="both", expand=1)
481n/a vbar["command"] = canvas.yview
482n/a canvas["yscrollcommand"] = vbar.set
483n/a self.subframe = subframe = Frame(canvas)
484n/a self.sfid = canvas.create_window(0, 0, window=subframe, anchor="nw")
485n/a self.load_dict(dict)
486n/a
487n/a dict = -1
488n/a
489n/a def load_dict(self, dict, force=0, rpc_client=None):
490n/a if dict is self.dict and not force:
491n/a return
492n/a subframe = self.subframe
493n/a frame = self.frame
494n/a for c in list(subframe.children.values()):
495n/a c.destroy()
496n/a self.dict = None
497n/a if not dict:
498n/a l = Label(subframe, text="None")
499n/a l.grid(row=0, column=0)
500n/a else:
501n/a #names = sorted(dict)
502n/a ###
503n/a # Because of (temporary) limitations on the dict_keys type (not yet
504n/a # public or pickleable), have the subprocess to send a list of
505n/a # keys, not a dict_keys object. sorted() will take a dict_keys
506n/a # (no subprocess) or a list.
507n/a #
508n/a # There is also an obscure bug in sorted(dict) where the
509n/a # interpreter gets into a loop requesting non-existing dict[0],
510n/a # dict[1], dict[2], etc from the debugger_r.DictProxy.
511n/a ###
512n/a keys_list = dict.keys()
513n/a names = sorted(keys_list)
514n/a ###
515n/a row = 0
516n/a for name in names:
517n/a value = dict[name]
518n/a svalue = self.repr.repr(value) # repr(value)
519n/a # Strip extra quotes caused by calling repr on the (already)
520n/a # repr'd value sent across the RPC interface:
521n/a if rpc_client:
522n/a svalue = svalue[1:-1]
523n/a l = Label(subframe, text=name)
524n/a l.grid(row=row, column=0, sticky="nw")
525n/a l = Entry(subframe, width=0, borderwidth=0)
526n/a l.insert(0, svalue)
527n/a l.grid(row=row, column=1, sticky="nw")
528n/a row = row+1
529n/a self.dict = dict
530n/a # XXX Could we use a <Configure> callback for the following?
531n/a subframe.update_idletasks() # Alas!
532n/a width = subframe.winfo_reqwidth()
533n/a height = subframe.winfo_reqheight()
534n/a canvas = self.canvas
535n/a self.canvas["scrollregion"] = (0, 0, width, height)
536n/a if height > 300:
537n/a canvas["height"] = 300
538n/a frame.pack(expand=1)
539n/a else:
540n/a canvas["height"] = height
541n/a frame.pack(expand=0)
542n/a
543n/a def close(self):
544n/a self.frame.destroy()