File: Breakpoint.py

package info (click to toggle)
boa-constructor 0.4.4cvs20050714-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,080 kB
  • ctags: 9,175
  • sloc: python: 56,189; sh: 545; makefile: 40
file content (236 lines) | stat: -rw-r--r-- 8,251 bytes parent folder | download
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
"""
Provides a breakpoint registry that can be sent to another process (via
getBreakpointList()).
"""

import os

try: from cPickle import Pickler, Unpickler
except: from pickle import Pickler, Unpickler


class FileBreakpointList:
    def __init__(self):
        self.lines = {}  # lineno -> [{'temporary', 'cond', 'enabled', 'ignore'}]

    def loadBreakpoints(self, fn):
        try:
            if os.path.exists(fn):
                f = open(fn, 'rb')
                u = Unpickler(f)
                newlines = u.load()
                # The following line isn't quite correct
                # when multiple breakpoints are set on a
                # single line.
                self.lines.update(newlines)
                return 1
            else:
                return 0
        except:
            self.lines = {}
            return 0

    def saveBreakpoints(self, fn):
        try:
            if len(self.lines):
                savelines = {}
                # Filter out the temporary lines when saving.
                for lineno, linebreaks in self.lines.items():
                    savelines[lineno] = saveline = []
                    for brk in linebreaks:
                        if not brk['temporary']:
                            saveline.append(brk)
                f = open(fn, 'wb')
                p = Pickler(f)
                p.dump(savelines)
            else:
                os.remove(fn)
        except:
            pass

    def addBreakpoint(self, lineno, temp=0, cond='', ignore=0):
        newbrk = {'temporary':temp, 'cond':cond, 'enabled':1, 'ignore':ignore}
        if self.lines.has_key(lineno):
            linebreaks = self.lines[lineno]
            for brk in linebreaks:
                if brk['temporary'] == temp and brk['cond'] == cond:
                    # Already added.
                    return
            linebreaks.append(newbrk)
        else:
            self.lines[lineno] = linebreaks = [newbrk]

    def deleteBreakpoints(self, lineno):
        if self.lines.has_key(lineno):
            del self.lines[lineno]

    def moveBreakpoint(self, lineno, newlineno):
        if lineno != newlineno and self.lines.has_key(lineno):
            bp = self.lines[lineno]
            del self.lines[lineno]
            self.lines[lineno] = bp

    def adjustBreakpoints(self, lineno, delta):
        set_breaks = []
        # traverse list twice, first deleting then re-adding to avoid stepping
        # on our own toes
        for brklineno, breaks in self.lines.items():
            if lineno < brklineno-1:
                del self.lines[brklineno]
                set_breaks.append( (brklineno+delta, breaks) )
        for brklineno, breaks in set_breaks:
            self.lines[brklineno] = breaks

    def enableBreakpoints(self, lineno, enable=1):
        if self.lines.has_key(lineno):
            linebreaks = self.lines[lineno]
            for brk in linebreaks:
                brk['enabled'] = enable

    def ignoreBreakpoints(self, lineno, ignore=0):
        if self.lines.has_key(lineno):
            linebreaks = self.lines[lineno]
            for brk in linebreaks:
                brk['ignore'] = ignore

    def conditionalBreakpoints(self, lineno, cond=''):
        if self.lines.has_key(lineno):
            linebreaks = self.lines[lineno]
            for brk in linebreaks:
                brk['cond'] = cond

    def listBreakpoints(self):
        rval = []
        for lineno, linebreaks in self.lines.items():
            for brk in linebreaks:
                brkinfo = {'lineno':lineno}
                brkinfo.update(brk)
                rval.append(brkinfo)
        return rval

    def hasBreakpoint(self, lineno, endlineno=-1):
        if endlineno < 0:
            return self.lines.has_key(lineno)
        else:
            for line in self.lines.keys():
                if line >= lineno and line <= endlineno:
                    return 1
            return 0

    def clearTemporaryBreakpoints(self, lineno):
        if self.lines.has_key(lineno):
            linebreaks = self.lines[lineno]
            idx = 0
            while idx < len(linebreaks):
                brk = linebreaks[idx]
                if brk['temporary']:
                    del linebreaks[idx]
                else:
                    idx = idx + 1

    def clearAllBreakpoints(self):
        self.lines = {}


