[pypy-svn] r56125 - in pypy/branch/less-meta-instances/pypy/translator: cli/test jvm jvm/src/pypy jvm/test oosupport/test_template

niko at codespeak.net niko at codespeak.net
Fri Jun 27 18:09:40 CEST 2008


Author: niko
Date: Fri Jun 27 18:09:38 2008
New Revision: 56125

Added:
   pypy/branch/less-meta-instances/pypy/translator/jvm/src/pypy/PyPyThrowable.java
   pypy/branch/less-meta-instances/pypy/translator/oosupport/test_template/exception.py
Modified:
   pypy/branch/less-meta-instances/pypy/translator/cli/test/test_exception.py
   pypy/branch/less-meta-instances/pypy/translator/jvm/database.py
   pypy/branch/less-meta-instances/pypy/translator/jvm/node.py
   pypy/branch/less-meta-instances/pypy/translator/jvm/test/test_exception.py
   pypy/branch/less-meta-instances/pypy/translator/jvm/typesystem.py
Log:
start fixing jvm: test try_nested in test/test_exceptions still doesn't work



Modified: pypy/branch/less-meta-instances/pypy/translator/cli/test/test_exception.py
==============================================================================
--- pypy/branch/less-meta-instances/pypy/translator/cli/test/test_exception.py	(original)
+++ pypy/branch/less-meta-instances/pypy/translator/cli/test/test_exception.py	Fri Jun 27 18:09:38 2008
@@ -1,6 +1,6 @@
 import py
 from pypy.translator.cli.test.runtest import CliTest
-from pypy.rpython.test.test_exception import BaseTestException
+from pypy.translator.oosupport.test_template.exception import BaseTestException
 
 class TestCliException(CliTest, BaseTestException):
     use_exception_transformer = False
@@ -9,73 +9,12 @@
         kwds['exctrans'] = self.use_exception_transformer
         return CliTest.interpret(self, *args, **kwds)
 
-    def test_nested_try(self):
-        def helper(x):
-            if x == 0:
-                raise ValueError
-        def dummy():
-            pass        
-        def fn(x):
-            try:
-                try:
-                    helper(x)
-                finally:
-                    dummy()
-            except ValueError, e:
-                 raise
-        
-        self.interpret_raises(ValueError, fn, [0])
-
-    def test_exception_not_last(self):
-        def helper(x):
-            if x == 0:
-                raise ValueError
-        def fn(x):
-            helper(x)
-            try:
-                helper(1)
-            finally:
-                return -1
-            return x
-        self.interpret_raises(ValueError, fn, [0])
-
     def test_raise_and_catch_other(self):
         pass
 
     def test_raise_prebuilt_and_catch_other(self):
         pass
 
-    def test_missing_return_block(self):
-        class Base:
-            def foo(self):
-                raise ValueError
-
-        class Derived(Base):
-            def foo(self):
-                return 42
-
-        def fn(x):
-            if x:
-                obj = Base()
-            else:
-                obj = Derived()
-            return obj.foo()
-        assert self.interpret(fn, [0]) == 42
-
-    def test_missing_handler(self):
-        def foo(x):
-            if x:
-                raise ValueError
-        
-        def fn(x):
-            try:
-                foo(x)
-            except ValueError:
-                raise
-            return 42
-        assert self.interpret(fn, [0], backendopt=False) == 42
-        self.interpret_raises(ValueError, fn, [1], backendopt=False)
-
 
 class TestCliExceptionTransformer(TestCliException):
     use_exception_transformer = True

Modified: pypy/branch/less-meta-instances/pypy/translator/jvm/database.py
==============================================================================
--- pypy/branch/less-meta-instances/pypy/translator/jvm/database.py	(original)
+++ pypy/branch/less-meta-instances/pypy/translator/jvm/database.py	Fri Jun 27 18:09:38 2008
@@ -40,11 +40,11 @@
 
         self._constants = {}      # flowmodel.Variable --> jvm.Const
 
-        # Special fields for the Object class, see _translate_Object
-        self._object_interf = None
-        self._object_impl = None
-        self._object_exc_impl = None
-
+#        # Special fields for the Object class, see _translate_Object
+#        self._object_interf = None
+#        self._object_impl = None
+#        self._object_exc_impl = None
+#
         # Create information about the Main class we will build:
         #
         #    It will have two static fields, 'ilink' and 'pypy'.  The
