Armin Rigo avatar Armin Rigo committed 9f78054 Draft

Exception support.

Comments (0)

Files changed (2)

pypy/module/_continuation/interp_continuation.py

 
     def descr_init(self, w_callable):
         if self.h:
-            raise geterror(self.space, "continuation already filled")
+            raise geterror(self.space, "continuation already __init__ialized")
         sthread = self.build_sthread()
         start_state.origin = self
         start_state.w_callable = w_callable
-        self.h = self.sthread.new(new_stacklet_callback)
-        if not self.h:
+        try:
+            self.h = sthread.new(new_stacklet_callback)
+            if sthread.is_empty_handle(self.h):    # early return
+                raise MemoryError
+        except MemoryError:
             raise getmemoryerror(self.space)
 
     def descr_switch(self, w_value=None):
         start_state.w_value = w_value
-        self.h = self.sthread.switch(self.h)
+        try:
+            self.h = self.sthread.switch(self.h)
+        except MemoryError:
+            raise getmemoryerror(self.space)
+        if start_state.propagate_exception:
+            e = start_state.propagate_exception
+            start_state.propagate_exception = None
+            raise e
         w_value = start_state.w_value
         start_state.w_value = None
         return w_value
         self.w_callable = None
         self.args = None
         self.w_value = None
+        self.propagate_exception = None
 start_state = StartState()
 start_state.clear()
 
     self = start_state.origin
     w_callable = start_state.w_callable
     start_state.clear()
-    self.h = self.sthread.switch(h)
+    try:
+        self.h = self.sthread.switch(h)
+    except MemoryError:
+        return h       # oups!  do an early return in this case
     #
     space = self.space
-    w_result = space.call_function(w_callable, space.wrap(self))
-    #
-    start_state.w_value = w_result
-    return self.h
+    try:
+        w_result = space.call_function(w_callable, space.wrap(self))
+    except Exception, e:
+        start_state.propagate_exception = e
+        return self.h
+    else:
+        start_state.w_value = w_result
+        return self.h

pypy/module/_continuation/test/test_stacklet.py

         assert res == 42
         raises(error, c.__init__, empty_callback)
 
-    def test_bogus_return_value(self):
-        from _continuation import new
+    def test_propagate_exception(self):
+        from _continuation import continuation
         #
-        def empty_callback(h):
-            assert h.is_pending()
-            seen.append(h)
-            return 42
-        #
-        seen = []
-        raises(TypeError, new, empty_callback)
-        assert len(seen) == 1
-        assert not seen[0].is_pending()
-
-    def test_propagate_exception(self):
-        from _continuation import new
-        #
-        def empty_callback(h):
-            assert h.is_pending()
-            seen.append(h)
+        def empty_callback(c1):
+            assert c1 is c
+            seen.append(42)
             raise ValueError
         #
         seen = []
-        raises(ValueError, new, empty_callback)
-        assert len(seen) == 1
-        assert not seen[0].is_pending()
+        c = continuation(empty_callback)
+        raises(ValueError, c.switch)
+        assert seen == [42]
 
     def test_callback_with_arguments(self):
         from _continuation import new
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.