sources for description.py [rev. 38799]
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
import py
from py.__.apigen.tracer import model
from py.__.code.source import getsource
import types
import inspect
import copy
MAX_CALL_SITES = 20
set = py.builtin.set
def is_private(name):
    return name.startswith('_') and not name.startswith('__')
class CallFrame(object):
    def __init__(self, frame):
        self.filename = frame.code.raw.co_filename
        self.lineno = frame.lineno
        self.firstlineno = frame.code.firstlineno
        try:
            self.source = getsource(frame.code.raw)
        except IOError:
            self.source = "could not get to source"
    def _getval(self):
        return (self.filename, self.lineno)
    def __hash__(self):
        return hash(self._getval())
    def __eq__(self, other):
        return self._getval() == other._getval()
    def __ne__(self, other):
        return not self == other
class CallStack(object):
    def __init__(self, tb):
        #if isinstance(tb, py.code.Traceback):
        #    self.tb = tb
        #else:
        #    self.tb = py.code.Traceback(tb)
        self.tb = [CallFrame(frame) for frame in tb]
    
    #def _getval(self):
    #    return [(frame.code.raw.co_filename, frame.lineno+1) for frame
    #        in self]
    
    def __hash__(self):
        return hash(tuple(self.tb))
    
    def __eq__(self, other):
        return self.tb == other.tb
    
    def __ne__(self, other):
        return not self == other
    
    #def __getattr__(self, attr):
    #    return getattr(self.tb, attr)
    
    def __iter__(self):
        return iter(self.tb)
    
    def __getitem__(self, item):
        return self.tb[item]
    
    def __len__(self):
        return len(self.tb)
    
    def __cmp__(self, other):
        return cmp(self.tb, other.tb)
def cut_stack(stack, frame, upward_frame=None):
    if hasattr(frame, 'raw'):
        frame = frame.raw
    if upward_frame:
        if hasattr(upward_frame, 'raw'):
            upward_frame = upward_frame.raw
        lst = [py.code.Frame(i) for i in stack[stack.index(frame):\
                stack.index(upward_frame)+1]]
        if len(lst) > 1:
            return CallStack(lst[:-1])
        return CallStack(lst)
    return CallStack([py.code.Frame(i) for i in stack[stack.index(frame):]])
##class CallSite(object):
##    def __init__(self, filename, lineno):
##        self.filename = filename
##        self.lineno = lineno
##    
##    def get_tuple(self):
##        return self.filename, self.lineno
##    
##    def __hash__(self):
##        return hash((self.filename, self.lineno))
##    
##    def __eq__(self, other):
##        return (self.filename, self.lineno) == (other.filename, other.lineno)
##    
##    def __ne__(self, other):
##        return not self == other
##    
##    def __cmp__(self, other):
##        if self.filename < other.filename:
##            return -1
##        if self.filename > other.filename:
##            return 1
##        if self.lineno < other.lineno:
##            return -1
##        if self.lineno > other.lineno:
##            return 1
##        return 0
    
class NonHashableObject(object):
    def __init__(self, cls):
        self.cls = cls
    
    def __hash__(self):
        raise NotImplementedError("Object of type %s are unhashable" % self.cls)
class Desc(object):
    def __init__(self, name, pyobj, **kwargs):
        self.pyobj = pyobj
        self.is_degenerated = False
        self.name = name
        if type(self) is Desc:
            # do not override property...
            self.code = NonHashableObject(self.__class__) # dummy think that makes code unhashable
    # we make new base class instead of using pypy's one because
    # of type restrictions of pypy descs
    
    def __hash__(self):
        return hash(self.code)
    
    def __eq__(self, other):
        if isinstance(other, Desc):
            return self.code == other.code
        if isinstance(other, types.CodeType):
            return self.code == other
        if isinstance(other, tuple) and len(other) == 2:
            return self.code == other
        return False
    
    def __ne__(self, other):
        return not self == other
    # This set of functions will not work on Desc, because we need to
    # define code somehow
class FunctionDesc(Desc):
    def __init__(self, *args, **kwargs):
        super(FunctionDesc, self).__init__(*args, **kwargs)
        self.inputcells = [model.s_ImpossibleValue for i in xrange(self.\
            code.co_argcount)]
        self.call_sites = {}
        self.keep_frames = kwargs.get('keep_frames', False)
        self.frame_copier = kwargs.get('frame_copier', lambda x:x)
        self.retval = model.s_ImpossibleValue
        self.exceptions = {}
    
    def consider_call(self, inputcells):
        for cell_num, cell in enumerate(inputcells):
            self.inputcells[cell_num] = model.unionof(cell, self.inputcells[cell_num])
    def consider_call_site(self, frame, cut_frame):
        if len(self.call_sites) > MAX_CALL_SITES:
            return
        stack = [i[0] for i in inspect.stack()]
        cs = cut_stack(stack, frame, cut_frame)
        self.call_sites[cs] = cs
    
    def consider_exception(self, exc, value):
        self.exceptions[exc] = True
    
    def get_call_sites(self):
        # convinient accessor for various data which we keep there
        if not self.keep_frames:
            return [(key, val) for key, val in self.call_sites.iteritems()]
        else:
            lst = []
            for key, val in self.call_sites.iteritems():
                for frame in val:
                    lst.append((key, frame))
            return lst
    
    def consider_return(self, arg):
        self.retval = model.unionof(arg, self.retval)
    def consider_start_locals(self, frame):
        pass
    def consider_end_locals(self, frame):
        pass
    
    def getcode(self):
        return self.pyobj.func_code
    code = property(getcode)
    
    def get_local_changes(self):
        return {}
    
