Esempio n. 1
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()
Esempio n. 2
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()
Esempio 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__"
Esempio n. 4
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]
Esempio 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__"
Esempio n. 6
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]
Esempio n. 7
0
 def test_backto_main(self):
     maintask = coroutine.getcurrent()
     def f(task):
         task.switch()
     co = coroutine()
     co.bind(f,maintask)
     co.switch()
Esempio n. 8
0
    def test_backto_main(self):
        maintask = coroutine.getcurrent()

        def f(task):
            task.switch()

        co = coroutine()
        co.bind(f, maintask)
        co.switch()
Esempio n. 9
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]
Esempio n. 10
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]
Esempio n. 11
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()
Esempio n. 12
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()
Esempio n. 13
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
Esempio n. 14
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
Esempio n. 15
0
except ImportError:
    print __doc__
    sys.exit(2)


def ackermann(x, y):
    check()
    if x == 0:
        return y + 1
    if y == 0:
        return ackermann(x - 1, 1)
    return ackermann(x - 1, ackermann(x, y - 1))

# ____________________________________________________________

main = coroutine.getcurrent()
sys.setrecursionlimit(100000)

interrupt_flag = False

def interrupt_handler(*args):
    global interrupt_flag
    interrupt_flag = True

def check():
    if interrupt_flag:
        main.switch()


def execute(coro):
    signal.signal(signal.SIGINT, interrupt_handler)
Esempio n. 16
0
    print __doc__
    sys.exit(2)


def ackermann(x, y):
    check()
    if x == 0:
        return y + 1
    if y == 0:
        return ackermann(x - 1, 1)
    return ackermann(x - 1, ackermann(x, y - 1))


# ____________________________________________________________

main = coroutine.getcurrent()
sys.setrecursionlimit(100000)

interrupt_flag = False


def interrupt_handler(*args):
    global interrupt_flag
    interrupt_flag = True


def check():
    if interrupt_flag:
        main.switch()