@@ -182,67 +182,6 @@
         self.pending_node(clsobj)
         return clsobj
 
-    def _translate_Object(self, OBJ):
-        """
-        We handle the class 'Object' quite specially: we translate it
-        into an interface with two implementations.  One
-        implementation serves as the root of most objects, and the
-        other as the root for all exceptions.
-        """
-        assert self.is_Object(OBJ)
-        assert OBJ._superclass == ootype.ROOT
-
-        # Have we already translated Object?
-        if self._object_interf: return self._object_interf
-
-        # Create the interface and two implementations:
-        def gen_name(): return self._pkg(self._uniq(OBJ._name))
-        internm, implnm, exc_implnm = gen_name(), gen_name(), gen_name()
-        self._object_interf = node.Interface(internm)
-        self._object_impl = node.Class(implnm, supercls=jvm.jObject)
-        self._object_exc_impl = node.Class(exc_implnm, supercls=jvm.jThrowable)
-        self._object_impl.add_interface(self._object_interf)
-        self._object_exc_impl.add_interface(self._object_interf)
-
-        # Translate the fields into properties on the interface,
-        # and into actual fields on the implementations.
-        for fieldnm, (FIELDOOTY, fielddef) in OBJ._fields.iteritems():
-            if FIELDOOTY is ootype.Void: continue
-            fieldty = self.lltype_to_cts(FIELDOOTY)
-
-            # Currently use hacky convention of _jvm_FieldName for the name
-            methodnm = "_jvm_"+fieldnm
-
-            def getter_method_obj(node):
-                return jvm.Method.v(node, methodnm+"_g", [], fieldty)
-            def putter_method_obj(node):
-                return jvm.Method.v(node, methodnm+"_p", [fieldty], jvm.jVoid)
-            
-            # Add get/put methods to the interface:
-            prop = jvm.Property(
-                fieldnm, 
-                getter_method_obj(self._object_interf),
-                putter_method_obj(self._object_interf),
-                OOTYPE=FIELDOOTY)
-            self._object_interf.add_property(prop)
-
-            # Generate implementations:
-            def generate_impl(clsobj):
-                clsnm = clsobj.name
-                fieldobj = jvm.Field(clsnm, fieldnm, fieldty, False, FIELDOOTY)
-                clsobj.add_field(fieldobj, fielddef)
-                clsobj.add_method(node.GetterFunction(
-                    self, clsobj, getter_method_obj(clsobj), fieldobj))
-                clsobj.add_method(node.PutterFunction(
-                    self, clsobj, putter_method_obj(clsobj), fieldobj))
-            generate_impl(self._object_impl)
-            generate_impl(self._object_exc_impl)
-
-        # Ensure that we generate all three classes.
-        self.pending_node(self._object_interf)
-        self.pending_node(self._object_impl)
-        self.pending_node(self._object_exc_impl)
-
     def _translate_superclass_of(self, OOSUB):
         """
         Invoked to translate OOSUB's super class.  Normally just invokes
@@ -250,12 +189,9 @@
         make all exceptions descend from Throwable.
         """
         OOSUPER = OOSUB._superclass
-        if not self.is_Object(OOSUPER):
-            return self.pending_class(OOSUPER)
-        self._translate_Object(OOSUPER)          # ensure this has been done
         if OOSUB._name == "exceptions.Exception":
-            return self._object_exc_impl
-        return self._object_impl        
+            return jvm.jPyPyThrowable
+        return self.pending_class(OOSUPER)
 
     def _translate_instance(self, OOTYPE):
         assert isinstance(OOTYPE, ootype.Instance)
@@ -564,15 +500,6 @@
         TP = annotation_to_lltype(s_tp)
         return self.lltype_to_cts(TP)
 
-    def exception_root_object(self):
-        """
-        Returns a JvmType representing the version of Object that
-        serves as the root of all exceptions.
-        """
-        self.lltype_to_cts(rclass.OBJECT)
-        assert self._object_interf
-        return self._object_exc_impl
-
     # _________________________________________________________________
     # Uh....
     #

