[pypy-svn] r45554 - in pypy/dist/pypy/translator/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Wed Aug 8 16:18:51 CEST 2007


Author: antocuni
Date: Wed Aug  8 16:18:50 2007
New Revision: 45554

Modified:
   pypy/dist/pypy/translator/cli/function.py
   pypy/dist/pypy/translator/cli/ilgenerator.py
   pypy/dist/pypy/translator/cli/metavm.py
   pypy/dist/pypy/translator/cli/test/runtest.py
   pypy/dist/pypy/translator/cli/test/test_exception.py
Log:
- test test_exception both with the native exceptions and the exception transformer

- kill LastExceptionHandler





Modified: pypy/dist/pypy/translator/cli/function.py
==============================================================================
--- pypy/dist/pypy/translator/cli/function.py	(original)
+++ pypy/dist/pypy/translator/cli/function.py	Wed Aug  8 16:18:50 2007
@@ -18,156 +18,7 @@
 from pypy.translator.cli.support import log
 from pypy.translator.cli.ilgenerator import CLIBaseGenerator
 
-USE_LAST = False
-
-class NativeExceptionHandler(object):
-    def begin_try(self):
-        self.ilasm.begin_try()
-
-    def end_try(self, target_label):
-        self.ilasm.leave(target_label)
-        self.ilasm.end_try()
-
-    def begin_catch(self, llexitcase):
-        ll_meta_exc = llexitcase
-        ll_exc = ll_meta_exc._inst.class_._INSTANCE
-        cts_exc = self.cts.lltype_to_cts(ll_exc, False)
-        self.ilasm.begin_catch(cts_exc)
-
-    def end_catch(self, target_label):
-        self.ilasm.leave(target_label)
-        self.ilasm.end_catch()
-
-    def render_raise_block(self, block):
-        exc = block.inputargs[1]
-        self.load(exc)
-        self.ilasm.opcode('throw')
-
-    def store_exception_and_link(self, link):
-        if self._is_raise_block(link.target):
-            # the exception value is on the stack, use it as the 2nd target arg
-            assert len(link.args) == 2
-            assert len(link.target.inputargs) == 2
-            self.store(link.target.inputargs[1])
-        else:
-            # the exception value is on the stack, store it in the proper place
-            if isinstance(link.last_exception, flowmodel.Variable):
-                self.ilasm.opcode('dup')
-                self.store(link.last_exc_value)                            
-                self.ilasm.get_field(('class Object_meta', 'Object', 'meta'))
-                self.store(link.last_exception)
-            else:
-                self.store(link.last_exc_value)
-            self._setup_link(link)
-
-class LastExceptionHandler(object):
-    in_try = False
-
-    def begin_try(self):
-        self.in_try = True
-        self.ilasm.opcode('// begin_try')
-
-    def end_try(self, target_label):
-        self.ilasm.opcode('ldsfld', 'object last_exception')
-        self.ilasm.opcode('brnull', target_label)
-        self.ilasm.opcode('// end try')
-        self.in_try = False
-
-    def begin_catch(self, llexitcase):
-        self.ilasm.label(self.current_label('catch'))
-        ll_meta_exc = llexitcase
-        ll_exc = ll_meta_exc._inst.class_._INSTANCE
-        cts_exc = self.cts.lltype_to_cts(ll_exc, False)
-        self.ilasm.opcode('ldsfld', 'object last_exception')
-        self.isinstance(cts_exc)
-        self.ilasm.opcode('dup')
-        self.ilasm.opcode('brtrue.s', 6)
-        self.ilasm.opcode('pop')
-        self.ilasm.opcode('br', self.next_label('catch'))
-        # here is the target of the above brtrue.s
-        self.ilasm.opcode('ldnull')
-        self.ilasm.opcode('stsfld', 'object last_exception')
-
-    def end_catch(self, target_label):
-        self.ilasm.opcode('br', target_label)
-
-    def store_exception_and_link(self, link):
-        if self._is_raise_block(link.target):
-            # the exception value is on the stack, use it as the 2nd target arg
-            assert len(link.args) == 2
-            assert len(link.target.inputargs) == 2
-            self.store(link.target.inputargs[1])
-        else:
-            # the exception value is on the stack, store it in the proper place
-            if isinstance(link.last_exception, flowmodel.Variable):
-                self.ilasm.opcode('dup')
-                self.store(link.last_exc_value)                            
-                self.ilasm.get_field(('class Object_meta', 'Object', 'meta'))
-                self.store(link.last_exception)
-            else:
-                self.store(link.last_exc_value)
-            self._setup_link(link)
-
-    def before_last_blocks(self):
-        self.ilasm.label(self.current_label('catch'))
-        self.ilasm.opcode('nop')
-
-    def render_raise_block(self, block):
-        exc = block.inputargs[1]
-        self.load(exc)
-        self.ilasm.opcode('stsfld', 'object last_exception')
-        if not self.return_block: # must be a void function
-            TYPE = self.graph.getreturnvar().concretetype
-            default = TYPE._defl()
-            if default is not None: # concretetype is Void
-                try:
-                    self.db.constant_generator.push_primitive_constant(self, TYPE, default)
-                except AssertionError:
-                    self.ilasm.opcode('ldnull') # :-(
-            self.ilasm.opcode('ret')
-        else:
-            self.ilasm.opcode('br', self._get_block_name(self.return_block))
-
-    def _render_op(self, op):
-        OOFunction._render_op(self, op)
-        if op.opname in ('direct_call', 'oosend', 'indirect_call') and not self.in_try:
-            self._premature_return()
-
-    def _render_sub_op(self, sub_op):
-        OOFunction._render_sub_op(self, sub_op)
-        if sub_op.op.opname in ('direct_call', 'oosend', 'indirect_call') and not self.in_try:
-            self._premature_return(need_pop=sub_op.op.result is not ootype.Void)
-
-
-    def _premature_return(self, need_pop=False):
-        try:
-            return_block = self._get_block_name(self.graph.returnblock)
-        except KeyError:
-            self.ilasm.opcode('//premature return')
-            self.ilasm.opcode('ldsfld', 'object last_exception')
-            TYPE = self.graph.getreturnvar().concretetype
-            default = TYPE._defl()
-            if default is None: # concretetype is Void
-                self.ilasm.opcode('brfalse.s', 1)
-                self.ilasm.opcode('ret')
-            else:
-                self.ilasm.opcode('brfalse.s', 3) # ??
-                try:
-                    self.db.constant_generator.push_primitive_constant(self, TYPE, default)
-                except AssertionError:
-                    self.ilasm.opcode('ldnull') # :-(
-                self.ilasm.opcode('ret')
-        else:
-            self.ilasm.opcode('ldsfld', 'object last_exception')
-            self.ilasm.opcode('brtrue', return_block)
-
-
-if USE_LAST:
-    ExceptionHandler = LastExceptionHandler
-else:
-    ExceptionHandler = NativeExceptionHandler
-
-class Function(ExceptionHandler, OOFunction, Node, CLIBaseGenerator):
+class Function(OOFunction, Node, CLIBaseGenerator):
 
     def __init__(self, *args, **kwargs):
         OOFunction.__init__(self, *args, **kwargs)
