Ejemplo n.º 1
0
    def test_propagation(self):
        exceptions = []
        co = coroutine()
        co2 = coroutine()

        def f(main):
            main.switch()

        co.bind(f, coroutine.getcurrent())
        co.switch()

        try:
            co.throw(RuntimeError)
        except RuntimeError:
            exceptions.append(1)

        def f2():
            raise RuntimeError

        co2.bind(f2)

        try:
            co2.switch()
        except RuntimeError:
            exceptions.append(2)

        assert exceptions == [1, 2]
Ejemplo n.º 2
0
    def test_propagation(self):
        exceptions = []
        co = coroutine()
        co2 = coroutine()

        def f(main):
            main.switch()

        co.bind(f, coroutine.getcurrent())
        co.switch()

        try:
            co.throw(RuntimeError)
        except RuntimeError:
            exceptions.append(1)

        def f2():
            raise RuntimeError

        co2.bind(f2)

        try:
            co2.switch()
        except RuntimeError:
            exceptions.append(2)

        assert exceptions == [1, 2]
Ejemplo n.º 3
0
    def test_is_zombie_del_with_frame(self):
        import gc
        res = []

        class MyCoroutine(coroutine):
            def __del__(self):
                res.append(self.is_zombie)

        main = coroutine.getcurrent()

        def f():
            print 'in coro'
            main.switch()

        co = MyCoroutine()
        co.bind(f)
        co.switch()
        del co
        for i in range(10):
            gc.collect()
            if res:
                break
        co = coroutine()
        co.bind(f)
        co.switch()
        assert res[0], "is_zombie was False in __del__"
Ejemplo n.º 4
0
 def test_backto_main(self):
     maintask = coroutine.getcurrent()
     def f(task):
         task.switch()
     co = coroutine()
     co.bind(f,maintask)
     co.switch()
Ejemplo n.º 5
0
    def test_is_zombie_del_with_frame(self):
        import gc

        res = []

        class MyCoroutine(coroutine):
            def __del__(self):
                res.append(self.is_zombie)

        main = coroutine.getcurrent()

        def f():
            print("in coro")
            main.switch()

        co = MyCoroutine()
        co.bind(f)
        co.switch()
        del co
        for i in range(10):
            gc.collect()
            if res:
                break
        co = coroutine()
        co.bind(f)
        co.switch()
        assert res[0], "is_zombie was False in __del__"
Ejemplo n.º 6
0
 def test_simple_task(self):
     maintask = coroutine.getcurrent()
     def f():pass
     co = coroutine()
     co.bind(f)
     co.switch()
     assert not co.is_alive
     assert maintask is coroutine.getcurrent()
Ejemplo n.º 7
0
    def test_bogus_bind(self):
        co = coroutine()

        def f():
            pass

        co.bind(f)
        raises(ValueError, co.bind, f)
Ejemplo n.º 8
0
    def test_bogus_bind(self):
        co = coroutine()

        def f():
            pass

        co.bind(f)
        raises(ValueError, co.bind, f)
Ejemplo n.º 9
0
 def test_is_zombie(self):
     co = coroutine()
     def f():
         print 'in coro'
     assert not co.is_zombie
     co.bind(f)
     assert not co.is_zombie
     co.switch()
     assert not co.is_zombie
Ejemplo n.º 10
0
    def test_backto_main(self):
        maintask = coroutine.getcurrent()

        def f(task):
            task.switch()

        co = coroutine()
        co.bind(f, maintask)
        co.switch()
Ejemplo n.º 11
0
 def test_kill(self):
     co = coroutine()
     def f():
         pass
     assert not co.is_alive
     co.bind(f)
     assert co.is_alive
     co.kill()
     assert not co.is_alive
Ejemplo n.º 12
0
    def test_is_zombie(self):
        co = coroutine()

        def f():
            print 'in coro'

        assert not co.is_zombie
        co.bind(f)
        assert not co.is_zombie
        co.switch()
        assert not co.is_zombie
Ejemplo n.º 13
0
    def test_simple_task(self):
        maintask = coroutine.getcurrent()

        def f():
            pass

        co = coroutine()
        co.bind(f)
        co.switch()
        assert not co.is_alive
        assert maintask is coroutine.getcurrent()
Ejemplo n.º 14
0
 def test_raise_propagate(self):
     co = coroutine()
     def f():
         return 1/0
     co.bind(f)
     try:
         co.switch()
     except ZeroDivisionError:
         pass
     else:
         raise AssertionError("exception not propagated")
Ejemplo n.º 15
0
    def test_kill(self):
        co = coroutine()

        def f():
            pass

        assert not co.is_alive
        co.bind(f)
        assert co.is_alive
        co.kill()
        assert not co.is_alive
Ejemplo n.º 16
0
 def test_throw(self):
     exceptions = []
     co = coroutine()
     def f(main):
         try:
             main.switch()
         except RuntimeError:
             exceptions.append(True)
     
     co.bind(f, coroutine.getcurrent())
     co.switch()
     co.throw(RuntimeError)
     assert exceptions == [True]
