[pypy-svn] r7772 - in pypy/trunk/src/pypy: annotation translator/test

arigo at codespeak.net arigo at codespeak.net
Tue Dec 7 17:24:13 CET 2004


Author: arigo
Date: Tue Dec  7 17:24:12 2004
New Revision: 7772

Modified:
   pypy/trunk/src/pypy/annotation/binaryop.py
   pypy/trunk/src/pypy/annotation/bookkeeper.py
   pypy/trunk/src/pypy/annotation/factory.py
   pypy/trunk/src/pypy/annotation/model.py
   pypy/trunk/src/pypy/annotation/unaryop.py
   pypy/trunk/src/pypy/translator/test/snippet.py
   pypy/trunk/src/pypy/translator/test/test_annrpython.py
Log:
Modified SomeDict() to represent a dictionary with variable keys, but whose 
keys are of a consistent types, and whose values idem.

Disabled the now-failing test_knownkeysdict() and replaced it with a 
test_generaldict().



Modified: pypy/trunk/src/pypy/annotation/binaryop.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/binaryop.py	(original)
+++ pypy/trunk/src/pypy/annotation/binaryop.py	Tue Dec  7 17:24:12 2004
@@ -197,27 +197,18 @@
 class __extend__(pairtype(SomeDict, SomeDict)):
 
     def union((dic1, dic2)):
-        result = dic1.items.copy()
-        for key, s_value in dic2.items.items():
-            if key in result:
-                result[key] = unionof(result[key], s_value)
-            else:
-                result[key] = s_value
-        return SomeDict(setunion(dic1.factories, dic2.factories), result)
+        return SomeDict(setunion(dic1.factories, dic2.factories),
+                        unionof(dic1.s_key, dic2.s_key),
+                        unionof(dic1.s_value, dic2.s_value))
 
 
 class __extend__(pairtype(SomeDict, SomeObject)):
 
     def getitem((dic1, obj2)):
-        if obj2.is_constant():
-            return dic1.items.get(obj2.const, SomeImpossibleValue())
-        else:
-            return unionof(*dic1.items.values())
+        return dic1.s_value
 
     def setitem((dic1, obj2), s_value):
-        assert obj2.is_constant()
-        key = obj2.const
-        generalize(dic1.factories, key, s_value)
+        generalize(dic1.factories, obj2, s_value)
 
 
 class __extend__(pairtype(SomeTuple, SomeInteger)):

Modified: pypy/trunk/src/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/bookkeeper.py	(original)
+++ pypy/trunk/src/pypy/annotation/bookkeeper.py	Tue Dec  7 17:24:12 2004
@@ -85,10 +85,9 @@
             items_s = [self.immutablevalue(e) for e in x]
             result = SomeList({}, unionof(*items_s))
         elif tp is dict:   # exactly a dict
-            items = {}
-            for key, value in x.items():
-                items[key] = self.immutablevalue(value)
-            result = SomeDict({}, items)
+            keys_s   = [self.immutablevalue(e) for e in x.keys()]
+            values_s = [self.immutablevalue(e) for e in x.values()]
+            result = SomeDict({}, unionof(*keys_s), unionof(*values_s))
         elif ishashable(x) and x in BUILTIN_ANALYZERS:
             result = SomeBuiltin(BUILTIN_ANALYZERS[x])
         elif callable(x) or isinstance(x, staticmethod): # XXX

Modified: pypy/trunk/src/pypy/annotation/factory.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/factory.py	(original)
+++ pypy/trunk/src/pypy/annotation/factory.py	Tue Dec  7 17:24:12 2004
@@ -44,24 +44,25 @@
 
 
 class DictFactory:
-    items = {}
+    s_key   = SomeImpossibleValue()
+    s_value = SomeImpossibleValue()
 
     def __repr__(self):
-        return '%s(items=%r)' % (self.__class__.__name__, self.items)
+        return '%s(s_key=%r, s_value=%r)' % (self.__class__.__name__,
+                                             self.s_key, self.s_value)
 
     def create(self):
-        return SomeDict(factories = {self: True}, items = self.items)
-
-    def generalize(self, key, s_new_value):
-        self.items = self.items.copy()
-        if key not in self.items:
-            self.items[key] = s_new_value
-            return True
-        elif not self.items[key].contains(s_new_value):
-            self.items[key] = unionof(self.items[key], s_new_value)
-            return True
-        else:
+        return SomeDict(factories = {self: True},
+                        s_key     = self.s_key,
+                        s_value   = self.s_value)
+
+    def generalize(self, s_new_key, s_new_value):
+        if (self.s_key.contains(s_new_key) and
+            self.s_value.contains(s_new_value)):
             return False
+        self.s_key   = unionof(self.s_key,   s_new_key)
+        self.s_value = unionof(self.s_value, s_new_value)
+        return True
 
 
 def generalize(factories, *args):

Modified: pypy/trunk/src/pypy/annotation/model.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/model.py	(original)
+++ pypy/trunk/src/pypy/annotation/model.py	Tue Dec  7 17:24:12 2004
@@ -142,11 +142,12 @@
 
 
 class SomeDict(SomeObject):
-    "Stands for a dict with known keys."
+    "Stands for a dict."
     knowntype = dict
-    def __init__(self, factories, items):
+    def __init__(self, factories, s_key, s_value):
         self.factories = factories
-        self.items = items    # dict {realkey: s_value}
+        self.s_key = s_key
+        self.s_value = s_value
 
 
 class SomeIterator(SomeObject):

Modified: pypy/trunk/src/pypy/annotation/unaryop.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/unaryop.py	(original)
+++ pypy/trunk/src/pypy/annotation/unaryop.py	Tue Dec  7 17:24:12 2004
@@ -80,11 +80,6 @@
     def iter(tup):
         return SomeIterator(unionof(*tup.items))
 
-class __extend__(SomeDict):
-
-    def len(dic):
-        return immutablevalue(len(dic.items))
-
 
 class __extend__(SomeList):
 

Modified: pypy/trunk/src/pypy/translator/test/snippet.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/snippet.py	(original)
+++ pypy/trunk/src/pypy/translator/test/snippet.py	Tue Dec  7 17:24:12 2004
@@ -373,6 +373,11 @@
         d = {'b': -123}
     return d['b']
 
+def generaldict(key=str, value=int, key2=str, value2=int):
+    d = {key: value}
+    d[key2] = value2
+    return d[key or key2]
+
 def prime(n=int):
     return len([i for i in range(1,n+1) if n%i==0]) == 2
 

Modified: pypy/trunk/src/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_annrpython.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_annrpython.py	Tue Dec  7 17:24:12 2004
@@ -199,14 +199,20 @@
         self.assertEquals(classes[snippet.H].attrs.keys(), ['attr']) 
         self.assertEquals(classes[snippet.H].about_attribute('attr'),
                           a.bookkeeper.immutablevalue(1))
-       
 
-    def test_knownkeysdict(self):
+    def DISABLED_test_knownkeysdict(self):
+        # disabled, SomeDict() is now a general {s_key: s_value} dict
         a = RPythonAnnotator()
         s = a.build_types(snippet.knownkeysdict, [int])
         # result should be an integer
         self.assertEquals(s.knowntype, int)
 
+    def test_generaldict(self):
+        a = RPythonAnnotator()
+        s = a.build_types(snippet.generaldict, [str, int, str, int])
+        # result should be an integer
+        self.assertEquals(s.knowntype, int)
+
     def test_somebug1(self):
         a = RPythonAnnotator()
         s = a.build_types(snippet._somebug1, [int])



More information about the Pypy-commit mailing list