@@ -243,6 +94,45 @@
             self.load(return_var)
         self.ilasm.opcode('ret')
 
+    def begin_try(self):
+        self.ilasm.begin_try()
+
+    def end_try(self, target_label):
+        self.ilasm.leave(target_label)
+        self.ilasm.end_try()
+
+    def begin_catch(self, llexitcase):
+        ll_meta_exc = llexitcase
+        ll_exc = ll_meta_exc._inst.class_._INSTANCE
+        cts_exc = self.cts.lltype_to_cts(ll_exc, False)
+        self.ilasm.begin_catch(cts_exc)
+
+    def end_catch(self, target_label):
+        self.ilasm.leave(target_label)
+        self.ilasm.end_catch()
+
+    def render_raise_block(self, block):
+        exc = block.inputargs[1]
+        self.load(exc)
+        self.ilasm.opcode('throw')
+
+    def store_exception_and_link(self, link):
+        if self._is_raise_block(link.target):
+            # the exception value is on the stack, use it as the 2nd target arg
+            assert len(link.args) == 2
+            assert len(link.target.inputargs) == 2
+            self.store(link.target.inputargs[1])
+        else:
+            # the exception value is on the stack, store it in the proper place
+            if isinstance(link.last_exception, flowmodel.Variable):
+                self.ilasm.opcode('dup')
+                self.store(link.last_exc_value)
+                self.ilasm.get_field(('class Object_meta', 'Object', 'meta'))
+                self.store(link.last_exception)
+            else:
+                self.store(link.last_exc_value)
+            self._setup_link(link)
+
     # XXX: this method should be moved into oosupport, but other
     # backends are not ready :-(
     def render_bool_switch(self, block):

Modified: pypy/dist/pypy/translator/cli/ilgenerator.py
==============================================================================
--- pypy/dist/pypy/translator/cli/ilgenerator.py	(original)
+++ pypy/dist/pypy/translator/cli/ilgenerator.py	Wed Aug  8 16:18:50 2007
@@ -58,7 +58,6 @@
             self.code.writeline('.module %s.netmodule' % name)
         else:
             self.code.writeline('.assembly %s {}' % name)
-        self.code.writeline('.field static object last_exception') # XXX
 
     def close(self):
         self.out.close()

Modified: pypy/dist/pypy/translator/cli/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/cli/metavm.py	(original)
+++ pypy/dist/pypy/translator/cli/metavm.py	Wed Aug  8 16:18:50 2007
@@ -159,13 +159,6 @@
         self.mapping = mapping
 
     def render(self, generator, op):
-        from pypy.translator.cli.function import LastExceptionHandler
-        if isinstance(generator, LastExceptionHandler):
-            self.render_last(generator, op)
-        else:
-            self.render_native(generator, op)
-
-    def render_native(self, generator, op):
         ilasm = generator.ilasm
         label = '__check_block_%d' % MapException.COUNT
         MapException.COUNT += 1
