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

arigo at codespeak.net arigo at codespeak.net
Tue Oct 4 11:29:56 CEST 2005


Author: arigo
Date: Tue Oct  4 11:29:51 2005
New Revision: 18124

Modified:
   pypy/dist/pypy/annotation/binaryop.py
   pypy/dist/pypy/translator/test/test_annrpython.py
Log:
Test and fix: the improve() on SomeInstance did not preserve the 'const'
attributes.  The fix makes sure that the result of improve() is not worse than
the original object.



Modified: pypy/dist/pypy/annotation/binaryop.py
==============================================================================
--- pypy/dist/pypy/annotation/binaryop.py	(original)
+++ pypy/dist/pypy/annotation/binaryop.py	Tue Oct  4 11:29:51 2005
@@ -528,8 +528,15 @@
                     return SomePBC({None: True})
                 else:
                     return SomeImpossibleValue()
-        return SomeInstance(resdef, can_be_None=ins1.can_be_None and ins2.can_be_None)
-        
+        res = SomeInstance(resdef, can_be_None=ins1.can_be_None and ins2.can_be_None)
+        if ins1.contains(res) and ins2.contains(res):
+            return res    # fine
+        else:
+            # this case can occur in the presence of 'const' attributes,
+            # which we should try to preserve.  Fall-back...
+            thistype = pairtype(SomeInstance, SomeInstance)
+            return super(thistype, pair(ins1, ins2)).improve()
+
 
 class __extend__(pairtype(SomeIterator, SomeIterator)):
 

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	Tue Oct  4 11:29:51 2005
@@ -1745,7 +1745,20 @@
         a = self.RPythonAnnotator()
         s = a.build_types(f, [D])
         assert s == annmodel.SomeImpossibleValue()
-        
+
+    def test_is_constant_instance(self):
+        class A(object):
+            pass
+        prebuilt_instance = A()
+        def f(x):
+            if x is prebuilt_instance:
+                return x
+            raise Exception
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [A])
+        assert s.is_constant()
+        assert s.const is prebuilt_instance
+
 def g(n):
     return [0,1,2,n]
 



More information about the Pypy-commit mailing list