Author: Armin Rigo <[email protected]>
Branch: stacklet
Changeset: r46314:3dfb6772812d
Date: 2011-08-06 11:42 +0200
http://bitbucket.org/pypy/pypy/changeset/3dfb6772812d/
Log: Switch().
diff --git a/pypy/module/_stacklet/interp_stacklet.py
b/pypy/module/_stacklet/interp_stacklet.py
--- a/pypy/module/_stacklet/interp_stacklet.py
+++ b/pypy/module/_stacklet/interp_stacklet.py
@@ -28,7 +28,7 @@
self.thrd = lltype.nullptr(rstacklet.thread_handle.TO)
rstacklet.deletethread(thrd)
- def new_stacklet_object(self, space, h):
+ def new_stacklet_object(self, h):
if self.pending_exception is not None:
e = self.pending_exception
self.pending_exception = None
@@ -44,7 +44,7 @@
start_state.args = None
raise MemoryError
elif rstacklet.is_empty_handle(h):
- return space.w_None
+ return self.space.w_None
else:
return W_Stacklet(self, h)
@@ -69,12 +69,19 @@
self.sthread.w_error,
space.wrap("stacklet has already been resumed"))
+ def switch(self, space):
+ h = self.consume_handle()
+ sthread = self.sthread
+ h = rstacklet.switch(sthread.thrd, h)
+ return sthread.new_stacklet_object(h)
+
def is_pending(self, space):
return space.newbool(bool(self.h))
W_Stacklet.typedef = TypeDef(
'Stacklet',
__module__ = '_stacklet',
+ switch = interp2app(W_Stacklet.switch),
is_pending = interp2app(W_Stacklet.is_pending),
)
W_Stacklet.acceptable_as_base_class = False
@@ -128,4 +135,4 @@
start_state.args = __args__
h = rstacklet.new(sthread.thrd, new_stacklet_callback,
lltype.nullptr(rffi.VOIDP.TO))
- return sthread.new_stacklet_object(space, h)
+ return sthread.new_stacklet_object(h)
diff --git a/pypy/module/_stacklet/test/test_stacklet.py
b/pypy/module/_stacklet/test/test_stacklet.py
--- a/pypy/module/_stacklet/test/test_stacklet.py
+++ b/pypy/module/_stacklet/test/test_stacklet.py
@@ -73,3 +73,23 @@
h = newstacklet(empty_callback)
assert h is None
assert seen[0] is Stacklet
+
+ def test_switch(self):
+ from _stacklet import newstacklet
+ #
+ def switchbackonce_callback(h):
+ seen.append(1)
+ assert h.is_pending()
+ h2 = h.switch()
+ seen.append(3)
+ assert not h.is_pending()
+ assert h2.is_pending()
+ return h2
+ #
+ seen = []
+ h = newstacklet(switchbackonce_callback)
+ seen.append(2)
+ assert h.is_pending()
+ h2 = h.switch()
+ assert h2 is None
+ assert seen == [1, 2, 3]
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit