[pypy-svn] r18117 - in pypy/dist/pypy: annotation translator translator/test

pedronis at codespeak.net pedronis at codespeak.net
Mon Oct 3 23:14:02 CEST 2005


Author: pedronis
Date: Mon Oct  3 23:13:59 2005
New Revision: 18117

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/translator/annrpython.py
   pypy/dist/pypy/translator/test/test_annrpython.py
Log:
(arigo, pedronis)

test & fix

after chasing for an afternoon: space content



Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Mon Oct  3 23:13:59 2005
@@ -201,6 +201,15 @@
         getbookkeeper().count("coerce", obj1, obj2)
         return pair(obj1, obj2).union()   # reasonable enough
 
+    # approximation of an annotation intersection, the result should be the annotation obj or 
+    # the intersection of obj and improvement
+    def improve((obj, improvement)):
+        if not improvement.contains(obj) and obj.contains(improvement):
+            return improvement
+        else:
+            return obj
+
+    
 # cloning a function with identical code, for the can_only_throw attribute
 def _clone(f, can_only_throw = None):
     newfunc = type(f)(f.func_code, f.func_globals, f.func_name,
@@ -503,6 +512,25 @@
                 return SomeObject()
         return SomeInstance(basedef, can_be_None=ins1.can_be_None or ins2.can_be_None)
 
+    def improve((ins1, ins2)):
+        if ins1.classdef is None:
+            resdef = ins2.classdef
+        elif ins2.classdef is None:
+            resdef = ins1.classdef
+        else:
+            basedef = ins1.classdef.commonbase(ins2.classdef)
+            if basedef is ins1.classdef:
+                resdef = ins2.classdef
+            elif basedef is ins2.classdef:
+                resdef = ins1.classdef
+            else:
+                if ins1.can_be_None and ins2.can_be_None:
+                    return SomePBC({None: True})
+                else:
+                    return SomeImpossibleValue()
+        return SomeInstance(resdef, can_be_None=ins1.can_be_None and ins2.can_be_None)
+        
+
 class __extend__(pairtype(SomeIterator, SomeIterator)):
 
     def union((iter1, iter2)):

Modified: pypy/dist/pypy/translator/annrpython.py
==============================================================================
--- pypy/dist/pypy/translator/annrpython.py	(original)
+++ pypy/dist/pypy/translator/annrpython.py	Mon Oct  3 23:13:59 2005
@@ -582,9 +582,7 @@
                     cell = self.binding(a)
                     if (link.exitcase, a) in knowntypedata:
                         knownvarvalue = knowntypedata[(link.exitcase, a)]
-                        if not knownvarvalue.contains(cell) and \
-                           cell.contains(knownvarvalue): # sanity check
-                            cell = knownvarvalue
+                        cell = pair(cell, knownvarvalue).improve()
 
                     if hasattr(cell,'is_type_of'):
                         renamed_is_type_of = []

Modified: pypy/dist/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/translator/test/test_annrpython.py	(original)
+++ pypy/dist/pypy/translator/test/test_annrpython.py	Mon Oct  3 23:13:59 2005
@@ -1730,6 +1730,21 @@
         a = self.RPythonAnnotator()
         s = a.build_types(f, [])
         assert s.knowntype == B
+
+    def test_type_is_no_improvement(self):
+        class B(object):
+            pass
+        class C(B):
+            pass
+        class D(B):
+            pass
+        def f(x):
+            if type(x) is C:
+                return x
+            raise Exception
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [D])
+        assert s == annmodel.SomeImpossibleValue()
         
 def g(n):
     return [0,1,2,n]



More information about the Pypy-commit mailing list