[pypy-svn] r28459 - in pypy/dist/pypy: interpreter module/stackless module/stackless/test

tismer at codespeak.net tismer at codespeak.net
Wed Jun 7 17:00:18 CEST 2006


Author: tismer
Date: Wed Jun  7 17:00:17 2006
New Revision: 28459

Modified:
   pypy/dist/pypy/interpreter/executioncontext.py
   pypy/dist/pypy/module/stackless/coroutine.py
   pypy/dist/pypy/module/stackless/test/test_coroutine.py
Log:
initial coroutine pickling. We just cannot test the real functionality
without compiling the resume point stuff into a binary ;-)

Modified: pypy/dist/pypy/interpreter/executioncontext.py
==============================================================================
--- pypy/dist/pypy/interpreter/executioncontext.py	(original)
+++ pypy/dist/pypy/interpreter/executioncontext.py	Wed Jun  7 17:00:17 2006
@@ -58,7 +58,25 @@
         current.is_tracing = self.is_tracing
 
     # coroutine: I think this is all, folks!
-    
+
+    # well, not quite: we need an interface for pickling
+    def subcontext_getstate(coobj):
+        # we just save the framestack
+        space = coobj.space
+        items = [space.wrap(item) for item in coobj.framestack.items]
+        return space.newtuple(items)
+    subcontext_getstate = staticmethod(subcontext_getstate)
+
+    def subcontext_setstate(coobj, w_state):
+        from pypy.interpreter.pyframe import PyFrame
+        space = coobj.space
+        items = [space.interp_w(PyFrame, item)
+                 for item in space.unpackiterable(w_state)]
+        coobj.framestack.items = items
+    subcontext_setstate = staticmethod(subcontext_setstate)
+
+    # coroutine: now I really I think this is all, folks!
+
     def get_builtin(self):
         try:
             return self.framestack.top().builtin

Modified: pypy/dist/pypy/module/stackless/coroutine.py
==============================================================================
--- pypy/dist/pypy/module/stackless/coroutine.py	(original)
+++ pypy/dist/pypy/module/stackless/coroutine.py	Wed Jun  7 17:00:17 2006
@@ -102,6 +102,38 @@
         return space.wrap(AppCoroutine._get_state(space).current)
     w_getcurrent = staticmethod(w_getcurrent)
 
+    # pickling interface
+    def descr__reduce__(self, space):
+        # this is trying to be simplistic at the moment.
+        # we neither allow to pickle main (which can become a mess
+        # since it has some deep anchestor frames)
+        # nor we allowto pickle the current coroutine.
+        # rule: switch before pickling.
+        # you cannot construct the tree that you are climbing.
+        
+        from pypy.interpreter.mixedmodule import MixedModule
+        w_mod    = space.getbuiltinmodule('stackless')
+        mod      = space.interp_w(MixedModule, w_mod)
+        new_inst = mod.get('coroutine')
+        w        = space.wrap
+        nt = space.newtuple
+        ec = self.space.getexecutioncontext()
+
+        tup_base = [
+            ]
+        tup_state = [
+            w(self.flags),
+            ec.subcontext_getstate(self),
+            ]
+
+        return nt([new_inst, nt(tup_base), nt(tup_state)])
+
+    def descr__setstate__(self, space, w_args):
+        args_w = space.unpackiterable(w_args)
+        w_flags, w_state = args_w
+        self.flags = space.int_w(w_flags)
+        ec = self.space.getexecutioncontext()
+        ec.subcontext_setstate(self, w_state)
 
 # _mixin_ did not work
 for methname in StacklessFlags.__dict__:
@@ -140,6 +172,11 @@
     kill = interp2app(AppCoroutine.w_kill),
     is_zombie = GetSetProperty(AppCoroutine.w_get_is_zombie, doc=AppCoroutine.get_is_zombie.__doc__),
     getcurrent = interp2app(AppCoroutine.w_getcurrent),
+    __reduce__   = interp2app(AppCoroutine.descr__reduce__,
+                              unwrap_spec=['self', ObjSpace]),
+    __setstate__ = interp2app(AppCoroutine.descr__setstate__,
+                              unwrap_spec=['self', ObjSpace, W_Root]),
+    __module__ = 'stackless',
 )
 
 class AppCoState(BaseCoState):

Modified: pypy/dist/pypy/module/stackless/test/test_coroutine.py
==============================================================================
--- pypy/dist/pypy/module/stackless/test/test_coroutine.py	(original)
+++ pypy/dist/pypy/module/stackless/test/test_coroutine.py	Wed Jun  7 17:00:17 2006
@@ -13,3 +13,15 @@
         print stackless.__file__
         co = stackless.coroutine()
         print co
+        # not much we can do here without compiling.
+        # well, we can pickle, at least:
+
+    def test_pickle_coroutine(self):
+        # this test is limited to basic pickling.
+        # real stacks can only tested with a stackless pypy build.
+        import stackless
+        co = stackless.coroutine()
+        import pickle
+        pckl = pickle.dumps(co)
+        co2 = pickle.loads(pckl)
+        
\ No newline at end of file



More information about the Pypy-commit mailing list