Modified: pypy/branch/less-meta-instances/pypy/translator/jvm/node.py
==============================================================================
--- pypy/branch/less-meta-instances/pypy/translator/jvm/node.py	(original)
+++ pypy/branch/less-meta-instances/pypy/translator/jvm/node.py	Fri Jun 27 18:09:38 2008
@@ -22,7 +22,8 @@
 from pypy.translator.jvm.typesystem import \
      JvmGeneratedClassType, jString, jStringArray, jVoid, jThrowable, jInt, \
      jObject, JvmType, jStringBuilder, jPyPyInterlink, jCallbackInterfaces, \
-     JvmGeneratedInterfaceType, jPyPy, jPyPyAbstractMethodException
+     JvmGeneratedInterfaceType, jPyPy, jPyPyAbstractMethodException, \
+     jPyPyThrowable, OBJECTGETCLASS
 from pypy.translator.jvm.opcodes import \
      opcodes
 from pypy.translator.jvm.option import \
@@ -173,8 +174,7 @@
             gen.goto(done_printing)
             gen.end_try()
 
-            jexc = self.db.exception_root_object()
-            gen.begin_catch(jexc)
+            gen.begin_catch(jPyPyThrowable)
             gen.emit(jvm.PYPYDUMPEXCWRAPPER) # dumps to stdout
             gen.end_catch()
 
@@ -366,7 +366,7 @@
 
     def begin_catch(self, llexitcase):
         ll_meta_exc = llexitcase
-        ll_exc = ll_meta_exc._inst.class_._INSTANCE
+        ll_exc = ll_meta_exc._INSTANCE
         jtype = self.cts.lltype_to_cts(ll_exc)
         assert jtype.throwable # SHOULD only try to catch subtypes of Exception
         self.ilasm.begin_catch(jtype)
@@ -384,11 +384,12 @@
         else:
             # the exception value is on the stack, store it in the proper place
             if isinstance(link.last_exception, flowmodel.Variable):
-                self.ilasm.emit(jvm.DUP)
+                # if the code that follows is interested in the class
+                # of the exception, extract it
+                #self.ilasm.dup_jtype(jPyPyThrowable)
                 self.ilasm.store(link.last_exc_value)
-                fld = self.db.lltype_to_cts(rclass.OBJECT).lookup_field('meta')
-                self.ilasm.emit(fld)
-                self.ilasm.store(link.last_exception)
+                #self.ilasm.emit(OBJECTGETCLASS)
+                #self.ilasm.store(link.last_exception)
             else:
                 self.ilasm.store(link.last_exc_value)
             self._setup_link(link)
@@ -450,8 +451,15 @@
                 can_branch_directly(last_op.opname) and
                 not_in_link_args(block.exitswitch)):
 
+                self.generator.add_comment(
+                    "short-circuit final comparison on %s, block has %d ops" % (
+                    block.exitswitch, len(block.operations)))
+
                 for op in block.operations[:-1]:
                     self._render_op(op)
+                    
+                self.generator.add_comment(
+                    "inlining comparison: %r" % (last_op),)
                 for arg in last_op.args:
                     self.ilasm.load(arg)
                 truelink, falselink = true_false_exits()
@@ -503,12 +511,12 @@
         self.ilasm.load(exc)
 
         # Check whether the static type is known to be throwable.
-        # If not, emit a CHECKCAST to the base exception type.
+        # If not, emit a CHECKCAST to throwable.
         # According to Samuele, no non-Exceptions should be thrown,
         # but this is not enforced by the RTyper or annotator.
         jtype = self.db.lltype_to_cts(exc.concretetype)
         if not jtype.throwable:
-            self.ilasm.downcast_jtype(self.db.exception_root_object())
+            self.ilasm.downcast_jtype(jThrowable)
             
         self.ilasm.throw()
 

Added: pypy/branch/less-meta-instances/pypy/translator/jvm/src/pypy/PyPyThrowable.java
==============================================================================
--- (empty file)
+++ pypy/branch/less-meta-instances/pypy/translator/jvm/src/pypy/PyPyThrowable.java	Fri Jun 27 18:09:38 2008
@@ -0,0 +1,8 @@
+package pypy;
+
+// This class is used as the superclass of RPython's
+// exception.Exception class.  We use this rather than Throwable
+// because it makes it easy to catch RPython exceptions in our
+// automated tests (just catch any PyPyThrowable instance)
+public class PyPyThrowable extends Throwable
+{}
\ No newline at end of file

