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

hpk at codespeak.net hpk at codespeak.net
Tue Nov 23 16:50:03 CET 2004


Author: hpk
Date: Tue Nov 23 16:50:03 2004
New Revision: 7625

Modified:
   pypy/trunk/src/pypy/annotation/binaryop.py
   pypy/trunk/src/pypy/annotation/builtin.py
   pypy/trunk/src/pypy/annotation/factory.py
   pypy/trunk/src/pypy/translator/test/snippet.py
   pypy/trunk/src/pypy/translator/test/test_annrpython.py
Log:
made "isinstance" and "is" more "clever" about 
preserving knowntypedata.  added a couple of
tests which illustrate that e.g. 

    if x is None: 
        # in this branch the annotator will know
        # that x is really None 

Accordingly made generalizitation of SomeBool()s a bit more
smarter so that they preserve knowntypedata across 
generalization and even receive it for constant 
cases (because a SomeBool(const=True) could be generalized
after a reflow so the knowntypedata information is still
useful even thought it doesn't appear to be)  



Modified: pypy/trunk/src/pypy/annotation/binaryop.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/binaryop.py	(original)
+++ pypy/trunk/src/pypy/annotation/binaryop.py	Tue Nov 23 16:50:03 2004
@@ -98,40 +98,24 @@
             return SomeBool()
 
     def is_((obj1, obj2)):
-        const = None
-        vararg = None
-        if obj1.is_constant():
-            const = obj1
-            var = obj2
-            vararg = 1
+        # XXX assumption: for "X is Y" we for simplification 
+        #     assume that X is possibly variable and Y constant 
+        #     (and not the other way round) 
+        r = SomeBool()
         if obj2.is_constant():
-            if const is not None:
-                return immutablevalue(obj1.const is obj2.const)
-            # we are in the case "SomeXXX is None" here 
-            if obj2.const is None and obj1.__class__ != SomeObject: 
-                return immutablevalue(False) 
-            const = obj2
-            var = obj1
-            vararg = 0
-        if const is not None:
-            # XXX HACK HACK HACK
-            # XXX HACK HACK HACK
-            # XXX HACK HACK HACK
-            # XXX HACK HACK HACK
-            # XXX HACK HACK HACK
-            fn, block, i = getbookkeeper().position_key
-            annotator = getbookkeeper().annotator
-            op = block.operations[i]
-            assert op.opname == "is_" 
-            assert len(op.args) == 2
-            assert annotator.binding(op.args[vararg]) is var
-            assert annotator.binding(op.args[1-vararg]).const is const.const
-            r = SomeBool()
-            r.knowntypedata = (op.args[vararg], const)
-            return r
-            
-        return SomeBool()
-
+            if obj1.is_constant(): 
+                r.const = obj1.const is obj2.const 
+        # XXX HACK HACK HACK
+        # XXX HACK HACK HACK
+        # XXX HACK HACK HACK
+        fn, block, i = getbookkeeper().position_key
+        annotator = getbookkeeper().annotator
+        op = block.operations[i]
+        assert op.opname == "is_" 
+        assert len(op.args) == 2
+        assert annotator.binding(op.args[0]) == obj1 
+        r.knowntypedata = (op.args[0], obj2)
+        return r
 
 class __extend__(pairtype(SomeInteger, SomeInteger)):
     # unsignedness is considered a rare and contagious disease
@@ -153,8 +137,16 @@
 class __extend__(pairtype(SomeBool, SomeBool)):
 
     def union((boo1, boo2)):
-        return SomeBool()
-
+        s = SomeBool() 
+        if getattr(boo1, 'const', -1) == getattr(boo2, 'const', -2): 
+            s.const = boo1.const 
+        if hasattr(boo1, 'knowntypedata') and \
+           hasattr(boo2, 'knowntypedata') and \
+           boo1.knowntypedata[0] == boo2.knowntypedata[0]: 
+            s.knowntypedata = (
+                boo1.knowntypedata[0], 
+                unionof(boo1.knowntypedata[1], boo2.knowntypedata[1]))
+        return s 
 
 class __extend__(pairtype(SomeString, SomeString)):
 

Modified: pypy/trunk/src/pypy/annotation/builtin.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/builtin.py	(original)
+++ pypy/trunk/src/pypy/annotation/builtin.py	Tue Nov 23 16:50:03 2004
@@ -40,35 +40,31 @@
     return cls2 is object or issubclass(cls1, cls2)
 
 def builtin_isinstance(s_obj, s_type):
+    s = SomeBool() 
     if s_type.is_constant():
         typ = s_type.const
         # XXX bit of a hack:
         if issubclass(typ, (int, long)):
             typ = int
         if s_obj.is_constant():
-            return immutablevalue(isinstance(s_obj.const, typ))
+            s.const = isinstance(s_obj.const, typ)
         elif our_issubclass(s_obj.knowntype, typ):
-            return immutablevalue(True)
+            s.const = True 
         elif not our_issubclass(typ, s_obj.knowntype): 
-            return immutablevalue(False)
-        else:
-            # XXX HACK HACK HACK
-            # XXX HACK HACK HACK
-            # XXX HACK HACK HACK
-            # XXX HACK HACK HACK
-            # XXX HACK HACK HACK
-            bk = getbookkeeper()
-            fn, block, i = bk.position_key
-            annotator = bk.annotator
-            op = block.operations[i]
-            assert op.opname == "simple_call" 
-            assert len(op.args) == 3
-            assert op.args[0] == Constant(isinstance)
-            assert annotator.binding(op.args[1]) is s_obj
-            r = SomeBool()
-            r.knowntypedata = (op.args[1], bk.valueoftype(typ))
-            return r
-    return SomeBool()
+            s.const = False 
+        # XXX HACK HACK HACK
+        # XXX HACK HACK HACK
+        # XXX HACK HACK HACK
+        bk = getbookkeeper()
+        fn, block, i = bk.position_key
+        annotator = bk.annotator
+        op = block.operations[i]
+        assert op.opname == "simple_call" 
+        assert len(op.args) == 3
+        assert op.args[0] == Constant(isinstance)
+        assert annotator.binding(op.args[1]) is s_obj
+        s.knowntypedata = (op.args[1], bk.valueoftype(typ))
+    return s 
 
 def builtin_issubclass(s_cls1, s_cls2):
     if s_cls1.is_constant() and s_cls2.is_constant():

Modified: pypy/trunk/src/pypy/annotation/factory.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/factory.py	(original)
+++ pypy/trunk/src/pypy/annotation/factory.py	Tue Nov 23 16:50:03 2004
@@ -268,6 +268,8 @@
         assert isinstance(func, FunctionType), "expected function, got %r"%func
         # do we need to specialize this function in several versions?
         x = getattr(func, '_specialize_', False)
+        #if not x: 
+        #    x = 'argtypes'
         if x:
             if x == 'argtypes':
                 key = short_type_name(args)

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 Nov 23 16:50:03 2004
@@ -492,7 +492,7 @@
 
 def flow_identity_info(x=object, y=object):
     if x is None:
-        if None is y:
+        if y is None:
             return (x, y)
         else:
             return (x, None)
@@ -623,3 +623,16 @@
     else: 
         x = apbc 
     return x.answer 
+
+
+def is_and_knowntype(x): 
+    if x is None: 
+        return x 
+    else: 
+        return None 
+
+def isinstance_and_knowntype(x): 
+    if isinstance(x, APBC): 
+        return x
+    else: 
+        return apbc 

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 Nov 23 16:50:03 2004
@@ -341,6 +341,21 @@
         self.assertEquals(s, annmodel.SomeInteger(nonneg=True)) 
         #self.assertEquals(s.__class__, annmodel.SomeInteger) 
 
+    def test_is_and_knowntype_data(self): 
+        a = RPythonAnnotator()
+        s = a.build_types(snippet.is_and_knowntype, [bool])
+        #a.simplify()
+        #a.translator.view()
+        self.assert_(isinstance(s, annmodel.SomeNone))
+
+    def test_isinstance_and_knowntype_data(self): 
+        a = RPythonAnnotator()
+        x = annmodel.SomePBC({snippet.apbc: True}) 
+        s = a.build_types(snippet.isinstance_and_knowntype, [x]) 
+        #a.simplify()
+        #a.translator.view()
+        self.assertEquals(s, x) 
+
 def g(n):
     return [0,1,2,n]
 



More information about the Pypy-commit mailing list