Ejemplo n.º 17
0
def spawn(spawn_callable, *pargs, **kwargs):
    
    print >>sys.stderr, pargs
    new_coro = stackless.coroutine()
    new_coro.bind(start_script, spawn_callable, pargs)
    save_obj = ResumeState(None, new_coro)
    with MaybeFile() as new_coro_fp:
        pickle.dump(save_obj, new_coro_fp)
        out_dict = {"request": "spawn", "coro_descriptor": dict()}
        describe_maybe_file(new_coro_fp, out_dict["coro_descriptor"])
    out_dict.update(kwargs)
    response = message_helper.synchronous_request(out_dict)
    return response["outputs"]
Ejemplo n.º 18
0
def spawn(spawn_callable, *pargs, **kwargs):

    print >> sys.stderr, pargs
    new_coro = stackless.coroutine()
    new_coro.bind(start_script, spawn_callable, pargs)
    save_obj = ResumeState(None, new_coro)
    with MaybeFile() as new_coro_fp:
        pickle.dump(save_obj, new_coro_fp)
        out_dict = {"request": "spawn", "coro_descriptor": dict()}
        describe_maybe_file(new_coro_fp, out_dict["coro_descriptor"])
    out_dict.update(kwargs)
    response = message_helper.synchronous_request(out_dict)
    return response["outputs"]
Ejemplo n.º 19
0
    def test_raise_propagate(self):
        co = coroutine()

        def f():
            return 1 / 0

        co.bind(f)
        try:
            co.switch()
        except ZeroDivisionError:
            pass
        else:
            raise AssertionError("exception not propagated")
Ejemplo n.º 20
0
    def test_wrapped_main(self):
        class mwrap(object):
            def __init__(self, coro):
                self._coro = coro

            def __getattr__(self, attr):
                return getattr(self._coro, attr)

        maintask = mwrap(coroutine.getcurrent())
        def f(task):
            task.switch()
        co = coroutine()
        co.bind(f,maintask)
        co.switch()
Ejemplo n.º 21
0
    def test_throw(self):
        exceptions = []
        co = coroutine()

        def f(main):
            try:
                main.switch()
            except RuntimeError:
                exceptions.append(True)

        co.bind(f, coroutine.getcurrent())
        co.switch()
        co.throw(RuntimeError)
        assert exceptions == [True]
Ejemplo n.º 22
0
    def test_wrapped_main(self):
        class mwrap(object):
            def __init__(self, coro):
                self._coro = coro

            def __getattr__(self, attr):
                return getattr(self._coro, attr)

        maintask = mwrap(coroutine.getcurrent())

        def f(task):
            task.switch()

        co = coroutine()
        co.bind(f, maintask)
        co.switch()
Ejemplo n.º 23
0
 def test_strange_test(self):
     def f():
         return 42
     def create():
         b = coroutine()
         b.bind(f)
         b.switch()
         return b
     a = coroutine()
     a.bind(create)
     b = a.switch()
     def nothing():
         pass
     a.bind(nothing)
     def kill():
         a.kill()
     b.bind(kill)
     b.switch()
Ejemplo n.º 24
0
    def test_catch_coroutineexit(self):
        coroutineexit = []
        co_a = coroutine()
        co_test = coroutine.getcurrent()

        def a():
            try:
                co_test.switch()
            except CoroutineExit:
                coroutineexit.append(True)
                raise

        co_a.bind(a)
        co_a.switch()
        assert co_a.is_alive

        co_a.kill()
        assert coroutineexit == [True]
        assert not co_a.is_alive
Ejemplo n.º 25
0
    def test_catch_coroutineexit(self):
        coroutineexit = []
        co_a = coroutine()
        co_test = coroutine.getcurrent()

        def a():
            try:
                co_test.switch()
            except CoroutineExit:
                coroutineexit.append(True)
                raise

        co_a.bind(a)
        co_a.switch()
        assert co_a.is_alive

        co_a.kill()
        assert coroutineexit == [True]
        assert not co_a.is_alive
Ejemplo n.º 26
0
    def test_strange_test(self):
        def f():
            return 42

        def create():
            b = coroutine()
            b.bind(f)
            b.switch()
            return b

        a = coroutine()
        a.bind(create)
        b = a.switch()

        def nothing():
            pass

        a.bind(nothing)

        def kill():
            a.kill()

        b.bind(kill)
        b.switch()
Ejemplo n.º 27
0
Archivo: skypy.py Proyecto: ms705/ciel
def create_coroutine(spawn_callable, pargs):
 
    new_coro = stackless.coroutine()
    new_coro.bind(start_script, spawn_callable, pargs)
    return new_coro
Ejemplo n.º 28
0
    signal.signal(signal.SIGINT, interrupt_handler)
    res = coro.switch()
    if res is None and coro.is_alive:  # interrupted!
        print "interrupted! writing %s..." % (filename, )
        f = open(filename, 'w')
        pickle.dump(coro, f)
        f.close()
        print "done"
    else:
        print "result:", res


