[py-svn] r32906 - in py/branch/apigen/py/test/tracer: . testing

fijal at codespeak.net fijal at codespeak.net
Thu Oct 5 13:22:30 CEST 2006


Author: fijal
Date: Thu Oct  5 13:22:09 2006
New Revision: 32906

Added:
   py/branch/apigen/py/test/tracer/description.py   (contents, props changed)
Modified:
   py/branch/apigen/py/test/tracer/   (props changed)
   py/branch/apigen/py/test/tracer/testing/   (props changed)
   py/branch/apigen/py/test/tracer/testing/test_docgen.py
   py/branch/apigen/py/test/tracer/tracer.py
Log:
Added simple test for using pypy's annotation model.


Added: py/branch/apigen/py/test/tracer/description.py
==============================================================================
--- (empty file)
+++ py/branch/apigen/py/test/tracer/description.py	Thu Oct  5 13:22:09 2006
@@ -0,0 +1,13 @@
+
+from pypy.annotation.description import FunctionDesc
+from pypy.annotation.model import unionof
+
+#class __extend__(FunctionDesc):
+def consider_call(self, inputcells):
+    if not hasattr(self, 'inputcells'):
+        self.inputcells = inputcells
+        return
+    for cell_num, cell in enumerate(inputcells):
+        self.inputcells[cell_num] = unionof(cell, self.inputcells[cell_num])
+
+FunctionDesc.consider_call = consider_call

Modified: py/branch/apigen/py/test/tracer/testing/test_docgen.py
==============================================================================
--- py/branch/apigen/py/test/tracer/testing/test_docgen.py	(original)
+++ py/branch/apigen/py/test/tracer/testing/test_docgen.py	Thu Oct  5 13:22:09 2006
@@ -6,6 +6,7 @@
 import sys
 
 from py.__.test.tracer.tracer import DocStorage, Tracer
+from pypy.annotation import model
 
 #def setup_module(mod):
 #    data_path = py.path.local(mod.__file__).dirpath().join("data")
@@ -16,14 +17,14 @@
 
 def test_basic():
     descs = {"fun":fun}
-    ds = DocStorage.from_dict(descs)
+    ds = DocStorage().from_dict(descs)
     t = Tracer(ds)
     t.start_tracing()
     fun(1, ("g", 3), 8)
+    fun(2., ("a", 1.), "a")
     t.end_tracing()
-    funcdesc = ds.descs[0]
-    assert funcdesc.fullname == 'fun'
-    assert len(funcdesc.args) == 3
-    assert funcdesc.args['a']._type is int
-    assert funcdesc.args['b']._type is tuple
-    assert funcdesc.args['c']._type is int
+    inputcells = ds.descs['fun'].inputcells
+    assert len(inputcells) == 3
+    assert isinstance(inputcells[0], model.SomeFloat)
+    assert isinstance(inputcells[1], model.SomeTuple)
+    assert isinstance(inputcells[2], model.SomeObject)

Modified: py/branch/apigen/py/test/tracer/tracer.py
==============================================================================
--- py/branch/apigen/py/test/tracer/tracer.py	(original)
+++ py/branch/apigen/py/test/tracer/tracer.py	Thu Oct  5 13:22:09 2006
@@ -10,95 +10,54 @@
 # from pypy's code.
 # BLAH BLAH BLAH
 
+from pypy.annotation.bookkeeper import Bookkeeper
+from pypy.annotation.policy import AnnotatorPolicy
+from py.__.test.tracer.description import FunctionDesc
+
+class DummyAnnotator(object):
+    policy = AnnotatorPolicy()
+
 class UnionError(Exception):
     pass
 
-class TypeDesc(object):
-    def __init__(self, _type):
-        self._type = _type
-    
-    def generalize(self, _type):
-        if self._type is _type:
-            return
-        raise UnionError("%r and %r incompatible" % (self._type, _type))
-
-class FuncDesc(object):
-    """ XXX This is totally random description used for testing.
-    We'll need descriptions for all tracked object types
-    """
-    def __init__(self, fullname, obj):
-        self.fullname = fullname
-        self.obj = obj
-        self.args = {}
-    
-    def check_is(self, frame):
-        return self.obj.func_code is frame.code.raw
-    
-    def __eq__(self, other):
-        return self.fullname == other.fullname and self.obj is other.obj
-    
-    def __ne__(self, other):
-        return not self.__eq__(other)
-    
-    def __hash__(self):
-        return hash(self.fullname)
-    
-    def consider_call(self, frame):
-        assert isinstance(frame, py.code.Frame)
-        for arg in frame.code.getargs():
-            self.setarg(arg, frame.f_locals[arg])
-    
-    def setarg(self, arg, value):
-        # set argument annotation
-        if arg in self.args:
-            self.args[arg].generalize(type(value))
-        else:
-            self.args[arg] = TypeDesc(type(value))
-
-def make_desc(fullname, obj):
-    # XXX hash it at some point
-    if type(obj) is types.FunctionType:
-        return FuncDesc(fullname, obj)
-    raise NotImplementedError("%r type has no description" % type(obj))
-
 class DocStorage(object):
     """ Class storing info about API
     """
-    def __init__(self, descs):
-        self.descs = descs
+    def __init__(self):
+        self.bookkeeper = Bookkeeper(DummyAnnotator())
+        #self.call_stack = []
     
     def consider_call(self, frame):
-        # check if we have available description for that call
-        self.frame = frame
-        desc = self.check_desc(frame)
-        if not desc:
-            return
-        desc.consider_call(frame)
-    
+        assert isinstance(frame, py.code.Frame)
+        desc = self.find_desc(frame)
+        if desc:
+            self.generalize_args(desc, frame)
+    
+    def generalize_args(self, desc, frame):
+        args = [arg for key, arg in frame.getargs()]
+        #self.call_stack.append((desc, args))
+        desc.consider_call([self.bookkeeper.immutablevalue(arg) for arg in args])
+
     def consider_return(self, frame, arg):
         pass
-    
-    def check_desc(self, frame):
-        assert isinstance(frame, py.code.Frame)
-        for desc in self.descs:
-            if desc.check_is(frame):
+
+    def find_desc(self, frame):
+        for desc in self.descs.values():
+            if desc.pyobj.func_code is frame.code.raw:
                 return desc
         return None
     
+    def from_dict(self, _dict):
+        self.descs = dict([self.make_desc(key, val) for key, val in _dict.iteritems()])
+        return self
+    
+    def make_desc(self, key, value):
+        desc = self.bookkeeper.getdesc(value)
+        return (key, desc)
+    
     def from_pkg(self, module):
-        """ Initialize DocStorage from module.__package__
-        """
-        # all elements should be stored in some kind of dictionary.
-        # they're usually not hashable, so we are creating some
-        # descriptions of them
-        descs = [make_desc(key, val) for key, val in 
-            module.__package__.__dict__.iteritems()]
-        return DocStorage(descs)
-    from_pkg = staticmethod(from_pkg)
-    
-    def from_dict(_dict):
-        return DocStorage([make_desc(key, val) for key, val in _dict.iteritems()])
-    from_dict = staticmethod(from_dict)
+        self.from_dict(module.__package__.__dict__)
+        return self
 
 class Tracer(object):
     """ Basic tracer object, used for gathering additional info


More information about the py-svn mailing list