[pypy-svn] rev 2270 - in pypy/trunk/src/pypy/annotation: . test

arigo at codespeak.net arigo at codespeak.net
Wed Nov 26 23:47:27 CET 2003


Author: arigo
Date: Wed Nov 26 23:47:26 2003
New Revision: 2270

Modified:
   pypy/trunk/src/pypy/annotation/annset.py
   pypy/trunk/src/pypy/annotation/model.py
   pypy/trunk/src/pypy/annotation/test/test_annset.py
Log:
intermediate checkin

- added a constant-predicate
- added get_type/check_type methods to annset.py 
- added intvalue, strvalue, etc. but don't hold your
  breath, next check-in will probably remove them anyway :-)

(arminl holgger on laggy screen ssseeeession )


Modified: pypy/trunk/src/pypy/annotation/annset.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/annset.py	(original)
+++ pypy/trunk/src/pypy/annotation/annset.py	Wed Nov 26 23:47:26 2003
@@ -1,13 +1,14 @@
 from __future__ import generators
 import types
-from model import Annotation, SomeValue, blackholevalue
+from model import Annotation, SomeValue, ann
+from model import immutable_types, blackholevalue, basicannotations
 
 class AnnotationSet:
     """An annotation set is a (large) family of Annotations."""
 
     # XXX STORED AS A PLAIN LIST, THE COMPLEXITY IS PLAINLY WRONG
 
-    def __init__(self, annlist=[]):
+    def __init__(self, annlist=basicannotations):  
         self.annlist = list(annlist)    # List of annotations
         self._shared = {}
         self.forward_deps = {}
@@ -170,6 +171,18 @@
         for previous_ann in self.using_annotations:
             deps = self.annset.forward_deps.setdefault(previous_ann, [])
             deps.append(ann)
+
+    def check_type(self, someval, checktype):
+        return self.query(ann.type[someval, ...],
+                          ann.constant(checktype)[...])
+
+    def set_type(self, someval, knowntype):
+        typeval = SomeValue()
+        self.set(ann.type[someval, typeval])
+        self.set(ann.constant(knowntype)[typeval])
+        if knowntype in immutable_types:
+            self.set(ann.immutable[someval])
+
 '''
     def merge(self, oldcell, newcell):
         """Update the heap to account for the merging of oldcell and newcell.

Modified: pypy/trunk/src/pypy/annotation/model.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/model.py	(original)
+++ pypy/trunk/src/pypy/annotation/model.py	Wed Nov 26 23:47:26 2003
@@ -1,17 +1,19 @@
+import types
 
 class SomeValue:
     pass
 
-# a conventional value for representing 'all Annotations match this one' 
-blackholevalue = SomeValue()
-
-def debugname(someval, name=None, _seen = {id(blackholevalue): 'blackholevalue'}):
+def debugname(someval, _seen = {}):
     """ return a simple name for a SomeValue. """
     try:
         return _seen[id(someval)]
     except KeyError:
-        if name is None:
-            name = "X%d" % len(seen)
+        if not _seen:
+            for name, value in globals().items():
+                if isinstance(value, SomeValue):
+                    _seen[id(value)] = name
+            return debugname(someval)
+        name = "X%d" % len(seen)
         _seen[id(someval)] = name
         return name
 
@@ -26,9 +28,23 @@
     def __str__(self):
         return self.name
 
+class ConstPredicate(Predicate):
+    def __init__(self, value):
+        Predicate.__init__(self, 'const%s' % value, 1)
+        self.value = value
+    def __eq__(self, other):
+        return self.__class__ is other.__class__ and self.value == other.value
+    def __ne__(self, other):
+        return not (self == other)
+    def __hash__(self):
+        return hash(self.value)
+
 class ann:
     add = Predicate('add', 3)
     snuff = Predicate('snuff', 2)   # for testing, to remove :-)
+    constant = ConstPredicate
+    type = Predicate('type', 2)
+    immutable = Predicate('immutable', 1)
 
 class Annotation:
     """An Annotation asserts something about SomeValues.  
@@ -52,3 +68,24 @@
                 self.predicate, ", ".join(map(repr, self.args)))
 
 
+immutable_types = {
+    int: 'int',
+    long: 'long',
+    tuple: 'tuple',
+    str: 'str',
+    bool: 'bool',
+    types.FunctionType: 'function',
+    }
+
+# a conventional value for representing 'all Annotations match this one' 
+blackholevalue = SomeValue()
+
+# a few values representing 'any value of the given type'
+# the following loops creates intvalue, strvalue, etc.
+basicannotations = []
+for _type, _name in immutable_types.items():
+    _val = globals()['%svalue' % _name] = SomeValue()
+    _tval = SomeValue()
+    basicannotations.append(ann.type[_val, _tval])
+    basicannotations.append(ann.constant(_type)[_tval])
+    basicannotations.append(ann.immutable[_val])

Modified: pypy/trunk/src/pypy/annotation/test/test_annset.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/test/test_annset.py	(original)
+++ pypy/trunk/src/pypy/annotation/test/test_annset.py	Wed Nov 26 23:47:26 2003
@@ -64,8 +64,27 @@
                     ann.snuff[..., c3])
         self.assertEquals(c, [c2])
 
+    def test_constant(self):
+        c1,c2,c3 = SomeValue(), SomeValue(), SomeValue()
+        lst = [
+            ann.constant(42)[c1],
+        ]
+        a = AnnotationSet(lst)
+        c = a.query(ann.constant(42)[...])
+        self.assertEquals(c, [c1])
+
+c1,c2,c3,c4 = SomeValue(), SomeValue(), SomeValue(), SomeValue()
+
 class TestRecording(test.IntTestCase):
 
+    def setUp(self):
+        self.lst = [
+            ann.add[c1, c3, c2],
+            ann.type[c1, c4],
+            ann.constant(int)[c4],
+        ]
+        self.annset = AnnotationSet(self.lst)
+
     def assertSameSet(self, annset, a, b):
         a = [annset.temporarykey(a1) for a1 in a]
         b = [annset.temporarykey(b1) for b1 in b]
@@ -81,19 +100,24 @@
         self.assertEquals(a, b)
 
     def test_simple(self):
-        c1,c2,c3 = SomeValue(), SomeValue(), SomeValue()
-        lst = [
-            ann.add[c1, c3, c2],
-        ]
-        a = AnnotationSet(lst)
+        a = self.annset
         def f(rec):
             if rec.query(ann.add[c1, c3, ...]):
                 rec.set(ann.snuff[c1, c3]) 
         a.record(f)
-        self.assertSameSet(a, a, lst + [ann.snuff[c1, c3]])
+        self.assertSameSet(a, a, self.lst + [ann.snuff[c1, c3]])
+
+        a.kill(self.lst[0])
+        self.assertSameSet(a, a, self.lst[1:])
 
-        a.kill(lst[0])
-        self.assertSameSet(a, a, [])
+    def test_type(self):
+        a = self.annset
+        def f(rec):
+            if rec.check_type(c1, int):
+                rec.set_type(c2, str)
+        a.record(f)
+        self.assert_(a.query(ann.type[c2, ...],
+                             ann.constant(str)[...]))
 
 if __name__ == '__main__':
     test.main()


More information about the Pypy-commit mailing list