[pypy-svn] r21609 - in pypy/dist/pypy/translator/c: . test

pedronis at codespeak.net pedronis at codespeak.net
Fri Dec 30 23:17:00 CET 2005


Author: pedronis
Date: Fri Dec 30 23:16:58 2005
New Revision: 21609

Modified:
   pypy/dist/pypy/translator/c/genc.py
   pypy/dist/pypy/translator/c/test/test_boehm.py
Log:
make test_boehm runnable using the isolate.py mechanism.



Modified: pypy/dist/pypy/translator/c/genc.py
==============================================================================
--- pypy/dist/pypy/translator/c/genc.py	(original)
+++ pypy/dist/pypy/translator/c/genc.py	Fri Dec 30 23:16:58 2005
@@ -9,6 +9,7 @@
 from pypy.translator.tool.cbuild import import_module_from_directory
 from pypy.rpython.lltypesystem import lltype
 from pypy.tool.udir import udir
+from pypy.tool import isolate
 from pypy.translator.locality.calltree import CallTree
 from pypy.translator.c.support import log
 from pypy.rpython.typesystem import getfunctionptr
@@ -108,12 +109,23 @@
         if self.symboltable:
             self.symboltable.attach(mod)   # hopefully temporary hack
         return mod
+
+    def isolated_import(self):
+        assert self._compiled
+        assert not self.c_ext_module
+        self.c_ext_module = isolate.Isolate((str(self.c_source_filename.dirpath()),
+                                             self.c_source_filename.purebasename))
+        return self.c_ext_module
         
     def get_entry_point(self):
         assert self.c_ext_module 
         return getattr(self.c_ext_module, 
                        self.entrypoint.func_name)
 
+    def cleanup(self):
+        assert self.c_ext_module
+        if isinstance(self.c_ext_module, isolate.Isolate):
+            isolate.close_isolate(self.c_ext_module)
 
 class CStandaloneBuilder(CBuilder):
     standalone = True

Modified: pypy/dist/pypy/translator/c/test/test_boehm.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_boehm.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_boehm.py	Fri Dec 30 23:16:58 2005
@@ -1,71 +1,83 @@
 import py
 from pypy.translator.translator import TranslationContext
-from pypy.translator.tool.cbuild import skip_missing_compiler
+from pypy.translator.tool.cbuild import skip_missing_compiler, check_boehm_presence
 from pypy.translator.c.genc import CExtModuleBuilder
 
-py.test.skip("boehm test is fragile wrt. the number of dynamically loaded libs")
-
-
-def getcompiled(func):
-    from pypy.translator.c.gc import BoehmGcPolicy
-    t = TranslationContext(simplifying=True)
-    # builds starting-types from func_defs 
-    argstypelist = []
-    if func.func_defaults:
-        for spec in func.func_defaults:
-            if isinstance(spec, tuple):
-                spec = spec[0] # use the first type only for the tests
-            argstypelist.append(spec)
-    a = t.buildannotator().build_types(func, argstypelist)
-    t.buildrtyper().specialize()
-    t.checkgraphs()
-    def compile():
-        cbuilder = CExtModuleBuilder(t, func, gcpolicy=BoehmGcPolicy)
-        c_source_filename = cbuilder.generate_source()
-        cbuilder.compile()
-        cbuilder.import_module()    
-        return cbuilder.get_entry_point()
-    return skip_missing_compiler(compile)
-
-
-def test_malloc_a_lot():
-    def malloc_a_lot():
-        i = 0
-        while i < 10:
-            i += 1
-            a = [1] * 10
-            j = 0
-            while j < 20:
-                j += 1
-                a.append(j)
-    fn = getcompiled(malloc_a_lot)
-    fn()
-
-def test__del__():
-    class State:
-        pass
-    s = State()
-    class A(object):
-        def __del__(self):
-            s.a_dels += 1
-    class B(A):
-        def __del__(self):
-            s.b_dels += 1
-    class C(A):
-        pass
-    def f():
-        s.a_dels = 0
-        s.b_dels = 0
-        A()
-        B()
-        C()
-        A()
-        B()
-        C()
-        return s.a_dels * 10 + s.b_dels
-    fn = getcompiled(f)
-    res = f()
-    assert res == 42
-    res = fn() #does not crash
-    res = fn() #does not crash
-    assert 0 <= res <= 42 # 42 cannot be guaranteed
+def setup_module(mod):
+    if not check_boehm_presence():
+        py.test.skip("Boehm GC not present")
+
+class TestUsingBoehm:
+
+    # deal with cleanups
+    def setup_method(self, meth):
+        self._cleanups = []
+    def teardown_method(self, meth):
+        while self._cleanups:
+            #print "CLEANUP"
+            self._cleanups.pop()()
+
+    def getcompiled(self, func):
+        from pypy.translator.c.gc import BoehmGcPolicy
+        t = TranslationContext(simplifying=True)
+        # builds starting-types from func_defs 
+        argstypelist = []
+        if func.func_defaults:
+            for spec in func.func_defaults:
+                if isinstance(spec, tuple):
+                    spec = spec[0] # use the first type only for the tests
+                argstypelist.append(spec)
+        a = t.buildannotator().build_types(func, argstypelist)
+        t.buildrtyper().specialize()
+        t.checkgraphs()
+        def compile():
+            cbuilder = CExtModuleBuilder(t, func, gcpolicy=BoehmGcPolicy)
+            c_source_filename = cbuilder.generate_source()
+            cbuilder.compile()
+            mod = cbuilder.isolated_import()
+            self._cleanups.append(cbuilder.cleanup) # schedule cleanup after test
+            return cbuilder.get_entry_point()
+        return skip_missing_compiler(compile)
+
+
+    def test_malloc_a_lot(self):
+        def malloc_a_lot():
+            i = 0
+            while i < 10:
+                i += 1
+                a = [1] * 10
+                j = 0
+                while j < 20:
+                    j += 1
+                    a.append(j)
+        fn = self.getcompiled(malloc_a_lot)
+        fn()
+
+    def test__del__(self):
+        class State:
+            pass
+        s = State()
+        class A(object):
+            def __del__(self):
+                s.a_dels += 1
+        class B(A):
+            def __del__(self):
+                s.b_dels += 1
+        class C(A):
+            pass
+        def f():
+            s.a_dels = 0
+            s.b_dels = 0
+            A()
+            B()
+            C()
+            A()
+            B()
+            C()
+            return s.a_dels * 10 + s.b_dels
+        fn = self.getcompiled(f)
+        res = f()
+        assert res == 42
+        res = fn() #does not crash
+        res = fn() #does not crash
+        assert 0 <= res <= 42 # 42 cannot be guaranteed



More information about the Pypy-commit mailing list