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

arigo at codespeak.net arigo at codespeak.net
Sat May 8 13:24:36 CEST 2004


Author: arigo
Date: Sat May  8 13:24:36 2004
New Revision: 4324

Modified:
   pypy/trunk/src/pypy/annotation/binaryop.py
   pypy/trunk/src/pypy/annotation/factory.py
   pypy/trunk/src/pypy/annotation/model.py
   pypy/trunk/src/pypy/annotation/test/test_model.py
   pypy/trunk/src/pypy/annotation/unaryop.py
   pypy/trunk/src/pypy/translator/annrpython.py
Log:
Added a generally useful function unionof(*somevalues).


Modified: pypy/trunk/src/pypy/annotation/binaryop.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/binaryop.py	(original)
+++ pypy/trunk/src/pypy/annotation/binaryop.py	Sat May  8 13:24:36 2004
@@ -7,7 +7,7 @@
 from pypy.annotation.model import SomeString, SomeList
 from pypy.annotation.model import SomeTuple, SomeImpossibleValue
 from pypy.annotation.model import SomeInstance, SomeFunction
-from pypy.annotation.model import set, setunion, missing_operation
+from pypy.annotation.model import unionof, set, setunion, missing_operation
 from pypy.annotation.factory import BlockedInference
 
 
@@ -70,7 +70,7 @@
 
     def union((lst1, lst2)):
         return SomeList(setunion(lst1.factories, lst2.factories),
-                        s_item = pair(lst1.s_item, lst2.s_item).union())
+                        s_item = unionof(lst1.s_item, lst2.s_item))
 
     add = union
 
@@ -84,7 +84,7 @@
         if len(tup1.items) != len(tup2.items):
             return SomeObject()
         else:
-            unions = [pair(x,y).union() for x,y in zip(tup1.items, tup2.items)]
+            unions = [unionof(x,y) for x,y in zip(tup1.items, tup2.items)]
             return SomeTuple(items = unions)
 
     def add((tup1, tup2)):
@@ -97,10 +97,7 @@
         if int2.is_constant():
             return tup1.items[int2.const]
         else:
-            result = SomeImpossibleValue()
-            for a in tup1.items:
-                result = pair(result, a).union()
-            return result
+            return unionof(*tup1.items)
 
 
 class __extend__(pairtype(SomeList, SomeInteger)):

Modified: pypy/trunk/src/pypy/annotation/factory.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/factory.py	(original)
+++ pypy/trunk/src/pypy/annotation/factory.py	Sat May  8 13:24:36 2004
@@ -10,6 +10,7 @@
 from pypy.annotation.pairtype import pair
 from pypy.annotation.model import SomeImpossibleValue, SomeList
 from pypy.annotation.model import SomeObject, SomeInstance
+from pypy.annotation.model import unionof
 from pypy.interpreter.miscutils import getthreadlocals
 
 
@@ -111,7 +112,7 @@
         return SomeList(factories = {self: True}, s_item = self.s_item)
 
     def generalize(self, s_new_item):
-        self.s_item = pair(self.s_item, s_new_item).union()
+        self.s_item = unionof(self.s_item, s_new_item)
 
 
 class FuncCallFactory:
@@ -188,10 +189,11 @@
         for clsdef in self.getmro():
             assert clsdef is self or attr not in clsdef.attrs
         # (2) remove the attribute from subclasses
+        subclass_values = []
         for subdef in self.getallsubdefs():
             if attr in subdef.attrs:
-                s_value = pair(s_value, subdef.attrs[attr]).union()
+                subclass_values.append(subdef.attrs[attr])
                 del subdef.attrs[attr]
             # bump the revision number of this class and all subclasses
             subdef.revision += 1
-        self.attrs[attr] = s_value
+        self.attrs[attr] = unionof(s_value, *subclass_values)

