Example #1
0
    def test_greenlet_tracing(self):
        main = greenlet.getcurrent()
        actions = []

        def trace(*args):
            actions.append(args)

        def dummy():
            pass

        def dummyexc():
            raise SomeError()

        oldtrace = greenlet.settrace(trace)
        try:
            g1 = greenlet.greenlet(dummy)
            g1.switch()
            g2 = greenlet.greenlet(dummyexc)
            py.test.raises(SomeError, g2.switch)
        finally:
            greenlet.settrace(oldtrace)
        assert actions == [
            ('switch', (main, g1)),
            ('switch', (g1, main)),
            ('switch', (main, g2)),
            ('throw', (g2, main)),
        ]
Example #2
0
 def run_thread():
     main = greenlet.getcurrent()
     assert main not in mains
     mains.append(main)
     try:
         mains[0].switch()
     except Exception as e:
         got_exception.append(e)
     lock.release()
Example #3
0
 def test_exception_disables_tracing(self):
     main = greenlet.getcurrent()
     actions = []
     def trace(*args):
         actions.append(args)
         raise SomeError()
     def dummy():
         main.switch()
     g = greenlet.greenlet(dummy)
     g.switch()
     oldtrace = greenlet.settrace(trace)
     try:
         py.test.raises(SomeError, g.switch)
         assert greenlet.gettrace() is None
     finally:
         greenlet.settrace(oldtrace)
     assert actions == [
         ('switch', (main, g)),
     ]
Example #4
0
    def test_exception_disables_tracing(self):
        main = greenlet.getcurrent()
        actions = []

        def trace(*args):
            actions.append(args)
            raise SomeError()

        def dummy():
            main.switch()

        g = greenlet.greenlet(dummy)
        g.switch()
        oldtrace = greenlet.settrace(trace)
        try:
            py.test.raises(SomeError, g.switch)
            assert greenlet.gettrace() is None
        finally:
            greenlet.settrace(oldtrace)
        assert actions == [
            ('switch', (main, g)),
        ]
Example #5
0
    def test_cannot_switch_to_main_of_different_thread(self):
        mains = []
        mains.append(greenlet.getcurrent())
        lock = thread.allocate_lock()
        lock.acquire()
        got_exception = []

        #
        def run_thread():
            main = greenlet.getcurrent()
            assert main not in mains
            mains.append(main)
            try:
                mains[0].switch()
            except Exception as e:
                got_exception.append(e)
            lock.release()

        #
        thread.start_new_thread(run_thread, ())
        lock.acquire()
        assert isinstance(got_exception[0], greenlet.error)
Example #6
0
 def test_greenlet_tracing(self):
     main = greenlet.getcurrent()
     actions = []
     def trace(*args):
         actions.append(args)
     def dummy():
         pass
     def dummyexc():
         raise SomeError()
     oldtrace = greenlet.settrace(trace)
     try:
         g1 = greenlet.greenlet(dummy)
         g1.switch()
         g2 = greenlet.greenlet(dummyexc)
         py.test.raises(SomeError, g2.switch)
     finally:
         greenlet.settrace(oldtrace)
     assert actions == [
         ('switch', (main, g1)),
         ('switch', (g1, main)),
         ('switch', (main, g2)),
         ('throw', (g2, main)),
     ]