Author: Armin Rigo <ar...@tunes.org>
Branch: py3.5-corowrapper
Changeset: r87185:53f068b021d8
Date: 2016-09-17 19:18 +0200
http://bitbucket.org/pypy/pypy/changeset/53f068b021d8/

Log:    CoroutineWrapper

diff --git a/pypy/interpreter/generator.py b/pypy/interpreter/generator.py
--- a/pypy/interpreter/generator.py
+++ b/pypy/interpreter/generator.py
@@ -234,6 +234,9 @@
     def descr_throw(self, w_type, w_val=None, w_tb=None):
         """throw(typ[,val[,tb]]) -> raise exception in generator/coroutine,
 return next yielded value or raise StopIteration."""
+        return self.throw(w_type, w_val, w_tb)
+
+    def throw(self, w_type, w_val, w_tb):
         from pypy.interpreter.pytraceback import check_traceback
 
         space = self.space
@@ -385,10 +388,7 @@
     KIND = "coroutine"
 
     def descr__await__(self, space):
-        # implement this function:
-        # https://github.com/python/cpython/blob/3.5/Objects/genobject.c#L786
-        # you need a new CoroutineWrapper object + CoroutineWrapperType
-        return self
+        return space.wrap(CoroutineWrapper(self))
 
     def _finalize_(self):
         # If coroutine was never awaited on issue a RuntimeWarning.
@@ -402,6 +402,31 @@
         GeneratorOrCoroutine._finalize_(self)
 
 
+class CoroutineWrapper(W_Root):
+    _immutable_ = True
+
+    def __init__(self, coroutine):
+        self.coroutine = coroutine
+
+    def descr__iter__(self, space):
+        return space.wrap(self)
+
+    def descr__next__(self, space):
+        return self.coroutine.send_ex(space.w_None)
+
+    def descr_send(self, space, w_arg):
+        return self.coroutine.send_ex(w_arg)
+    descr_send.__doc__ = Coroutine.descr_send.__doc__
+
+    def descr_throw(self, w_type, w_val=None, w_tb=None):
+        return self.coroutine.throw(w_type, w_val, w_tb)
+    descr_throw.__doc__ = Coroutine.descr_throw.__doc__
+
+    def descr_close(self):
+        return self.coroutine.descr_close()
+    descr_close.__doc__ = Coroutine.descr_close.__doc__
+
+
 @specialize.memo()
 def get_generator_exit(space):
     return OperationError(space.w_GeneratorExit,
diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -1478,6 +1478,7 @@
 
     def GET_AITER(self, oparg, next_instr):
         from pypy.interpreter.generator import AIterWrapper, get_awaitable_iter
+        xxxx
 
         space = self.space
         w_obj = self.popvalue()
@@ -1514,6 +1515,7 @@
 
     def GET_ANEXT(self, oparg, next_instr):
         from pypy.interpreter.generator import get_awaitable_iter
+        zzzz
 
         space = self.space
         w_aiter = self.peekvalue()
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -468,6 +468,7 @@
     ClassMethod, BuiltinFunction, descr_function_get)
 from pypy.interpreter.pytraceback import PyTraceback
 from pypy.interpreter.generator import GeneratorIterator, Coroutine
+from pypy.interpreter.generator import CoroutineWrapper
 from pypy.interpreter.nestedscope import Cell
 from pypy.interpreter.special import NotImplemented, Ellipsis
 
@@ -834,6 +835,15 @@
 )
 assert not Coroutine.typedef.acceptable_as_base_class  # no __new__
 
+CoroutineWrapper.typedef = TypeDef("coroutine_wrapper",
+    __iter__     = interp2app(CoroutineWrapper.descr__iter__),
+    __next__     = interp2app(CoroutineWrapper.descr__next__),
+    send         = interp2app(CoroutineWrapper.descr_send),
+    throw        = interp2app(CoroutineWrapper.descr_throw),
+    close        = interp2app(CoroutineWrapper.descr_close),
+)
+assert not CoroutineWrapper.typedef.acceptable_as_base_class  # no __new__
+
 Cell.typedef = TypeDef("cell",
     __total_ordering__ = 'auto',
     __lt__       = interp2app(Cell.descr__lt__),
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to