[pypy-svn] r29145 - in pypy/dist/pypy: lib module/_stackless/test

pedronis at codespeak.net pedronis at codespeak.net
Thu Jun 22 17:19:12 CEST 2006


Author: pedronis
Date: Thu Jun 22 17:19:10 2006
New Revision: 29145

Modified:
   pypy/dist/pypy/lib/stackless.py
   pypy/dist/pypy/module/_stackless/test/test_stackless.py
Log:
(arre, pedronis)
make the simplest example of tasklet pickling/unpickling work, added it as a test to run on top of a stackless 
pypy-c



Modified: pypy/dist/pypy/lib/stackless.py
==============================================================================
--- pypy/dist/pypy/lib/stackless.py	(original)
+++ pypy/dist/pypy/lib/stackless.py	Thu Jun 22 17:19:10 2006
@@ -76,9 +76,11 @@
         self.tempval = None
         self._coro = coro
 
-    def __str__(self):
+    def __repr__(self):
         return tasklet.__str__(self)
 
+    __str__ = __repr__
+
     def __getattr__(self,attr):
         return getattr(self._coro,attr)
 
@@ -291,7 +293,7 @@
         self.setup(*argl, **argd)
         return self
 
-    def __str__(self):
+    def __repr__(self):
         next = None
         if self.next is not None:
             next = self.next.thread_id
@@ -304,6 +306,8 @@
             bs = '-'
         return 'T%s(%s) (%s, %s)' % (self.thread_id, bs, next, prev)
 
+    __str__ = __repr__
+
     def bind(self, func):
         """
         Binding a tasklet to a callable object.
@@ -436,13 +440,15 @@
         self.insert()
 
     def __reduce__(self):
+        # xxx save more
         one, two, three = coroutine.__reduce__(self)
         assert one is coroutine
         assert two == ()
-        return tasklet, (), (three, self.tempval)
+        return tasklet, (), (three, self.alive, self.tempval)
 
-    def __setstate__(self, (coro_state, tempval)):
+    def __setstate__(self, (coro_state, alive, tempval)):
         coroutine.__setstate__(self, coro_state)
+        self.alive = alive
         self.tempval = tempval
 
 def channel_callback(chan, task, sending, willblock):

Modified: pypy/dist/pypy/module/_stackless/test/test_stackless.py
==============================================================================
--- pypy/dist/pypy/module/_stackless/test/test_stackless.py	(original)
+++ pypy/dist/pypy/module/_stackless/test/test_stackless.py	Thu Jun 22 17:19:10 2006
@@ -308,6 +308,22 @@
 
         assert output == [(1,), (2,)]
 
+    def test_schedule(self):
+        output = []
+        def print_(*args):
+            output.append(args)
+
+        import stackless
+        def f(i):
+            print_(i)
+
+        stackless.tasklet(f)(1)
+        stackless.tasklet(f)(2)
+        stackless.schedule()
+
+        assert output == [(1,), (2,)]
+
+
     def test_cooperative(self):
         output = []
         def print_(*args):
@@ -327,3 +343,54 @@
         assert output == [('schedule', 1), ('schedule', 2),
                           ('schedule', 1), ('schedule', 2),
                           ('schedule', 1), ('schedule', 2),]
+
+class Test_StacklessPickling:
+
+    def setup_class(cls):
+        skip_on_missing_buildoption(stackless=True)
+
+
+    def test_basic_tasklet_pickling(self):
+        import stackless
+        from stackless import run, schedule, tasklet
+        import pickle
+
+        output = []
+
+        import new
+
+        mod = new.module('mod')
+        mod.output = output
+
+        exec """from stackless import schedule
+        
+def aCallable(name):
+    output.append(('b', name))
+    schedule()
+    output.append(('a', name))
+""" in mod.__dict__
+        import sys
+        sys.modules['mod'] = mod
+        aCallable = mod.aCallable
+
+
+        tasks = []
+        for name in "ABCDE":
+            tasks.append(tasklet(aCallable)(name))
+
+        schedule()
+
+        assert output == [('b', x) for x in "ABCDE"]
+        del output[:]
+        pickledTasks = pickle.dumps(tasks)
+
+        schedule()
+        assert output == [('a', x) for x in "ABCDE"]
+        del output[:]
+        
+        unpickledTasks = pickle.loads(pickledTasks)
+        for task in unpickledTasks:
+            task.insert()
+
+        schedule()
+        assert output == [('a', x) for x in "ABCDE"]



More information about the Pypy-commit mailing list