[pypy-commit] pypy stacklet: Tests and fixes for switch(to=xx) not checking the state of xx.

arigo noreply at buildbot.pypy.org
Fri Aug 19 01:29:47 CEST 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: stacklet
Changeset: r46618:a97f46dc7c63
Date: 2011-08-18 22:46 +0200
http://bitbucket.org/pypy/pypy/changeset/a97f46dc7c63/

Log:	Tests and fixes for switch(to=xx) not checking the state of xx.

diff --git a/pypy/module/_continuation/interp_continuation.py b/pypy/module/_continuation/interp_continuation.py
--- a/pypy/module/_continuation/interp_continuation.py
+++ b/pypy/module/_continuation/interp_continuation.py
@@ -52,9 +52,6 @@
         if self.sthread is None:
             start_state.clear()
             raise geterror(self.space, "continulet not initialized yet")
-        if self.sthread.is_empty_handle(self.h):
-            start_state.clear()
-            raise geterror(self.space, "continulet already finished")
         ec = self.check_sthread()
         saved_topframeref = ec.topframeref
         #
@@ -66,9 +63,14 @@
             # double switch: the final destination is to.h
             start_state.destination = to
         #
+        h = start_state.destination.h
+        sthread = self.sthread
+        if sthread.is_empty_handle(h):
+            start_state.clear()
+            raise geterror(self.space, "continulet already finished")
+        #
         try:
-            sthread = self.sthread
-            do_switch(sthread, start_state.destination.h)
+            do_switch(sthread, h)
         except MemoryError:
             start_state.clear()
             raise getmemoryerror(self.space)
@@ -85,8 +87,12 @@
 
     def descr_switch(self, w_value=None, w_to=None):
         to = self.space.interp_w(W_Continulet, w_to, can_be_None=True)
-        if self is to:    # double-switch to myself: no-op
-            return w_value
+        if to is not None:
+            if self is to:    # double-switch to myself: no-op
+                return w_value
+            if to.sthread is None:
+                start_state.clear()
+                raise geterror(self.space, "continulet not initialized yet")
         start_state.w_value = w_value
         return self.switch(to)
 
diff --git a/pypy/module/_continuation/test/test_stacklet.py b/pypy/module/_continuation/test/test_stacklet.py
--- a/pypy/module/_continuation/test/test_stacklet.py
+++ b/pypy/module/_continuation/test/test_stacklet.py
@@ -487,6 +487,31 @@
         assert res == 'z'
         raises(TypeError, c1.switch, to=c2)  # "can't send non-None value"
 
+    def test_switch2_not_initialized_yet(self):
+        from _continuation import continulet, error
+        #
+        def f1(c1):
+            not_reachable
+        #
+        c1 = continulet(f1)
+        c2 = continulet.__new__(continulet)
+        e = raises(error, c1.switch, to=c2)
+        assert str(e.value) == "continulet not initialized yet"
+
+    def test_switch2_already_finished(self):
+        from _continuation import continulet, error
+        #
+        def f1(c1):
+            not_reachable
+        def empty_callback(c):
+            return 42
+        #
+        c1 = continulet(f1)
+        c2 = continulet(empty_callback)
+        c2.switch()
+        e = raises(error, c1.switch, to=c2)
+        assert str(e.value) == "continulet already finished"
+
     def test_throw(self):
         import sys
         from _continuation import continulet


More information about the pypy-commit mailing list