[pypy-svn] r22610 - pypy/dist/pypy/translator/c/test

tismer at codespeak.net tismer at codespeak.net
Tue Jan 24 18:05:24 CET 2006


Author: tismer
Date: Tue Jan 24 18:05:23 2006
New Revision: 22610

Modified:
   pypy/dist/pypy/translator/c/test/test_coroutine.py
Log:
(Eric, Jacob, Christian)   some refinements to coroutines, as they will be used in the stackless module most probably.

Modified: pypy/dist/pypy/translator/c/test/test_coroutine.py
==============================================================================
--- pypy/dist/pypy/translator/c/test/test_coroutine.py	(original)
+++ pypy/dist/pypy/translator/c/test/test_coroutine.py	Tue Jan 24 18:05:23 2006
@@ -70,9 +70,10 @@
 
 
 class CoState(object):
-    pass
+    def __init__(self):
+        self.last = self.current = self.main = Coroutine()
 
-costate = CoState()
+costate = None
 
 class CoroutineDamage(SystemError):
     pass
@@ -81,6 +82,10 @@
 
     def __init__(self):
         self.frame = None
+        if costate is None:
+            self.parent = self
+        else:
+            self.parent = costate.main
 
     def bind(self, thunk):
         if self.frame is not None:
@@ -88,23 +93,27 @@
         self.frame = self._bind(thunk)
 
     def _bind(self, thunk):
-        binder = costate.current
+        self.parent = costate.current
         costate.last.frame = yield_current_frame_to_caller()
         thunk.call()
-        if binder.frame is None:
-            binder = costate.main
-        costate.last, costate.current = costate.current, binder
-        frame, binder.frame = binder.frame, None
-        return frame
+        if self.parent.frame is None:
+            self.parent = costate.main
+        return self._update_state(self.parent)
 
     def switch(self):
         if self.frame is None:
             raise CoroutineDamage
-        costate.last, costate.current = costate.current, self
-        frame, self.frame = self.frame, None
-        costate.last.frame = frame.switch()
+        costate.last.frame = self._update_state(self).switch()
+        # note that last gets updated before assignment!
+
+    def _update_state(new):
+        costate.last, costate.current = costate.current, new
+        frame, new.frame = new.frame, None
+        return frame
+    _update_state = staticmethod(_update_state)
+
+costate = CoState()
 
-costate.current = costate.last = costate.main = Coroutine()
 
 def output(stuff):
     os.write(2, stuff + '\n')



More information about the Pypy-commit mailing list