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