[pypy-svn] r10388 - in pypy/branch/pypy-normalize-exception: annotation annotation/test interpreter objspace objspace/flow objspace/flow/test translator/test

pedronis at codespeak.net pedronis at codespeak.net
Thu Apr 7 01:28:27 CEST 2005


Author: pedronis
Date: Thu Apr  7 01:28:27 2005
New Revision: 10388

Modified:
   pypy/branch/pypy-normalize-exception/annotation/binaryop.py
   pypy/branch/pypy-normalize-exception/annotation/bookkeeper.py
   pypy/branch/pypy-normalize-exception/annotation/builtin.py
   pypy/branch/pypy-normalize-exception/annotation/model.py
   pypy/branch/pypy-normalize-exception/annotation/test/test_model.py
   pypy/branch/pypy-normalize-exception/interpreter/gateway.py
   pypy/branch/pypy-normalize-exception/objspace/dummy.py
   pypy/branch/pypy-normalize-exception/objspace/flow/objspace.py
   pypy/branch/pypy-normalize-exception/objspace/flow/test/test_objspace.py
   pypy/branch/pypy-normalize-exception/translator/test/test_annrpython.py
Log:
branch ready for merge

tests pass

geninterplevel reactivated

Let the flow space generate TypeError code when necessary in raise code,
with test case

added support to distinguish SomeInstances that can be or cannot be None,
is_ annotation is capable of using the info. Some tests for the model 
changes.

Avoid that annotating an assert in tests tries to annotate py.test
AssertionError code!! 

targetpypy1 works again

targetpypy0 finishes annotatation without blocked blocks,
in addition to the above changes necessary for this also tweaked dummy space 
a bit to survive trough the normalize_exception code analysis.



Modified: pypy/branch/pypy-normalize-exception/annotation/binaryop.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/annotation/binaryop.py	(original)
+++ pypy/branch/pypy-normalize-exception/annotation/binaryop.py	Thu Apr  7 01:28:27 2005
@@ -100,20 +100,26 @@
         if obj2.is_constant():
             if obj1.is_constant(): 
                 r.const = obj1.const is obj2.const
+            if obj2.const is None and not getattr(obj1, 'can_be_None', True):
+                r.const = False
+        elif obj1.is_constant():
+            if obj1.const is None and not getattr(obj2, 'can_be_None', True):
+                r.const = False
         # XXX HACK HACK HACK
         # XXX HACK HACK HACK
         # XXX HACK HACK HACK
         bk = getbookkeeper()
-        if hasattr(obj1,'is_type_of') and obj2.is_constant():
-            r.knowntypedata = (obj1.is_type_of, bk.valueoftype(obj2.const))
-            return r
-        fn, block, i = bk.position_key
-        annotator = bk.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)
+        if bk is not None: # for testing
+            if hasattr(obj1,'is_type_of') and obj2.is_constant():
+                r.knowntypedata = (obj1.is_type_of, bk.valueoftype(obj2.const))
+                return r
+            fn, block, i = bk.position_key
+            annotator = bk.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)):
@@ -270,7 +276,7 @@
         if basedef is None:
             # print warning?
             return SomeObject()
-        return SomeInstance(basedef)
+        return SomeInstance(basedef, can_be_None=ins1.can_be_None or ins2.can_be_None)
 
 class __extend__(pairtype(SomeIterator, SomeIterator)):
 
@@ -329,7 +335,7 @@
 class __extend__(pairtype(SomeInstance, SomePBC)):
     def union((ins, pbc)):
         if pbc.isNone():
-            return ins
+            return SomeInstance(classdef=ins.classdef, can_be_None = True)
         classdef = ins.classdef.superdef_containing(pbc.knowntype)
         if classdef is None:
             # print warning?

Modified: pypy/branch/pypy-normalize-exception/annotation/bookkeeper.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/annotation/bookkeeper.py	(original)
+++ pypy/branch/pypy-normalize-exception/annotation/bookkeeper.py	Thu Apr  7 01:28:27 2005
@@ -321,7 +321,10 @@
 def getbookkeeper():
     """Get the current Bookkeeper.
     Only works during the analysis of an operation."""
-    return getthreadlocals().bookkeeper
+    try:
+        return getthreadlocals().bookkeeper
+    except AttributeError:
+        return None
 
 def ishashable(x):
     try:

Modified: pypy/branch/pypy-normalize-exception/annotation/builtin.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/annotation/builtin.py	(original)
+++ pypy/branch/pypy-normalize-exception/annotation/builtin.py	Thu Apr  7 01:28:27 2005
@@ -226,6 +226,9 @@
 BUILTIN_ANALYZERS[pypy.objspace.std.restricted_int.r_int] = builtin_int
 BUILTIN_ANALYZERS[pypy.objspace.std.restricted_int.r_uint] = restricted_uint
 BUILTIN_ANALYZERS[Exception.__init__.im_func] = exception_init
+# this one is needed otherwise when annotating assert in a test we may try to annotate 
+# py.test AssertionError.__init__ .
+BUILTIN_ANALYZERS[AssertionError.__init__.im_func] = exception_init
 BUILTIN_ANALYZERS[sys.getrefcount] = count
 BUILTIN_ANALYZERS[math.fmod] = math_fmod
 BUILTIN_ANALYZERS[math.floor] = math_floor

