[pypy-svn] r68760 - in pypy/branch/logging/pypy: rpython rpython/test translator

arigo at codespeak.net arigo at codespeak.net
Mon Oct 26 16:59:28 CET 2009


Author: arigo
Date: Mon Oct 26 16:59:27 2009
New Revision: 68760

Modified:
   pypy/branch/logging/pypy/rpython/annlowlevel.py
   pypy/branch/logging/pypy/rpython/test/test_annlowlevel.py
   pypy/branch/logging/pypy/translator/unsimplify.py
Log:
An interface to allow specialize_call() to ask the rtyper to call a
given function at the end of the RPython program.  Based on
unsimplify.call_final_function(), whacked a bit.



Modified: pypy/branch/logging/pypy/rpython/annlowlevel.py
==============================================================================
--- pypy/branch/logging/pypy/rpython/annlowlevel.py	(original)
+++ pypy/branch/logging/pypy/rpython/annlowlevel.py	Mon Oct 26 16:59:27 2009
@@ -145,6 +145,7 @@
         self.delayedconsts = []
         self.delayedfuncs = []
         self.newgraphs = {}
+        self.c_final_funcs = []
 
     def getgraph(self, ll_function, args_s, s_result):
         # get the graph of the mix-level helper ll_function and prepare it for
@@ -235,6 +236,10 @@
         else:
             return repr.convert_const(obj)
 
+    def register_atexit(self, ll_function):
+        c_func = self.constfunc(ll_function, [], annmodel.s_None)
+        self.c_final_funcs.append(c_func)
+
     def finish(self):
         self.finish_annotate()
         self.finish_rtype()
@@ -288,9 +293,13 @@
             assert FUNCTYPE == REAL
             p._become(real_p)
         rtyper.specialize_more_blocks()
+        for c_func in self.c_final_funcs:
+            from pypy.translator.unsimplify import write_call_to_final_function
+            write_call_to_final_function(rtyper.annotator.translator, c_func)
         self.delayedreprs.clear()
         del self.delayedconsts[:]
         del self.delayedfuncs[:]
+        del self.c_final_funcs[:]
         for graph in translator.graphs[original_graph_count:]:
             self.newgraphs[graph] = True
 

Modified: pypy/branch/logging/pypy/rpython/test/test_annlowlevel.py
==============================================================================
--- pypy/branch/logging/pypy/rpython/test/test_annlowlevel.py	(original)
+++ pypy/branch/logging/pypy/rpython/test/test_annlowlevel.py	Mon Oct 26 16:59:27 2009
@@ -2,13 +2,45 @@
 """ Few tests for annlowlevel helpers
 """
 
+import os
+from pypy.tool.udir import udir
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
 from pypy.rpython.lltypesystem.rstr import mallocstr, mallocunicode
+from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.ootypesystem import ootype
 from pypy.rpython.annlowlevel import hlstr, llstr, oostr
 from pypy.rpython.annlowlevel import hlunicode, llunicode
 
-class TestLLType(BaseRtypingTest, LLRtypeMixin):
+
+class AnnLowLevelTests(BaseRtypingTest):
+
+    def test_register_atexit(self):
+        from pypy.annotation import model as annmodel
+        from pypy.rpython.extregistry import ExtRegistryEntry
+        def callback():
+            pass
+        class CallbackEntry(ExtRegistryEntry):
+            _about_ = callback
+            def compute_result_annotation(self):
+                return annmodel.s_None
+            def specialize_call(self, hop):
+                annhelper = hop.rtyper.getannmixlevel()
+                annhelper.register_atexit(atexit)
+                return hop.inputconst(lltype.Void, None)
+        def f(n):
+            callback()
+            return n * 5
+        def atexit():
+            fd = os.open(tmpfn, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0666)
+            os.close(fd)
+        tmpfn = str(udir.join('%s.test_register_atexit' %
+                              self.__class__.__name__))
+        res = self.interpret(f, [5])
+        assert res == 25
+        assert os.path.exists(tmpfn)
+
+
+class TestLLType(AnnLowLevelTests, LLRtypeMixin):
     def test_hlstr(self):
         s = mallocstr(3)
         s.chars[0] = "a"
@@ -54,7 +86,7 @@
         assert res == 3
 
 
-class TestOOType(BaseRtypingTest, OORtypeMixin):
+class TestOOType(AnnLowLevelTests, OORtypeMixin):
     def test_hlstr(self):
         s = ootype.make_string("abc")
         assert hlstr(s) == "abc"

Modified: pypy/branch/logging/pypy/translator/unsimplify.py
==============================================================================
--- pypy/branch/logging/pypy/translator/unsimplify.py	(original)
+++ pypy/branch/logging/pypy/translator/unsimplify.py	Mon Oct 26 16:59:27 2009
@@ -158,7 +158,6 @@
 def call_final_function(translator, final_func, annhelper=None):
     """When the program finishes normally, call 'final_func()'."""
     from pypy.annotation import model as annmodel
-    from pypy.rpython.lltypesystem import lltype
     from pypy.rpython.annlowlevel import MixLevelHelperAnnotator
 
     own_annhelper = (annhelper is None)
@@ -167,7 +166,10 @@
     c_final_func = annhelper.constfunc(final_func, [], annmodel.s_None)
     if own_annhelper:
         annhelper.finish()
+    write_call_to_final_function(translator, c_final_func)
 
+def write_call_to_final_function(translator, c_final_func):
+    from pypy.rpython.lltypesystem import lltype
     entry_point = translator.graphs[0]
     v = copyvar(translator.annotator, entry_point.getreturnvar())
     extrablock = Block([v])



More information about the Pypy-commit mailing list