Modified: pypy/branch/less-meta-instances/pypy/translator/jvm/test/test_exception.py
==============================================================================
--- pypy/branch/less-meta-instances/pypy/translator/jvm/test/test_exception.py	(original)
+++ pypy/branch/less-meta-instances/pypy/translator/jvm/test/test_exception.py	Fri Jun 27 18:09:38 2008
@@ -1,57 +1,11 @@
 import py
 from pypy.translator.jvm.test.runtest import JvmTest
-from pypy.rpython.test.test_exception import BaseTestException
+from pypy.translator.oosupport.test_template.exception import BaseTestException
 
 class TestJvmException(JvmTest, BaseTestException):
-    def test_nested_try(self):
-        def helper(x):
-            if x == 0:
-                raise ValueError
-        def dummy():
-            pass        
-        def fn(x):
-            try:
-                try:
-                    helper(x)
-                finally:
-                    dummy()
-            except ValueError, e:
-                 raise
-        
-        self.interpret_raises(ValueError, fn, [0])
-
-    def test_exception_not_last(self):
-        def helper(x):
-            if x == 0:
-                raise ValueError
-        def fn(x):
-            helper(x)
-            try:
-                helper(1)
-            finally:
-                return -1
-            return x
-        self.interpret_raises(ValueError, fn, [0])
 
     def test_raise_and_catch_other(self):
         pass
 
     def test_raise_prebuilt_and_catch_other(self):
         pass
-
-    def test_missing_return_block(self):
-        class Base:
-            def foo(self):
-                raise ValueError
-
-        class Derived(Base):
-            def foo(self):
-                return 42
-
-        def fn(x):
-            if x:
-                obj = Base()
-            else:
-                obj = Derived()
-            return obj.foo()
-        assert self.interpret(fn, [0]) == 42

Modified: pypy/branch/less-meta-instances/pypy/translator/jvm/typesystem.py
==============================================================================
--- pypy/branch/less-meta-instances/pypy/translator/jvm/typesystem.py	(original)
+++ pypy/branch/less-meta-instances/pypy/translator/jvm/typesystem.py	Fri Jun 27 18:09:38 2008
@@ -186,6 +186,7 @@
 jCharClass = JvmClassType('java.lang.Character')
 jBoolClass = JvmClassType('java.lang.Boolean')
 jThrowable = JvmClassType('java.lang.Throwable', throwable=True)
+jPyPyThrowable = JvmClassType('pypy.PyPyThrowable', throwable=True)
 jObject = JvmClassType('java.lang.Object')
 jString = JvmClassType('java.lang.String')
 jCharSequence = JvmClassType('java.lang.CharSequence')

Added: pypy/branch/less-meta-instances/pypy/translator/oosupport/test_template/exception.py
==============================================================================
--- (empty file)
+++ pypy/branch/less-meta-instances/pypy/translator/oosupport/test_template/exception.py	Fri Jun 27 18:09:38 2008
@@ -0,0 +1,51 @@
+import py
+from pypy.rpython.test.test_exception \
+     import BaseTestException as RBaseTestException
+
+class BaseTestException(RBaseTestException):
+    def test_nested_try(self):
+        def helper(x):
+            if x == 0:
+                raise ValueError
+        def dummy():
+            pass        
+        def fn(x):
+            try:
+                try:
+                    helper(x)
+                finally:
+                    dummy()
+            except ValueError, e:
+                 raise
+        
+        self.interpret_raises(ValueError, fn, [0])
+
+    def test_exception_not_last(self):
+        def helper(x):
+            if x == 0:
+                raise ValueError
+        def fn(x):
+            helper(x)
+            try:
+                helper(1)
+            finally:
+                return -1
+            return x
+        self.interpret_raises(ValueError, fn, [0])
+
+    def test_missing_return_block(self):
+        class Base:
+            def foo(self):
+                raise ValueError
+
+        class Derived(Base):
+            def foo(self):
+                return 42
+
+        def fn(x):
+            if x:
+                obj = Base()
+            else:
+                obj = Derived()
+            return obj.foo()
+        assert self.interpret(fn, [0]) == 42



More information about the Pypy-commit mailing list