Modified: pypy/branch/pypy-normalize-exception/annotation/model.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/annotation/model.py	(original)
+++ pypy/branch/pypy-normalize-exception/annotation/model.py	Thu Apr  7 01:28:27 2005
@@ -166,9 +166,10 @@
 
 class SomeInstance(SomeObject):
     "Stands for an instance of a (user-defined) class."
-    def __init__(self, classdef):
+    def __init__(self, classdef, can_be_None=False):
         self.classdef = classdef
         self.knowntype = classdef.cls
+        self.can_be_None = can_be_None
     def fmt_knowntype(self, kt):
         return None
     def fmt_classdef(self, cd):

Modified: pypy/branch/pypy-normalize-exception/annotation/test/test_model.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/annotation/test/test_model.py	(original)
+++ pypy/branch/pypy-normalize-exception/annotation/test/test_model.py	Thu Apr  7 01:28:27 2005
@@ -11,6 +11,29 @@
 s6 = SomeImpossibleValue()
 slist = [s1,s2,s3,s4,s5,s6]
 
+
+class C(object):
+    pass
+
+class DummyClassDef:
+    def __init__(self, cls=C):
+        self.cls = cls
+
+si0 = SomeInstance(DummyClassDef(), True)
+si1 = SomeInstance(DummyClassDef())
+sNone = SomePBC({None: True})
+sTrue = SomeBool()
+sTrue.const = True
+sFalse = SomeBool()
+sFalse.const = False
+
+def test_is_None():
+    assert pair(sNone, sNone).is_() == sTrue
+    assert pair(si1, sNone).is_() == sFalse
+    assert pair(si0, sNone).is_() != sTrue
+    assert pair(si0, sNone).is_() != sFalse
+    assert pair(si0, sNone).is_() == SomeBool()
+
 def test_equality():
     assert s1 != s2 != s3 != s4 != s5 != s6
     assert s1 == SomeObject()

Modified: pypy/branch/pypy-normalize-exception/interpreter/gateway.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/interpreter/gateway.py	(original)
+++ pypy/branch/pypy-normalize-exception/interpreter/gateway.py	Thu Apr  7 01:28:27 2005
@@ -567,7 +567,7 @@
     return klass(source, filename, modname, do_imports)
 
 # uncomment this to check against applevel without translation
-ApplevelInterpClass = ApplevelClass
+##ApplevelInterpClass = ApplevelClass
 
 def appdef(source, applevel=ApplevelClass):
     """ NOT_RPYTHON: build an app-level helper function, like for example:

Modified: pypy/branch/pypy-normalize-exception/objspace/dummy.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/objspace/dummy.py	(original)
+++ pypy/branch/pypy-normalize-exception/objspace/dummy.py	Thu Apr  7 01:28:27 2005
@@ -77,6 +77,9 @@
         self.w_Ellpisis = W_Special(Ellipsis)
         self.w_False = self.wrap(0)
         self.w_True = self.wrap(1)
+        self.w_tuple = W_Special(tuple)
+        self.w_type = W_Special(type)
+        self.w_str = W_Special(str)
 
         for en in ObjSpace.ExceptionTable:
             setattr(self, 'w_'+en, self.wrap(en))

Modified: pypy/branch/pypy-normalize-exception/objspace/flow/objspace.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/objspace/flow/objspace.py	(original)
+++ pypy/branch/pypy-normalize-exception/objspace/flow/objspace.py	Thu Apr  7 01:28:27 2005
@@ -48,7 +48,7 @@
         self.w_type     = Constant(type)
         self.w_tuple    = Constant(tuple)
         for exc in [KeyError, ValueError, IndexError, StopIteration,
-                    AssertionError]:
+                    AssertionError, TypeError]:
             clsname = exc.__name__
             setattr(self, 'w_'+clsname, Constant(exc))
         # the following exceptions are the ones that should not show up

Modified: pypy/branch/pypy-normalize-exception/objspace/flow/test/test_objspace.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/objspace/flow/test/test_objspace.py	(original)
+++ pypy/branch/pypy-normalize-exception/objspace/flow/test/test_objspace.py	Thu Apr  7 01:28:27 2005
@@ -278,6 +278,14 @@
         self.show(x)
 
     #__________________________________________________________
+    def raisez(z, tb):
+        raise z.__class__,z, tb
+
+    def test_raisez(self):
+        x = self.codetest(self.raisez)
+        self.show(x)
+
+    #__________________________________________________________
     def raise_and_catch_1(exception_instance):
         try:
             raise exception_instance

Modified: pypy/branch/pypy-normalize-exception/translator/test/test_annrpython.py
==============================================================================
--- pypy/branch/pypy-normalize-exception/translator/test/test_annrpython.py	(original)
+++ pypy/branch/pypy-normalize-exception/translator/test/test_annrpython.py	Thu Apr  7 01:28:27 2005
@@ -606,6 +606,13 @@
         s = a.build_types(f, [object])
         assert s.knowntype is C
 
+    def test_ann_assert(self):
+        def assert_(x):
+            assert x,"XXX"
+        a = RPythonAnnotator()
+        s = a.build_types(assert_, [])
+        assert s.const is None
+
     def test_overrides(self):
         import sys
         excs = []



More information about the Pypy-commit mailing list