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

arigo at codespeak.net arigo at codespeak.net
Thu Dec 18 11:57:13 CET 2003


Author: arigo
Date: Thu Dec 18 11:57:13 2003
New Revision: 2479

Modified:
   pypy/trunk/src/pypy/annotation/annset.py
   pypy/trunk/src/pypy/annotation/test/test_annset.py
Log:
Last few changes before we think that dependencies are a possibly bad idea
after all.


Modified: pypy/trunk/src/pypy/annotation/annset.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/annset.py	(original)
+++ pypy/trunk/src/pypy/annotation/annset.py	Thu Dec 18 11:57:13 2003
@@ -15,6 +15,14 @@
         self.annlist = list(annlist)    # List of annotations
         self._normalized = {}  # maps SomeValues to some 'standard' one that
                                # is shared with it
+        self.mappings_to_normalize = [self._normalized]
+
+    def getbindings(self):
+        """Return a general-purpose mapping between whatever you want and
+        SomeValues.  The SomeValues are kept normalized by the AnnotationSet."""
+        bindings = {}
+        self.mappings_to_normalize.append(bindings)
+        return bindings
 
     def normalized(self, someval):
         return self._normalized.get(someval, someval)
@@ -22,10 +30,11 @@
     def setshared(self, someval1, someval2):
         someval1 = self.normalized(someval1)
         someval2 = self.normalized(someval2)
-        for key, value in self._normalized.items():
-            if value is someval1:
-                self._normalized[key] = someval2
-        self._normalized[someval1] = someval2
+        for mapping in self.mappings_to_normalize:
+            for key, value in mapping.items():
+                if value is someval2:
+                    mapping[key] = someval1
+        self._normalized[someval2] = someval1
     
     def isshared(self, someval1, someval2):
         return self.normalized(someval1) is self.normalized(someval2)
@@ -96,6 +105,10 @@
         else:
             return None
 
+    def findall(self, checkann):
+        """ list all matching annotations."""
+        return list(self._annmatches(checkann))
+
     def queryconstant(self, cell):
         "Return the list of all 'x' such that ANN.constant(x)[cell] is set."
         cell = self.normalized(cell)
@@ -250,6 +263,16 @@
             results.append(matchvalue)
         return results
 
+    def get(self, *querylist):
+        """Like query() but asserts that there is at most one answer.
+        Returns None if there isn't any answer."""
+        resultlist = self.query(*querylist)
+        assert len(resultlist) <= 1, "Confusing annotations..."
+        if resultlist:
+            return resultlist[0]
+        else:
+            return None
+
     def set(self, ann):
         """Insert the annotation into the AnnotationSet, recording dependency
         from all previous queries done on this Recorder instance."""
@@ -258,11 +281,23 @@
         for previous_ann in self.using_annotations:
             self.annset.adddependency(previous_ann, ann)
 
-    def check_type(self, someval, checktype):
-        return bool(self.query(ANN.type[someval, QUERYARG],
-                               ANN.constant(checktype)[QUERYARG]))
+    def delete(self, queryann):
+        """Kill the annotations matching the pattern."""
+        matchannlist = self.annset.findall(queryann)
+        self.annset.simplify(kill=matchannlist)
+
+    def checktype(self, someval, checktype):
+        if isinstance(checktype, tuple):
+            for t in checktype:
+                if self.checktype(someval, t):
+                    return True
+            else:
+                return False
+        else:
+            return bool(self.query(ANN.type[someval, QUERYARG],
+                                   ANN.constant(checktype)[QUERYARG]))
 
-    def set_type(self, someval, knowntype):
+    def settype(self, someval, knowntype):
         typeval = SomeValue()
         self.set(ANN.type[someval, typeval])
         self.set(ANN.constant(knowntype)[typeval])

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	Thu Dec 18 11:57:13 2003
@@ -307,11 +307,27 @@
     def test_type(self):
         a = self.annset
         def f(rec):
-            if rec.check_type(c1, int):
-                rec.set_type(c2, str)
+            if rec.checktype(c1, int):
+                rec.settype(c2, str)
         a.record(f)
         self.assert_(a.query(ANN.type[c2, QUERYARG],
                              ANN.constant(str)[QUERYARG]))
 
+    def test_type2(self):
+        a = self.annset
+        def f(rec):
+            if rec.checktype(c1, (int, long)):
+                rec.settype(c2, str)
+        a.record(f)
+        self.assert_(a.query(ANN.type[c2, QUERYARG],
+                             ANN.constant(str)[QUERYARG]))
+
+    def test_delete(self):
+        a = self.annset
+        def f(rec):
+            rec.delete(ANN.add[c1, c3, ...])
+        a.record(f)
+        self.assertSameSet(a, self.lst[1:])
+
 if __name__ == '__main__':
     test.main()


More information about the Pypy-commit mailing list