try:
    operation, filename = sys.argv[1:]
except ValueError:
    print __doc__
    sys.exit(2)

if operation == '--start':
    coro = coroutine()
    coro.bind(ackermann, 3, 7)
    print "running from the start..."
    execute(coro)
elif operation == '--resume':
    print "reloading %s..." % (filename, )
    f = open(filename)
    coro = pickle.load(f)
    f.close()
    print "done, running now..."
    execute(coro)
Ejemplo n.º 29
0
 def create():
     b = coroutine()
     b.bind(f)
     b.switch()
     return b
Ejemplo n.º 30
0
 def create():
     b = coroutine()
     b.bind(f)
     b.switch()
     return b
Ejemplo n.º 31
0
print >>sys.stderr, "SkyPy: Awaiting task"
entry_dict = pickle.load(sys.stdin)

skypy.main_coro = stackless.coroutine.getcurrent()
user_script_namespace = imp.load_source("user_script_namespace", entry_dict["source_filename"])

if "coro_filename" in entry_dict:
    print >>sys.stderr, "SkyPy: Resuming"
    resume_fp = open(entry_dict["coro_filename"], "r")
    resume_state = pickle.load(resume_fp)
    resume_fp.close()
    user_coro = resume_state.coro
else:
    print >>sys.stderr, "Entering at", entry_dict["entry_point"], "args", entry_dict["entry_args"]
    skypy.persistent_state = skypy.PersistentState()
    user_coro = stackless.coroutine()
    user_coro.bind(skypy.start_script, user_script_namespace.__dict__[entry_dict["entry_point"]], entry_dict["entry_args"])
    resume_state = skypy.ResumeState(skypy.persistent_state, user_coro)
if not entry_dict["is_continuation"]:
    resume_state.persistent_state = skypy.PersistentState()
    resume_state.persistent_state.export_json = entry_dict["export_json"]
    resume_state.persistent_state.ret_output = entry_dict["ret_output"]
    resume_state.persistent_state.extra_outputs = entry_dict["extra_outputs"]
skypy.persistent_state = resume_state.persistent_state
skypy.extra_outputs = skypy.persistent_state.extra_outputs

skypy.file_outputs = FileOutputRecords()
skypy.message_helper = RpcHelper(sys.stdin, sys.stdout, skypy.file_outputs)
skypy.file_outputs.set_message_helper(skypy.message_helper)

user_coro.switch()
Ejemplo n.º 32
0
def execute(coro):
    signal.signal(signal.SIGINT, interrupt_handler)
    res = coro.switch()
    if res is None and coro.is_alive:    # interrupted!
        print "interrupted! writing %s..." % (filename,)
        f = open(filename, 'w')
        pickle.dump(coro, f)
        f.close()
        print "done"
    else:
        print "result:", res

try:
    operation, filename = sys.argv[1:]
except ValueError:
    print __doc__
    sys.exit(2)

if operation == '--start':
    coro = coroutine()
    coro.bind(ackermann, 3, 7)
    print "running from the start..."
    execute(coro)
elif operation == '--resume':
    print "reloading %s..." % (filename,)
    f = open(filename)
    coro = pickle.load(f)
    f.close()
    print "done, running now..."
    execute(coro)
Ejemplo n.º 33
0
skypy.main_coro = stackless.coroutine.getcurrent()
user_script_namespace = imp.load_source("user_script_namespace",
                                        entry_dict["source_filename"])

if "coro_filename" in entry_dict:
    print >> sys.stderr, "SkyPy: Resuming"
    resume_fp = open(entry_dict["coro_filename"], "r")
    resume_state = pickle.load(resume_fp)
    resume_fp.close()
    user_coro = resume_state.coro
else:
    print >> sys.stderr, "Entering at", entry_dict[
        "entry_point"], "args", entry_dict["entry_args"]
    skypy.persistent_state = skypy.PersistentState()
    user_coro = stackless.coroutine()
    user_coro.bind(skypy.start_script,
                   user_script_namespace.__dict__[entry_dict["entry_point"]],
                   entry_dict["entry_args"])
    resume_state = skypy.ResumeState(skypy.persistent_state, user_coro)
if not entry_dict["is_continuation"]:
    resume_state.persistent_state = skypy.PersistentState()
    resume_state.persistent_state.export_json = entry_dict["export_json"]
    resume_state.persistent_state.ret_output = entry_dict["ret_output"]
    resume_state.persistent_state.extra_outputs = entry_dict["extra_outputs"]
skypy.persistent_state = resume_state.persistent_state
skypy.extra_outputs = skypy.persistent_state.extra_outputs

skypy.file_outputs = FileOutputRecords()
skypy.message_helper = RpcHelper(sys.stdin, sys.stdout, skypy.file_outputs)
skypy.file_outputs.set_message_helper(skypy.message_helper)