class ClassDesc(Desc):
    def __init__(self, *args, **kwargs):
        super(ClassDesc, self).__init__(*args, **kwargs)
        self.fields = {}
        # we'll gather informations about methods and possibly
        # other variables encountered here
    
    def getcode(self):
        # This is a hack. We're trying to return as much close to __init__
        # of us as possible, but still hashable object
        if hasattr(self.pyobj, '__init__'):
            if hasattr(self.pyobj.__init__, 'im_func') and \
                hasattr(self.pyobj.__init__.im_func, 'func_code'):
                result = self.pyobj.__init__.im_func.func_code
            else:
                result = self.pyobj.__init__
        else:
            result = self.pyobj
        try:
            hash(result)
        except KeyboardInterrupt, SystemExit:
            raise
        except: # XXX UUuuuu bare except here. What can it really rise???
            try:
                hash(self.pyobj)
                result = self.pyobj
            except:
                result = self
        return result
    code = property(getcode)
    
    def consider_call(self, inputcells):
        if '__init__' in self.fields:
            md = self.fields['__init__']
        else:
            md = MethodDesc(self.name + '.__init__', self.pyobj.__init__)
            self.fields['__init__'] = md
        md.consider_call(inputcells)
    
    def consider_return(self, arg):
        pass # we *know* what return value we do have
    
    def consider_exception(self, exc, value):
        if '__init__' in self.fields:
            md = self.fields['__init__']
        else:
            md = MethodDesc(self.name + '.__init__', self.pyobj.__init__)
            self.fields['__init__'] = md
        md.consider_exception(exc, value)
    def consider_start_locals(self, frame):
        if '__init__' in self.fields:
            md = self.fields['__init__']
            md.consider_start_locals(frame)
    def consider_end_locals(self, frame):
        if '__init__' in self.fields:
            md = self.fields['__init__']
            md.consider_end_locals(frame)
    
    def consider_call_site(self, frame, cut_frame):
        self.fields['__init__'].consider_call_site(frame, cut_frame)
    
    def add_method_desc(self, name, methoddesc):
        self.fields[name] = methoddesc
    
    def getfields(self):
        # return fields of values that has been used
        l = [i for i, v in self.fields.iteritems() if not is_private(i)]
        return l
    def getbases(self):
        bases = []
        tovisit = [self.pyobj]
        while tovisit:
            current = tovisit.pop()
            if current is not self.pyobj:
                bases.append(current)
            tovisit += [b for b in current.__bases__ if b not in bases]
        return bases
    bases = property(getbases)
    
##    def has_code(self, code):
##        # check __init__ method
##        return self.pyobj.__init__.im_func.func_code is code
##    
##    def consider_call(self, inputcells):
##        # special thing, make MethodDesc for __init__
##        
##
class MethodDesc(FunctionDesc):
    def __init__(self, *args, **kwargs):
        super(MethodDesc, self).__init__(*args, **kwargs)
        self.old_dict = {}
        self.changeset = {}
    # right now it's not different than method desc, only code is different
    def getcode(self):
        return self.pyobj.im_func.func_code
    code = property(getcode)
##    def has_code(self, code):
##        return self.pyobj.im_func.func_code is code
    def __hash__(self):
        return hash((self.code, self.pyobj.im_class))
    
    def __eq__(self, other):
        if isinstance(other, tuple):
            return self.code is other[0] and self.pyobj.im_class is other[1]
        if isinstance(other, MethodDesc):
            return self.pyobj is other.pyobj
        return False
    def consider_start_locals(self, frame):
        # XXX recursion issues?
        obj = frame.f_locals[self.pyobj.im_func.func_code.co_varnames[0]]
        try:
            if not obj:
                # static method
                return
        except AttributeError:
            return
        self.old_dict = self.perform_dict_copy(obj.__dict__)
    def perform_dict_copy(self, d):
        if d is None:
            return {}
        return d.copy()
    def consider_end_locals(self, frame):
        obj = frame.f_locals[self.pyobj.im_func.func_code.co_varnames[0]]
        try:
            if not obj:
                # static method
                return
        except AttributeError:
            return
        # store the local changes
        # update self.changeset
        self.update_changeset(obj.__dict__)
    def get_local_changes(self):
        return self.changeset
    
    def set_changeset(changeset, key, value):
        if key not in changeset:
            changeset[key] = set([value])
        else:
            changeset[key].add(value)
    set_changeset = staticmethod(set_changeset)
    
    def update_changeset(self, new_dict):
        changeset = self.changeset
        for k, v in self.old_dict.iteritems():
            if k not in new_dict:
                self.set_changeset(changeset, k, "deleted")
            elif new_dict[k] != v:
                self.set_changeset(changeset, k, "changed")
        for k, v in new_dict.iteritems():
            if k not in self.old_dict:
                self.set_changeset(changeset, k, "created")
        return changeset