[pypy-svn] r31441 - in pypy/dist/pypy/objspace/flow: . test

arigo at codespeak.net arigo at codespeak.net
Mon Aug 21 15:32:23 CEST 2006


Author: arigo
Date: Mon Aug 21 15:32:20 2006
New Revision: 31441

Modified:
   pypy/dist/pypy/objspace/flow/flowcontext.py
   pypy/dist/pypy/objspace/flow/specialcase.py
   pypy/dist/pypy/objspace/flow/test/test_objspace.py
Log:
(arre, pedronis, arigo)

Show a sane error when flowing functions with broken import statements.
Refactored the flow space tests a bit.


Modified: pypy/dist/pypy/objspace/flow/flowcontext.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/flowcontext.py	(original)
+++ pypy/dist/pypy/objspace/flow/flowcontext.py	Mon Aug 21 15:32:20 2006
@@ -281,6 +281,10 @@
 
             except OperationError, e:
                 #print "OE", e.w_type, e.w_value
+                if (self.space.do_imports_immediately and
+                    e.w_type is self.space.w_ImportError):
+                    raise ImportError('import statement always raises %s' % (
+                        e,))
                 link = self.make_link([e.w_type, e.w_value], self.graph.exceptblock)
                 self.recorder.crnt_block.closeblock(link)
 

Modified: pypy/dist/pypy/objspace/flow/specialcase.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/specialcase.py	(original)
+++ pypy/dist/pypy/objspace/flow/specialcase.py	Mon Aug 21 15:32:20 2006
@@ -2,6 +2,7 @@
 from pypy.objspace.flow.model import Constant
 from pypy.objspace.flow.operation import OperationName, Arity
 from pypy.interpreter.gateway import ApplevelClass
+from pypy.interpreter.error import OperationError
 from pypy.tool.cache import Cache
 
 def sc_import(space, fn, args):
@@ -13,7 +14,11 @@
     if space.do_imports_immediately:
         name, glob, loc, frm = (space.unwrap(w_name), space.unwrap(w_glob),
                                 space.unwrap(w_loc), space.unwrap(w_frm))
