[pypy-svn] r29769 - pypy/extradoc/talk/ep2006

arigo at codespeak.net arigo at codespeak.net
Fri Jul 7 19:18:13 CEST 2006


Author: arigo
Date: Fri Jul  7 19:18:08 2006
New Revision: 29769

Added:
   pypy/extradoc/talk/ep2006/test1.py   (contents, props changed)
   pypy/extradoc/talk/ep2006/test2.py   (contents, props changed)
Log:
Simplified cloning example that works on llinterp (test2.py) but fails on genc (translate.py test1).


Added: pypy/extradoc/talk/ep2006/test1.py
==============================================================================
--- (empty file)
+++ pypy/extradoc/talk/ep2006/test1.py	Fri Jul  7 19:18:08 2006
@@ -0,0 +1,92 @@
+from pypy.module._stackless.interp_coroutine import AbstractThunk
+from pypy.module._stackless.interp_clonable import ClonableCoroutine
+import os
+
+class ChoicePointHolder(object):
+    def __init__(self):
+        self.choicepoints = []
+        self.clone_me = False
+        self.answer = 0
+
+    def next_choice(self):
+        return self.choicepoints.pop()
+
+    def add(self, choice, answer=0):
+        self.choicepoints.append((choice, answer))
+
+    def more_choices(self):
+        return bool(self.choicepoints)
+
+    def choice(self):
+        os.write(1, "choice\n")
+        self.clone_me = True
+        self.g_main.switch()
+        os.write(1, "answer: %d\n" % (self.answer,))
+        return self.answer
+
+    def fail(self):
+        self.g_main.switch()
+        assert False
+
+choicepoints = ChoicePointHolder()
+
+# ____________________________________________________________
+
+invalid_branches = [
+##    [0, 0, 0],
+##    [0, 0, 1],
+##    [0, 1],
+##    [1, 0, 0, 0],
+##    [1, 0, 0, 0, 0],
+##    [1, 0, 0, 0, 1],
+##    [1, 0, 1],
+##    [1, 1, 0, 0],
+    ]
+
+class SearchTask(AbstractThunk):
+    def call(self):
+        path = []
+        res = choicepoints.choice()
+        path.append(res)
+        os.write(1, "A. {%x} trying: %s\n" % (id(path), path))
+        res = choicepoints.choice()
+        path.append(res)
+        os.write(1, "B. {%x} trying: %s\n" % (id(path), path))
+        res = choicepoints.choice()
+        assert len(path) == 2
+        path.append(res)
+        os.write(1, "C. {%x} found a solution: %s\n" % (id(path), path))
+
+# ____________________________________________________________
+
+
+class SearchAllTask(AbstractThunk):
+    def call(self):
+        search_coro = ClonableCoroutine()
+        search_coro.bind(SearchTask())
+        choicepoints.add(search_coro)
+
+        os.write(1, "starting\n")
+        while choicepoints.more_choices():
+            searcher, nextvalue = choicepoints.next_choice()
+            choicepoints.clone_me = False
+            choicepoints.answer = nextvalue
+            os.write(1, '<<< {%x} %d\n' % (id(searcher), nextvalue))
+            searcher.switch()
+            os.write(1, '>>> %d\n' % (choicepoints.clone_me,))
+            if choicepoints.clone_me:
+                searcher2 = searcher.clone()
+                os.write(1, 'searcher = {%x}, searcher2 = {%x}\n' % (
+                    id(searcher), id(searcher2)))
+                choicepoints.add(searcher, 5)
+                choicepoints.add(searcher2, 4)
+
+def entry_point(argv):
+    choicepoints.g_main = ClonableCoroutine()
+    choicepoints.g_main.bind(SearchAllTask())
+    choicepoints.g_main.switch()
+    return 0
+
+
+def target(*args):
+    return entry_point, None

Added: pypy/extradoc/talk/ep2006/test2.py
==============================================================================
--- (empty file)
+++ pypy/extradoc/talk/ep2006/test2.py	Fri Jul  7 19:18:08 2006
@@ -0,0 +1,27 @@
+import os
+
+execfile('test1.py')
+
+def entry_point():
+    choicepoints.g_main = ClonableCoroutine()
+    choicepoints.g_main.bind(SearchAllTask())
+    choicepoints.g_main.switch()
+    return 0
+
+# ____________________________________________________________
+
+from pypy.rpython.memory.test.test_transformed_gc import TestStacklessMarkSweepGC
+from pypy.translator.c import gc
+from pypy.rpython.memory import gctransform
+
+
+class TGC(TestStacklessMarkSweepGC):
+    class gcpolicy(gc.StacklessFrameworkGcPolicy):
+        class transformerclass(gctransform.StacklessFrameworkGCTransformer):
+            GC_PARAMS = {'start_heap_size': 99999999 }
+
+
+if __name__ == '__main__':
+    gctest = TGC()
+    run = gctest.runner(entry_point)
+    run([])



More information about the Pypy-commit mailing list