@@ -181,25 +174,6 @@
         ilasm.label(label)
         ilasm.opcode('nop')
 
-    def render_last(self, generator, op):
-        ilasm = generator.ilasm
-        stdflow = '__check_block_%d' % MapException.COUNT
-        MapException.COUNT += 1
-        premature_return = '__check_block_%d' % MapException.COUNT
-        MapException.COUNT += 1
-        ilasm.begin_try()
-        self.instr.render(generator, op)
-        ilasm.leave(stdflow)
-        ilasm.end_try()
-        for cli_exc, py_exc in self.mapping:
-            ilasm.begin_catch(cli_exc)
-            ilasm.new('instance void class %s::.ctor()' % py_exc)
-            ilasm.opcode('stsfld', 'object last_exception')
-            ilasm.leave(stdflow)
-            ilasm.end_catch()
-        ilasm.label(stdflow)
-        ilasm.opcode('nop')
-
 class _Box(MicroInstruction): 
     def render(self, generator, op):
         generator.load(op.args[0])

Modified: pypy/dist/pypy/translator/cli/test/runtest.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/runtest.py	(original)
+++ pypy/dist/pypy/translator/cli/test/runtest.py	Wed Aug  8 16:18:50 2007
@@ -73,7 +73,7 @@
         if return_type != 'void':
             ilasm.opcode('stloc', 'res')
         if self.wrap_exceptions:
-            ilasm.leave('check_last_exception')
+            ilasm.leave('check_etrafo_exception')
         else:
             ilasm.leave('print_result')
 
@@ -90,16 +90,6 @@
                     ilasm.leave('return')
                 ilasm.end_catch()
 
-            # check for USE_LAST exceptions
-            ilasm.label('check_last_exception')
-            ilasm.opcode('ldsfld', 'object last_exception')
-            ilasm.opcode('brnull', 'check_etrafo_exception')
-            # there is a pending exception
-            ilasm.opcode('ldsfld', 'object last_exception')
-            ilasm.call('string class [pypylib]pypy.test.Result::FormatException(object)')
-            ilasm.call('void class [mscorlib]System.Console::WriteLine(string)')
-            ilasm.opcode('br', 'return')
-
             # check for exception tranformer exceptions
             ilasm.label('check_etrafo_exception')
             if hasattr(self.db, 'exceptiontransformer'):
@@ -256,14 +246,15 @@
         self._ann = None
         self._cli_func = None
 
-    def _compile(self, fn, args, ann=None, backendopt=True, auto_raise_exc=False):
+    def _compile(self, fn, args, ann=None, backendopt=True, auto_raise_exc=False, exctrans=False):
         if ann is None:
             ann = [lltype_to_annotation(typeOf(x)) for x in args]
         if self._func is fn and self._ann == ann:
             return self._cli_func
         else:
             self._cli_func = compile_function(fn, ann, backendopt=backendopt,
-                                              auto_raise_exc=auto_raise_exc)
+                                              auto_raise_exc=auto_raise_exc,
+                                              exctrans=exctrans)
             self._func = fn
             self._ann = ann
             return self._cli_func
@@ -276,17 +267,17 @@
         if platform.processor() == 'powerpc':
             py.test.skip('PowerPC --> %s' % reason)
 
-    def interpret(self, fn, args, annotation=None, backendopt=True):
-        f = self._compile(fn, args, annotation, backendopt)
+    def interpret(self, fn, args, annotation=None, backendopt=True, exctrans=False):
+        f = self._compile(fn, args, annotation, backendopt=backendopt, exctrans=exctrans)
         res = f(*args)
         if isinstance(res, ExceptionWrapper):
             raise res
         return res
 
-    def interpret_raises(self, exception, fn, args):
+    def interpret_raises(self, exception, fn, args, exctrans=False):
         import exceptions # needed by eval
         try:
-            self.interpret(fn, args)
+            self.interpret(fn, args, exctrans=exctrans)
         except ExceptionWrapper, ex:
             assert issubclass(eval(ex.class_name), exception)
         else:

Modified: pypy/dist/pypy/translator/cli/test/test_exception.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_exception.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_exception.py	Wed Aug  8 16:18:50 2007
@@ -3,6 +3,12 @@
 from pypy.rpython.test.test_exception import BaseTestException
 
 class TestCliException(CliTest, BaseTestException):
+    use_exception_transformer = False
+
+    def interpret(self, *args, **kwds):
+        kwds['exctrans'] = self.use_exception_transformer
+        return CliTest.interpret(self, *args, **kwds)
+
     def test_nested_try(self):
         def helper(x):
             if x == 0:
@@ -55,3 +61,6 @@
                 obj = Derived()
             return obj.foo()
         assert self.interpret(fn, [0]) == 42
+
+class TestCliExceptionTransformer(TestCliException):
+    use_exception_transformer = True



More information about the Pypy-commit mailing list