[pypy-svn] r28919 - in pypy/dist/pypy/translator/backendopt: . test

pedronis at codespeak.net pedronis at codespeak.net
Sat Jun 17 19:14:21 CEST 2006


Author: pedronis
Date: Sat Jun 17 19:14:19 2006
New Revision: 28919

Modified:
   pypy/dist/pypy/translator/backendopt/inline.py
   pypy/dist/pypy/translator/backendopt/test/test_inline.py
Log:
extend inline exc matching heuristic to work with constants exceptions.



Modified: pypy/dist/pypy/translator/backendopt/inline.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/inline.py	(original)
+++ pypy/dist/pypy/translator/backendopt/inline.py	Sat Jun 17 19:14:19 2006
@@ -7,6 +7,7 @@
 from pypy.objspace.flow.model import traverse, mkentrymap, checkgraph
 from pypy.annotation import model as annmodel
 from pypy.rpython.lltypesystem.lltype import Bool, typeOf, Void, Ptr
+from pypy.rpython.lltypesystem.lltype import normalizeptr
 from pypy.rpython import rmodel
 from pypy.tool.algo import sparsemat
 from pypy.translator.backendopt.support import log, split_block_with_keepalive
@@ -82,14 +83,19 @@
     #XXX slightly brittle: find the exception type for simple cases
     #(e.g. if you do only raise XXXError) by doing pattern matching
     currvar = block.exits[0].args[1]
-    for op in block.operations[::-1]:
+    ops = block.operations
+    i = len(ops)-1
+    while True:
+        if isinstance(currvar, Constant):
+            return typeOf(normalizeptr(currvar.value)), block.exits[0]
+        if i < 0:
+            return None, None
+        op = ops[i]
+        i -= 1
         if op.opname in ("same_as", "cast_pointer") and op.result is currvar:
             currvar = op.args[0]
         elif op.opname == "malloc" and op.result is currvar:
-            break
-    else:
-        return None, None
-    return Ptr(op.args[0].value), block.exits[0]
+            return Ptr(op.args[0].value), block.exits[0]
 
 def does_raise_directly(graph, raise_analyzer):
     """ this function checks, whether graph contains operations which can raise

Modified: pypy/dist/pypy/translator/backendopt/test/test_inline.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/test/test_inline.py	(original)
+++ pypy/dist/pypy/translator/backendopt/test/test_inline.py	Sat Jun 17 19:14:19 2006
@@ -183,6 +183,30 @@
     result = eval_func([42])
     assert result == 1
 
+def test_inline_const_exceptions():
+    valueError = ValueError()
+    keyError = KeyError()
+    def f(x):
+        if x == 0:
+            raise valueError
+        if x == 1:
+            raise keyError
+    def g(x):
+        try:
+            f(x)
+        except ValueError:
+            return 2
+        except KeyError:
+            return x+2
+        return 1
+    eval_func = check_inline(f, g, [int])
+    result = eval_func([0])
+    assert result == 2
+    result = eval_func([1])
+    assert result == 3
+    result = eval_func([42])
+    assert result == 1
+
 def test_inline_exception_guarded():
     def h(x):
         if x == 1:



More information about the Pypy-commit mailing list