Guido van Rossum | 19a86e7 | 1992-01-27 17:00:10 +0000 | [diff] [blame] | 1 | # wdbframewin.py -- frame window for wdb.py |
| 2 | |
| 3 | # XXX To do: |
| 4 | # - display function name in window title |
| 5 | # - execute arbitrary statements instead of just evaluating expressions |
| 6 | # - allow setting variables by editing their values |
| 7 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 8 | |
| 9 | import stdwin |
| 10 | from stdwinevents import * |
| 11 | import basewin |
| 12 | import sys |
| 13 | |
| 14 | WIDTH = 40 |
Guido van Rossum | 19a86e7 | 1992-01-27 17:00:10 +0000 | [diff] [blame] | 15 | MINHEIGHT = 8 |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 16 | MAXHEIGHT = 16 |
| 17 | |
| 18 | class FrameWindow(basewin.BaseWindow): |
| 19 | |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 20 | def __init__(self, debugger, frame, dict, name): |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 21 | self.debugger = debugger |
| 22 | self.frame = frame # Not used except for identity tests |
| 23 | self.dict = dict |
| 24 | self.name = name |
Guido van Rossum | 19a86e7 | 1992-01-27 17:00:10 +0000 | [diff] [blame] | 25 | nl = max(MINHEIGHT, len(self.dict) + 5) |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 26 | nl = min(nl, MAXHEIGHT) |
| 27 | width = WIDTH*stdwin.textwidth('0') |
| 28 | height = nl*stdwin.lineheight() |
| 29 | stdwin.setdefwinsize(width, height) |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 30 | basewin.BaseWindow.__init__( |
| 31 | self, '--Frame ' + name + '--') |
Guido van Rossum | 19a86e7 | 1992-01-27 17:00:10 +0000 | [diff] [blame] | 32 | # XXX Should use current function name |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 33 | self.initeditor() |
| 34 | self.displaylist = ['>>>', '', '-'*WIDTH] |
| 35 | self.refreshframe() |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 36 | |
| 37 | def initeditor(self): |
| 38 | r = (stdwin.textwidth('>>> '), 0), (30000, stdwin.lineheight()) |
| 39 | self.editor = self.win.textcreate(r) |
| 40 | |
| 41 | def closeeditor(self): |
| 42 | self.editor.close() |
| 43 | |
| 44 | def dispatch(self, event): |
| 45 | type, win, detail = event |
| 46 | if type == WE_NULL: return # Dummy tested by mainloop |
| 47 | if type in (WE_DRAW, WE_COMMAND) \ |
| 48 | or not self.editor.event(event): |
| 49 | basewin.BaseWindow.dispatch(self, event) |
| 50 | |
| 51 | def close(self): |
| 52 | del self.debugger.framewindows[self.name] |
| 53 | del self.debugger, self.dict |
| 54 | self.closeeditor() |
| 55 | basewin.BaseWindow.close(self) |
| 56 | |
| 57 | def command(self, detail): |
| 58 | if detail == WC_RETURN: |
| 59 | self.re_eval() |
| 60 | else: |
| 61 | dummy = self.editor.event(WE_COMMAND, \ |
| 62 | self.win, detail) |
| 63 | |
Sjoerd Mullender | 148644d | 1993-10-01 14:29:45 +0000 | [diff] [blame] | 64 | def mouse_down(self, detail): |
| 65 | (h, v), clicks, button, mask = detail |
Sjoerd Mullender | be80e0e | 1993-10-01 14:39:45 +0000 | [diff] [blame] | 66 | if clicks != 2: |
| 67 | return |
Sjoerd Mullender | 148644d | 1993-10-01 14:29:45 +0000 | [diff] [blame] | 68 | i = v / stdwin.lineheight() |
| 69 | if 5 <= i < len(self.displaylist): |
| 70 | import string |
| 71 | name = string.splitfields(self.displaylist[i],' = ')[0] |
| 72 | if not self.dict.has_key(name): |
| 73 | stdwin.fleep() |
| 74 | return |
| 75 | value = self.dict[name] |
| 76 | if not hasattr(value, '__dict__'): |
| 77 | stdwin.fleep() |
| 78 | return |
| 79 | name = 'instance ' + `value` |
| 80 | if self.debugger.framewindows.has_key(name): |
| 81 | self.debugger.framewindows[name].popup() |
| 82 | else: |
| 83 | self.debugger.framewindows[name] = \ |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 84 | FrameWindow(self.debugger, |
Sjoerd Mullender | 148644d | 1993-10-01 14:29:45 +0000 | [diff] [blame] | 85 | self.frame, value.__dict__, |
| 86 | name) |
| 87 | return |
| 88 | stdwin.fleep() |
| 89 | |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 90 | def re_eval(self): |
| 91 | import string, repr |
| 92 | expr = string.strip(self.editor.gettext()) |
| 93 | if expr == '': |
| 94 | output = '' |
| 95 | else: |
| 96 | globals = self.frame.f_globals |
Guido van Rossum | 79c85f1 | 1993-12-14 15:54:01 +0000 | [diff] [blame] | 97 | globals['__privileged__'] = 1 |
Guido van Rossum | 19a86e7 | 1992-01-27 17:00:10 +0000 | [diff] [blame] | 98 | locals = self.dict |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 99 | try: |
| 100 | value = eval(expr, globals, locals) |
| 101 | output = repr.repr(value) |
| 102 | except: |
Guido van Rossum | 5e38b6f | 1995-02-27 13:13:40 +0000 | [diff] [blame] | 103 | if type(sys.exc_type) == type(''): |
| 104 | exc_type_name = sys.exc_type |
| 105 | else: exc_type_name = sys.exc_type.__name__ |
| 106 | output = exc_type_name + ': ' + `sys.exc_value` |
Guido van Rossum | babe2bf | 1992-01-22 22:21:31 +0000 | [diff] [blame] | 107 | self.displaylist[1] = output |
| 108 | lh = stdwin.lineheight() |
| 109 | r = (-10, 0), (30000, 2*lh) |
| 110 | self.win.change(r) |
| 111 | self.editor.setfocus(0, len(expr)) |
| 112 | |
| 113 | def draw(self, detail): |
| 114 | (left, top), (right, bottom) = detail |
| 115 | dummy = self.editor.draw(detail) |
| 116 | d = self.win.begindrawing() |
| 117 | try: |
| 118 | lh = d.lineheight() |
| 119 | h, v = 0, 0 |
| 120 | for line in self.displaylist: |
| 121 | if v+lh > top and v < bottom: |
| 122 | d.text((h, v), line) |
| 123 | v = v + lh |
| 124 | finally: |
| 125 | d.close() |
| 126 | |
| 127 | def refreshframe(self): |
| 128 | import repr |
| 129 | del self.displaylist[3:] |
| 130 | self.re_eval() |
| 131 | names = self.dict.keys() |
| 132 | for key, label in ('__args__', 'Args: '), \ |
| 133 | ('__return__', 'Return: '): |
| 134 | if self.dict.has_key(key): |
| 135 | names.remove(key) |
| 136 | value = self.dict[key] |
| 137 | label = label + repr.repr(value) |
| 138 | self.displaylist.append(label) |
| 139 | names.sort() |
| 140 | for name in names: |
| 141 | value = self.dict[name] |
| 142 | line = name + ' = ' + repr.repr(value) |
| 143 | self.displaylist.append(line) |
| 144 | self.win.setdocsize(0, \ |
| 145 | stdwin.lineheight() * len(self.displaylist)) |
| 146 | self.refreshall() # XXX Be more subtle later |