From 00cd2cab74947602c38a232c2076b6857b74dd83 Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Fri, 26 Feb 2021 10:39:29 +0100 Subject: Tests (passing) for _continulet switching to a different thread --- pypy/module/_continuation/interp_continuation.py | 2 +- pypy/module/_continuation/test/test_translated.py | 80 +++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/pypy/module/_continuation/interp_continuation.py b/pypy/module/_continuation/interp_continuation.py index f00fff154b..db1724986d 100644 --- a/pypy/module/_continuation/interp_continuation.py +++ b/pypy/module/_continuation/interp_continuation.py @@ -24,7 +24,7 @@ class W_Continulet(W_Root): ec = self.space.getexecutioncontext() if ec.stacklet_thread is not self.sthread: global_state.clear() - raise geterror(self.space, "inter-thread support is missing") + raise geterror(self.space, "cannot switch to a different thread") def descr_init(self, w_callable, __args__): if self.sthread is not None: diff --git a/pypy/module/_continuation/test/test_translated.py b/pypy/module/_continuation/test/test_translated.py index a2f83d0b8f..5f4bdfb76b 100644 --- a/pypy/module/_continuation/test/test_translated.py +++ b/pypy/module/_continuation/test/test_translated.py @@ -117,6 +117,86 @@ class AppTestWrapper: _vmprof.disable() f.close() + def test_thread_switch_to_sub(self): + try: + import thread, time + except ImportError: + py.test.skip("no threads") + c_list = [] + lock = thread.allocate_lock() + lock.acquire() + lock2 = thread.allocate_lock() + lock2.acquire() + # + def fn(): + c = _continuation.continulet(lambda c_main: c_main.switch()) + c.switch() + c_list.append(c) + lock.release() + lock2.acquire() + # + thread.start_new_thread(fn, ()) + lock.acquire() + [c] = c_list + py.test.raises(_continuation.error, c.switch) + # + lock2.release() + time.sleep(0.5) + py.test.raises(_continuation.error, c.switch) + + def test_thread_switch_to_sub_nonstarted(self): + try: + import thread, time + except ImportError: + py.test.skip("no threads") + c_list = [] + lock = thread.allocate_lock() + lock.acquire() + lock2 = thread.allocate_lock() + lock2.acquire() + # + def fn(): + c = _continuation.continulet(lambda c_main: None) + c_list.append(c) + lock.release() + lock2.acquire() + # + thread.start_new_thread(fn, ()) + lock.acquire() + [c] = c_list + py.test.raises(_continuation.error, c.switch) + # + lock2.release() + time.sleep(0.5) + py.test.raises(_continuation.error, c.switch) + + def test_thread_switch_to_main(self): + try: + import thread, time + except ImportError: + py.test.skip("no threads") + c_list = [] + lock = thread.allocate_lock() + lock.acquire() + lock2 = thread.allocate_lock() + lock2.acquire() + # + def fn(): + def in_continulet(c_main): + c_list.append(c_main) + lock.release() + lock2.acquire() + c = _continuation.continulet(in_continulet) + c.switch() + # + thread.start_new_thread(fn, ()) + lock.acquire() + [c] = c_list + py.test.raises(_continuation.error, c.switch) + # + lock2.release() + time.sleep(0.5) + py.test.raises(_continuation.error, c.switch) def _setup(): for _i in range(20): -- cgit v1.2.3-65-gdbad