-        return space.wrap(__import__(name, glob, loc, frm))
+        try:
+            mod = __import__(name, glob, loc, frm)
+        except ImportError, e:
+            raise OperationError(space.w_ImportError, space.wrap(str(e)))
+        return space.wrap(mod)
     # redirect it, but avoid exposing the globals
     w_glob = Constant({})
     return space.do_operation('simple_call', Constant(__import__),

Modified: pypy/dist/pypy/objspace/flow/test/test_objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/flow/test/test_objspace.py	(original)
+++ pypy/dist/pypy/objspace/flow/test/test_objspace.py	Mon Aug 21 15:32:20 2006
@@ -1,19 +1,18 @@
 import autopath
+import py
 from pypy.objspace.flow.model import Constant, Block, Link, Variable, traverse
 from pypy.objspace.flow.model import flatten
 from pypy.interpreter.argument import Arguments
 from pypy.translator.simplify import simplify_graph
 from pypy.objspace.flow import FlowObjSpace 
 from pypy.objspace.flow import objspace
+from pypy import conftest
 
 import os
 import operator
 is_operator = getattr(operator, 'is_', operator.eq) # it's not there 2.2
 
-class TestFlowObjSpace:
-    def setup_class(cls): 
-        cls.space = FlowObjSpace() 
-
+class Base:
     def codetest(self, func):
         import inspect
         try:
@@ -23,17 +22,17 @@
         #name = func.func_name
         graph = self.space.build_flow(func)
         graph.source = inspect.getsource(func)
+        self.show(graph)
         return graph
 
-    def reallyshow(self, x):
-        x.show()
-        #import os
-        #from pypy.translator.tool.make_dot import make_dot
-        #dest = make_dot(x.name, x)
-        #os.system('gv %s' % str(dest))
+    def show(self, graph):
+        if conftest.option.view:
+            graph.show()
+
 
-    def show(self, x):
-        pass   # or   self.reallyshow(x)
+class TestFlowObjSpace(Base):
+    def setup_class(cls): 
+        cls.space = FlowObjSpace() 
 
     def all_operations(self, graph):
         result = {}
@@ -54,7 +53,6 @@
         assert len(x.startblock.exits) == 1
         link, = x.startblock.exits
         assert link.target == x.returnblock
-        self.show(x)
 
     #__________________________________________________________
     def simplefunc(x):
@@ -72,7 +70,6 @@
 
     def test_simplebranch(self):
         x = self.codetest(self.simplebranch)
-        self.show(x)
 
     #__________________________________________________________
     def ifthenelse(i, j):
@@ -82,7 +79,6 @@
     
     def test_ifthenelse(self):
         x = self.codetest(self.simplebranch)
-        self.show(x)
 
     #__________________________________________________________
     def loop(x):
@@ -102,7 +98,6 @@
     
     def test_print(self):
         x = self.codetest(self.print_)
-        self.show(x)
 
     #__________________________________________________________
     def while_(i):
@@ -111,7 +106,6 @@
 
     def test_while(self):
         x = self.codetest(self.while_)
-        self.show(x)
 
     #__________________________________________________________
     def union_easy(i):
@@ -123,7 +117,6 @@
 
     def test_union_easy(self):
         x = self.codetest(self.union_easy)
-        self.show(x)
 
     #__________________________________________________________
     def union_hard(i):
@@ -133,7 +126,6 @@
     
     def test_union_hard(self):
         x = self.codetest(self.union_hard)
-        self.show(x)
 
     #__________________________________________________________
     def while_union(i):
@@ -145,7 +137,6 @@
     
     def test_while_union(self):
         x = self.codetest(self.while_union)
-        self.show(x)
 
     #__________________________________________________________
     def simple_for(lst):
@@ -156,7 +147,6 @@
     
     def test_simple_for(self):
         x = self.codetest(self.simple_for)
-        self.show(x)
 
     #__________________________________________________________
     def nested_whiles(i, j):
@@ -173,7 +163,6 @@
 
     def test_nested_whiles(self):
         x = self.codetest(self.nested_whiles)
-        self.show(x)
 
     #__________________________________________________________
     def break_continue(x):
@@ -193,7 +182,6 @@
 
     def test_break_continue(self):
         x = self.codetest(self.break_continue)
-        self.show(x)
 
     #__________________________________________________________
     def unpack_tuple(lst):
@@ -201,7 +189,6 @@
 
     def test_unpack_tuple(self):
         x = self.codetest(self.unpack_tuple)
-        self.show(x)
 
     #__________________________________________________________
     def reverse_3(lst):
@@ -214,7 +201,6 @@
 
     def test_reverse_3(self):
         x = self.codetest(self.reverse_3)
-        self.show(x)
 
     #__________________________________________________________
     def finallys(lst):
@@ -237,7 +223,6 @@
 
     def test_finallys(self):
         x = self.codetest(self.finallys)
-        self.show(x)
 
     #__________________________________________________________
     def const_pow():
@@ -245,7 +230,6 @@
 
     def test_const_pow(self):
         x = self.codetest(self.const_pow)
-        self.show(x)
 
     #__________________________________________________________
     def implicitException(lst):
@@ -400,7 +384,6 @@
 
     def test_freevar(self):
         x = self.codetest(self.freevar(3))
-        self.show(x)
 
     #__________________________________________________________
     def raise1(msg):
@@ -408,8 +391,8 @@
     
     def test_raise1(self):
         x = self.codetest(self.raise1)
-        self.show(x)
         simplify_graph(x)
+        self.show(x)
         ops = x.startblock.operations
         assert len(ops) == 2
         assert ops[0].opname == 'simple_call'
@@ -425,7 +408,6 @@
     
     def test_raise2(self):
         x = self.codetest(self.raise2)
-        self.show(x)
         # XXX can't check the shape of the graph, too complicated...
 
     #__________________________________________________________
@@ -434,7 +416,6 @@
     
     def test_raise3(self):
         x = self.codetest(self.raise3)
-        self.show(x)
         # XXX can't check the shape of the graph, too complicated...
 
     #__________________________________________________________
@@ -443,7 +424,6 @@
     
     def test_raise4(self):
         x = self.codetest(self.raise4)
-        self.show(x)
 
     #__________________________________________________________
     def raisez(z, tb):
@@ -451,7 +431,6 @@
 
     def test_raisez(self):
         x = self.codetest(self.raisez)
-        self.show(x)
 
     #__________________________________________________________
     def raise_and_catch_1(exception_instance):
@@ -463,7 +442,6 @@
     
     def test_raise_and_catch_1(self):
         x = self.codetest(self.raise_and_catch_1)
-        self.show(x)
 
     #__________________________________________________________
     def catch_simple_call():
@@ -475,7 +453,6 @@
     
     def test_catch_simple_call(self):
         x = self.codetest(self.catch_simple_call)
-        self.show(x)
 
     #__________________________________________________________
     def dellocal():
@@ -486,7 +463,6 @@
     
     def test_dellocal(self):
         x = self.codetest(self.dellocal)
-        self.show(x)
 
     #__________________________________________________________
     def globalconstdict(name):
@@ -496,7 +472,6 @@
     
     def test_globalconstdict(self):
         x = self.codetest(self.globalconstdict)
-        self.show(x)
 
     #__________________________________________________________
     
@@ -539,7 +514,6 @@
 
     def test_jump_target_specialization(self):
         x = self.codetest(self.jump_target_specialization)
-        self.show(x)
         def visitor(node):
             if isinstance(node, Block):
                 for op in node.operations:
@@ -592,7 +566,6 @@
 
     def test_highly_branching_example(self):
         x = self.codetest(self.highly_branching_example)
-        self.show(x)
         assert len(flatten(x)) < 60   # roughly 20 blocks + 30 links
 
     #__________________________________________________________
@@ -691,24 +664,49 @@
                         call_args.append(op)
         traverse(visit, graph)
         assert not call_args
-        
 
-class TestFlowObjSpaceDelay:
+    def test_catch_importerror_1(self):
+        def f():
+            try:
+                import pypy.this_does_not_exist
+            except ImportError:
+                return 1
+        graph = self.codetest(f)
+        simplify_graph(graph)
+        self.show(graph)
+        assert not graph.startblock.operations
+        assert len(graph.startblock.exits) == 1
+        assert graph.startblock.exits[0].target is graph.returnblock
+
+    def test_catch_importerror_2(self):
+        def f():
+            try:
+                from pypy import this_does_not_exist
+            except ImportError:
+                return 1
+        graph = self.codetest(f)
+        simplify_graph(graph)
+        self.show(graph)
+        assert not graph.startblock.operations
+        assert len(graph.startblock.exits) == 1
+        assert graph.startblock.exits[0].target is graph.returnblock
+
+    def test_importerror_1(self):
+        def f():
+            import pypy.this_does_not_exist
+        py.test.raises(ImportError, 'self.codetest(f)')
+
+    def test_importerror_2(self):
+        def f():
+            from pypy import this_does_not_exist
+        py.test.raises(ImportError, 'self.codetest(f)')
+
+
+class TestFlowObjSpaceDelay(Base):
     def setup_class(cls): 
         cls.space = FlowObjSpace()
         cls.space.do_imports_immediately = False
 
-    def codetest(self, func):
-        import inspect
-        try:
-            func = func.im_func
-        except AttributeError:
-            pass
-        #name = func.func_name
-        graph = self.space.build_flow(func)
-        graph.source = inspect.getsource(func)
-        return graph
-
     def test_import_something(self):
         def f():
             from some.unknown.module import stuff



More information about the Pypy-commit mailing list