[pypy-svn] r7510 - in pypy/trunk/src/pypy: objspace/flow translator translator/test

hpk at codespeak.net hpk at codespeak.net
Sat Nov 20 15:37:26 CET 2004


Author: hpk
Date: Sat Nov 20 15:37:25 2004
New Revision: 7510

Modified:
   pypy/trunk/src/pypy/objspace/flow/model.py
   pypy/trunk/src/pypy/translator/annrpython.py
   pypy/trunk/src/pypy/translator/test/snippet.py
   pypy/trunk/src/pypy/translator/test/test_annrpython.py
Log:
don't block annotation when encountering functions 
that have no return path (i.e. they only raise 
exceptions). 

there is a once-per-flowgraph hasonlyexceptionreturns() 
method that traverses the graph to see if the return 
block is ever reached. 

added a test for func() calling another func which 
just raises an exception ... 



Modified: pypy/trunk/src/pypy/objspace/flow/model.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/flow/model.py	(original)
+++ pypy/trunk/src/pypy/objspace/flow/model.py	Sat Nov 20 15:37:25 2004
@@ -32,6 +32,22 @@
             block.exits      = ()
         return block
 
+    def hasonlyexceptionreturns(self):
+        try:
+            return self._onlyex
+        except AttributeError: 
+            def visit(link):
+                if isinstance(link, Link):
+                    if link.target == self.returnblock: 
+                        raise ValueError(link) 
+            try:
+                traverse(visit, self)
+            except ValueError:
+                self._onlyex = False 
+            else:
+                self._onlyex = True
+            return self._onlyex 
+
     def show(self):
         from pypy.translator.tool.pygame.flowviewer import SingleGraphLayout
         SingleGraphLayout(self).display()

Modified: pypy/trunk/src/pypy/translator/annrpython.py
==============================================================================
--- pypy/trunk/src/pypy/translator/annrpython.py	(original)
+++ pypy/trunk/src/pypy/translator/annrpython.py	Sat Nov 20 15:37:25 2004
@@ -207,9 +207,18 @@
                 inputcells.append(annmodel.immutablevalue(extra))
         inputcells.extend(extracells)
         self.addpendingblock(func, block, inputcells, factory)
+
         # get the (current) return value
         v = graph.getreturnvar()
-        return self.bindings.get(v, annmodel.SomeImpossibleValue())
+        try:
+            return self.bindings[v]
+        except KeyError: 
+            # let's see if the graph only has exception returns 
+            if graph.hasonlyexceptionreturns(): 
+                # XXX for functions with exceptions what to 
+                #     do anyway? 
+                return annmodel.SomeNone() 
+            return annmodel.SomeImpossibleValue()
 
     def reflowfromposition(self, position_key):
         fn, block, index = position_key

Modified: pypy/trunk/src/pypy/translator/test/snippet.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/snippet.py	(original)
+++ pypy/trunk/src/pypy/translator/test/snippet.py	Sat Nov 20 15:37:25 2004
@@ -576,3 +576,10 @@
     else:
         x = func2
     return x
+
+def func_producing_exception():
+    raise ValueError, "this might e.g. block the caller"
+
+def funccallsex():
+    return func_producing_exception()
+

Modified: pypy/trunk/src/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_annrpython.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_annrpython.py	Sat Nov 20 15:37:25 2004
@@ -318,6 +318,13 @@
         # but let's at least check *something*
         self.assert_(isinstance(s, SomeCallable))
 
+    def test_func_calls_func_which_just_raises(self):
+        a = RPythonAnnotator()
+        s = a.build_types(snippet.funccallsex, [])
+        # the test is mostly that the above line hasn't blown up
+        # but let's at least check *something*
+        #self.assert_(isinstance(s, SomeCallable))
+
 def g(n):
     return [0,1,2,n]
 



More information about the Pypy-commit mailing list