| 1 | from py.test import skip |
| 2 | try: |
| 3 | import stackless |
| 4 | except ImportError: |
| 5 | try: |
| 6 | from lib_pypy import stackless as stackless |
| 7 | except ImportError as e: |
| 8 | skip('cannot import stackless: %s' % (e,)) |
| 9 | |
| 10 | |
| 11 | |
| 12 | class Test_StacklessPickling: |
| 13 | |
| 14 | def test_pickle_main_coroutine(self): |
| 15 | import stackless, pickle |
| 16 | s = pickle.dumps(stackless.coroutine.getcurrent()) |
| 17 | print(s) |
| 18 | c = pickle.loads(s) |
| 19 | assert c is stackless.coroutine.getcurrent() |
| 20 | |
| 21 | def test_basic_tasklet_pickling(self): |
| 22 | import stackless |
| 23 | from stackless import run, schedule, tasklet |
| 24 | import pickle |
| 25 | |
| 26 | output = [] |
| 27 | |
| 28 | import new |
| 29 | |
| 30 | mod = new.module('mod') |
| 31 | mod.output = output |
| 32 | |
| 33 | exec("""from stackless import schedule |
| 34 | |
| 35 | def aCallable(name): |
| 36 | output.append(('b', name)) |
| 37 | schedule() |
| 38 | output.append(('a', name)) |
| 39 | """, mod.__dict__) |
| 40 | import sys |
| 41 | sys.modules['mod'] = mod |
| 42 | aCallable = mod.aCallable |
| 43 | |
| 44 | |
| 45 | tasks = [] |
| 46 | for name in "ABCDE": |
| 47 | tasks.append(tasklet(aCallable)(name)) |
| 48 | |
| 49 | schedule() |
| 50 | |
| 51 | assert output == [('b', x) for x in "ABCDE"] |
| 52 | del output[:] |
| 53 | pickledTasks = pickle.dumps(tasks) |
| 54 | |
| 55 | schedule() |
| 56 | assert output == [('a', x) for x in "ABCDE"] |
| 57 | del output[:] |
| 58 | |
| 59 | unpickledTasks = pickle.loads(pickledTasks) |
| 60 | for task in unpickledTasks: |
| 61 | task.insert() |
| 62 | |
| 63 | schedule() |
| 64 | assert output == [('a', x) for x in "ABCDE"] |
| 65 |