[pypy-svn] r24667 - in pypy/dist/pypy/translator/squeak: . test

nik at codespeak.net nik at codespeak.net
Tue Mar 21 10:30:39 CET 2006


Author: nik
Date: Tue Mar 21 10:30:37 2006
New Revision: 24667

Modified:
   pypy/dist/pypy/translator/squeak/node.py
   pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py
Log:
make reraising of exceptions uncaught by a try/except work.


Modified: pypy/dist/pypy/translator/squeak/node.py
==============================================================================
--- pypy/dist/pypy/translator/squeak/node.py	(original)
+++ pypy/dist/pypy/translator/squeak/node.py	Tue Mar 21 10:30:37 2006
@@ -146,9 +146,10 @@
         elif block.exitswitch is c_last_exception:
             # exception branching
             # wuah. ugly!
+            codef = formatter.codef
             exc_var = self.gen.unique_name(("var", "exception"), "exception")
             yield "] on: %s do: [:%s |" \
-                    % (formatter.codef.format(self.OPERATION_ERROR), exc_var)
+                    % (codef.format(self.OPERATION_ERROR), exc_var)
             exc_exits = []
             non_exc_exit = None
             for exit in block.exits:
@@ -158,7 +159,13 @@
                     exc_exits.append(exit)
             for exit in exc_exits:
                 yield "(%s type isKindOf: %s) ifTrue: [" \
-                        % (exc_var, formatter.codef.format(exit.llexitcase))
+                        % (exc_var, codef.format(exit.llexitcase))
+                if exit.last_exception is not None:
+                    yield "%s := %s type." \
+                            % (codef.format(exit.last_exception), exc_var)
+                if exit.last_exc_value is not None:
+                    yield "%s := %s value." \
+                            % (codef.format(exit.last_exc_value), exc_var)
                 for line in self.render_link(exit):
                     yield line
                 yield "] ifFalse: ["

Modified: pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py
==============================================================================
--- pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py	(original)
+++ pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py	Tue Mar 21 10:30:37 2006
@@ -141,6 +141,8 @@
         assert fn(1) == "1"
         assert fn(2) == "2"
 
+class TestException:
+
     def test_simpleexception(self):
         def raising(i):
             if i > 0:
@@ -156,3 +158,44 @@
         assert fn(-1) == "0"
         assert fn(2) == "1"
 
+    def test_exceptbranch(self):
+        def raising(i):
+            if i == 0:
+                raise ValueError
+            elif i < 0:
+                raise AttributeError
+            else:
+                return i + 1
+        def f(i):
+            try:
+                return raising(i)
+            except ValueError:
+                return i
+            except AttributeError:
+                return -i
+        fn = compile_function(f, [int])
+        assert fn(-1) == "1"
+        assert fn(0) == "0"
+        assert fn(2) == "3"
+
+    def test_exceptreraise(self):
+        def raising(i):
+            if i == 0:
+                raise ValueError
+            elif i < 0:
+                raise AttributeError
+            else:
+                return i + 1
+        def f(i):
+            try:
+                return raising(i)
+            except ValueError:
+                return i
+        def g(i):
+            try:
+                return f(i)
+            except AttributeError:
+                return -i
+        fn = compile_function(g, [int])
+        assert fn(-2) == "2"
+



More information about the Pypy-commit mailing list