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

mwh at codespeak.net mwh at codespeak.net
Fri Nov 19 18:49:20 CET 2004


Author: mwh
Date: Fri Nov 19 18:49:19 2004
New Revision: 7461

Modified:
   pypy/trunk/src/pypy/annotation/builtin.py
   pypy/trunk/src/pypy/annotation/model.py
   pypy/trunk/src/pypy/translator/annrpython.py
   pypy/trunk/src/pypy/translator/test/snippet.py
   pypy/trunk/src/pypy/translator/test/test_annrpython.py
Log:
Smarten up valueoftype -- add optional bookkeeper argument, use it if
available when passed a user defined class.  Attach knowntype to return 
value in any case.  Tests for this.

Fixes to the isinstance annotation.


Modified: pypy/trunk/src/pypy/annotation/builtin.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/builtin.py	(original)
+++ pypy/trunk/src/pypy/annotation/builtin.py	Fri Nov 19 18:49:19 2004
@@ -33,6 +33,10 @@
 def builtin_chr(s_int):
     return SomeChar()
 
+def our_issubclass(cls1, cls2):
+    """ we're going to try to be less silly in the face of old-style classes"""
+    return cls2 is object or issubclass(cls1, cls2)
+
 def builtin_isinstance(s_obj, s_type):
     if s_type.is_constant():
         typ = s_type.const
@@ -41,25 +45,26 @@
             typ = int
         if s_obj.is_constant():
             return immutablevalue(isinstance(s_obj.const, typ))
-        elif issubclass(s_obj.knowntype, typ):
+        elif our_issubclass(s_obj.knowntype, typ):
             return immutablevalue(True)
-        elif not issubclass(typ, s_obj.knowntype): 
-            return immutablevalue(False) 
+        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
-            fn, block, i = getbookkeeper().position_key
-            annotator = getbookkeeper().annotator
+            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], valueoftype(typ))
+            r.knowntypedata = (op.args[1], valueoftype(typ, bk))
             return r
     return SomeBool()
 

Modified: pypy/trunk/src/pypy/annotation/model.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/model.py	(original)
+++ pypy/trunk/src/pypy/annotation/model.py	Fri Nov 19 18:49:19 2004
@@ -257,7 +257,7 @@
     result.const = x
     return result
 
-def valueoftype(t):
+def valueoftype(t, bookkeeper=None):
     "The most precise SomeValue instance that contains all objects of type t."
     if t is bool:
         return SomeBool()
@@ -267,8 +267,14 @@
         return SomeString()
     elif t is list:
         return SomeList(factories={})
+    # can't do dict, tuple
+    elif isinstance(t, (type, ClassType)) and \
+             t.__module__ != '__builtin__' and bookkeeper is not None:
+        return SomeInstance(bookkeeper.getclassdef(t))
     else:
-        return SomeObject()
+        o = SomeObject()
+        o.knowntype = t
+        return o
 
 ##def decode_simple_call(s_args, s_kwds):
 ##    s_nbargs = s_args.len()

Modified: pypy/trunk/src/pypy/translator/annrpython.py
==============================================================================
--- pypy/trunk/src/pypy/translator/annrpython.py	(original)
+++ pypy/trunk/src/pypy/translator/annrpython.py	Fri Nov 19 18:49:19 2004
@@ -58,7 +58,7 @@
         inputcells = []
         for t in input_arg_types:
             if not isinstance(t, annmodel.SomeObject):
-                t = annmodel.valueoftype(t)
+                t = annmodel.valueoftype(t, self.bookkeeper)
             inputcells.append(t)
         
         # register the entry point

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	Fri Nov 19 18:49:19 2004
@@ -462,6 +462,12 @@
         a = len(str(i))
     return a
 
+def flow_usertype_info(ob):
+    if isinstance(ob, WithInit):
+        return ob
+    else:
+        return WithMoreInit(1, 2)
+
 def flow_identity_info(x=object, y=object):
     if x is None:
         if None is y:

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	Fri Nov 19 18:49:19 2004
@@ -291,6 +291,18 @@
         # actually more precise information that i is non-negative
         self.assertEquals(s, annmodel.SomeInteger(nonneg=True))
 
+    def test_flow_usertype_info(self):
+        a = RPythonAnnotator()
+        s = a.build_types(snippet.flow_usertype_info, [object])
+        #a.translator.view()
+        self.assertEquals(s.knowntype, snippet.WithInit)
+
+    def test_flow_usertype_info2(self):
+        a = RPythonAnnotator()
+        s = a.build_types(snippet.flow_usertype_info, [snippet.WithMoreInit])
+        #a.translator.view()
+        self.assertEquals(s.knowntype, snippet.WithMoreInit)
+
     def test_flow_identity_info(self):
         a = RPythonAnnotator()
         s = a.build_types(snippet.flow_identity_info, [object, object])



More information about the Pypy-commit mailing list