Modified: pypy/trunk/src/pypy/annotation/model.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/model.py	(original)
+++ pypy/trunk/src/pypy/annotation/model.py	Sat May  8 13:24:36 2004
@@ -111,6 +111,14 @@
     will never show up at run-time, e.g. elements of an empty list."""
 
 
+def unionof(*somevalues):
+    "The most precise SomeValue instance that contains all the values."
+    s1 = SomeImpossibleValue()
+    for s2 in somevalues:
+        if s1 != s2:
+            s1 = pair(s1, s2).union()
+    return s1
+
 def immutablevalue(x):
     "The most precise SomeValue instance that contains the immutable value x."
     if isinstance(bool, type) and isinstance(x, bool):

Modified: pypy/trunk/src/pypy/annotation/test/test_model.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/test/test_model.py	(original)
+++ pypy/trunk/src/pypy/annotation/test/test_model.py	Sat May  8 13:24:36 2004
@@ -30,7 +30,7 @@
                                                           (s6,s6)])
 
 def test_union():
-    assert ([pair(s,t).union() for s in slist for t in slist] ==
+    assert ([unionof(s,t) for s in slist for t in slist] ==
             [s1, s1, s1, s1, s1, s1,
              s1, s2, s3, s1, s1, s2,
              s1, s3, s3, s1, s1, s3,

Modified: pypy/trunk/src/pypy/annotation/unaryop.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/unaryop.py	(original)
+++ pypy/trunk/src/pypy/annotation/unaryop.py	Sat May  8 13:24:36 2004
@@ -9,7 +9,7 @@
 from pypy.annotation.model import SomeInstance, SomeBuiltin, SomeClass
 from pypy.annotation.model import SomeFunction
 from pypy.annotation.model import immutablevalue, decode_simple_call
-from pypy.annotation.model import set, setunion, missing_operation
+from pypy.annotation.model import unionof, set, setunion, missing_operation
 from pypy.annotation.factory import BlockedInference, getbookkeeper
 from pypy.annotation.factory import InstanceFactory, FuncCallFactory
 
@@ -28,10 +28,11 @@
     def is_true(obj):
         return SomeBool()
 
-    def getattr(obj, attr):
-        # get a SomeBuiltin if the object has a corresponding method
-        if attr.is_constant() and isinstance(attr.const, str):
-            attr = attr.const
+    def getattr(obj, s_attr):
+        # get a SomeBuiltin if the SomeObject has
+        # a corresponding method to handle it
+        if s_attr.is_constant() and isinstance(s_attr.const, str):
+            attr = s_attr.const
             if hasattr(obj, 'method_' + attr):
                 return SomeBuiltin(getattr(obj, 'method_' + attr))
         return SomeObject()
@@ -57,9 +58,9 @@
             raise BlockedInference()
         return ins.classdef
 
-    def getattr(ins, attr):
-        if attr.is_constant() and isinstance(attr.const, str):
-            attr = attr.const
+    def getattr(ins, s_attr):
+        if s_attr.is_constant() and isinstance(s_attr.const, str):
+            attr = s_attr.const
             # look for the attribute in the MRO order
             for clsdef in ins.currentdef().getmro():
                 if attr in clsdef.attrs:
@@ -70,9 +71,9 @@
             raise BlockedInference(clsdef.getallfactories())
         return SomeObject()
 
-    def setattr(ins, attr, s_value):
-        if attr.is_constant() and isinstance(attr.const, str):
-            attr = attr.const
+    def setattr(ins, s_attr, s_value):
+        if s_attr.is_constant() and isinstance(s_attr.const, str):
+            attr = s_attr.const
             for clsdef in ins.currentdef().getmro():
                 if attr in clsdef.attrs:
                     # look for the attribute in ins.classdef or a parent class
@@ -114,8 +115,5 @@
         arglist = decode_simple_call(args, kwds)
         assert arglist is not None
         factory = getbookkeeper().getfactory(FuncCallFactory)
-        s_result = SomeImpossibleValue()
-        for func in fun.funcs:
-            s_next_result = factory.pycall(func, arglist)
-            s_result = pair(s_result, s_next_result).union()
-        return s_result
+        results = [factory.pycall(func, arglist) for func in fun.funcs]
+        return unionof(*results)

Modified: pypy/trunk/src/pypy/translator/annrpython.py
==============================================================================
--- pypy/trunk/src/pypy/translator/annrpython.py	(original)
+++ pypy/trunk/src/pypy/translator/annrpython.py	Sat May  8 13:24:36 2004
@@ -209,7 +209,7 @@
         # Merge the new 'cells' with each of the block's existing input
         # variables.
         oldcells = [self.binding(a) for a in block.inputargs]
-        unions = [pair(c1,c2).union() for c1, c2 in zip(oldcells, inputcells)]
+        unions = [annmodel.unionof(c1,c2) for c1, c2 in zip(oldcells,inputcells)]
         # if the merged cells changed, we must redo the analysis
         if unions != oldcells:
             self.bindinputargs(block, unions)


More information about the Pypy-commit mailing list