class BreakpointList:
    def __init__(self):
        self.files = {}  # filename -> FileBreakpointList

    def normalize(self, filename):
        if filename.find('://') < 0:
            filename = 'file://' + filename
        return filename

    def addBreakpoint(self, filename, lineno, temp=0, cond='', ignore=0):
        filename = self.normalize(filename)
        filelist = self.getFileBreakpoints(filename)
        filelist.addBreakpoint(lineno, temp, cond, ignore)

    def deleteBreakpoints(self, filename, lineno):
        filename = self.normalize(filename)
        if self.files.has_key(filename):
            filelist = self.files[filename]
            filelist.deleteBreakpoints(lineno)

    def moveBreakpoint(self, filename, lineno, newlineno):
        filename = self.normalize(filename)
        if self.files.has_key(filename):
            filelist = self.files[filename]
            filelist.moveBreakpoint(lineno, newlineno)

    def adjustBreakpoints(self, filename, lineno, delta):
        if self.files.has_key(filename):
            filelist = self.files[filename]
            return filelist.adjustBreakpoints(lineno, delta)
        return 0

    def enableBreakpoints(self, filename, lineno, enable=1):
        filename = self.normalize(filename)
        if self.files.has_key(filename):
            filelist = self.files[filename]
            filelist.enableBreakpoints(lineno, enable)

    def ignoreBreakpoints(self, filename, lineno, ignore=0):
        filename = self.normalize(filename)
        if self.files.has_key(filename):
            filelist = self.files[filename]
            filelist.ignoreBreakpoints(lineno, ignore)

    def conditionalBreakpoints(self, filename, lineno, cond=''):
        filename = self.normalize(filename)
        if self.files.has_key(filename):
            filelist = self.files[filename]
            filelist.conditionalBreakpoints(lineno, cond)

    def clearTemporaryBreakpoints(self, filename, lineno):
        filename = self.normalize(filename)
        if self.files.has_key(filename):
            filelist = self.files[filename]
            filelist.clearTemporaryBreakpoints(lineno)

    def renameFileBreakpoints(self, oldname, newname):
        oldname = self.normalize(oldname)
        newname = self.normalize(newname)
        if self.files.has_key(oldname):
            filelist = self.files[oldname]
            filelist.clearAllBreakpoints()
            del self.files[oldname]
            self.files[newname] = filelist

    def getFileBreakpoints(self, filename):
        filename = self.normalize(filename)
        if self.files.has_key(filename):
            return self.files[filename]
        else:
            self.files[filename] = filelist = FileBreakpointList()
            return filelist

    def hasBreakpoint(self, filename, lineno, endlineno=-1):
        filename = self.normalize(filename)
        if self.files.has_key(filename):
            filelist = self.files[filename]
            return filelist.hasBreakpoint(lineno, endlineno)
        return 0

    def getBreakpointList(self, fn=None):
        """Returns a list designed to pass to the setAllBreakpoints()
        debugger method.

        The optional fn constrains the return value to breakpoints in
        a specified file."""
        rval = []
        if fn is not None:
            fn = self.normalize(fn)
        for filename, filelist in self.files.items():
            if fn is None or filename == fn:
                for lineno, linebreaks in filelist.lines.items():
                    for brk in linebreaks:
                        brkinfo = {'filename': filename,
                                   'lineno': lineno}
                        brkinfo.update(brk)
                        rval.append(brkinfo)
        return rval


# ??? Should this really be a global variable?
bplist = BreakpointList()