[pypy-commit] pypy py3.5-async: Add lookup for __anext__ in next() and __anext__ to typedef of coroutines, add better (offline) async_for test, remove old async_for test and change it back to test only the gil error
raffael_t
pypy.commits at gmail.com
Wed Aug 17 11:26:34 EDT 2016
Author: Raffael Tfirst <raffael.tfirst at gmail.com>
Branch: py3.5-async
Changeset: r86250:d819f16ccf3f
Date: 2016-08-17 17:25 +0200
http://bitbucket.org/pypy/pypy/changeset/d819f16ccf3f/
Log: Add lookup for __anext__ in next() and __anext__ to typedef of
coroutines, add better (offline) async_for test, remove old
async_for test and change it back to test only the gil error
diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -801,6 +801,8 @@
__repr__ = interp2app(Coroutine.descr__repr__),
__reduce__ = interp2app(Coroutine.descr__reduce__),
__setstate__ = interp2app(Coroutine.descr__setstate__),
+ __next__ = interp2app(Coroutine.descr_next,
+ descrmismatch='__anext__'),
send = interp2app(Coroutine.descr_send,
descrmismatch='send'),
throw = interp2app(Coroutine.descr_throw,
diff --git a/pypy/module/_asyncio/test/test_asyncio.py b/pypy/module/_asyncio/test/test_asyncio.py
--- a/pypy/module/_asyncio/test/test_asyncio.py
+++ b/pypy/module/_asyncio/test/test_asyncio.py
@@ -15,9 +15,6 @@
async def f():
reader, writer = await asyncio.open_connection('example.com', 80)
- writer.write(b'a')
- async for line in reader:
- print('>>>', line)
writer.close()
loop = asyncio.get_event_loop()
@@ -25,6 +22,39 @@
print("done with async loop")
"""
+ def test_async_for(self):
+ # temporary test from
+ # http://blog.idego.pl/2015/12/05/back-to-the-future-with-async-and-await-in-python-3-5/
+ """
+import asyncio
+import logging
+import sys
+logging.basicConfig(level=logging.INFO, stream=sys.stdout, format="%(asctime)s: %(message)s")
+
+class AsyncIter:
+ def __init__(self):
+ self._data = list(range(10))
+ self._index = 0
+
+ async def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ while self._index < 10:
+ await asyncio.sleep(1)
+ self._index += 1
+ return self._data[self._index-1]
+ raise StopAsyncIteration
+
+async def do_loop():
+ async for x in AsyncIter():
+ logging.info(x)
+
+loop = asyncio.get_event_loop()
+futures = [asyncio.ensure_future(do_loop()), asyncio.ensure_future(do_loop())]
+loop.run_until_complete(asyncio.wait(futures))
+ """
+
def test_asynchronous_context_managers(self):
# it is important that "releasing lock A" happens before "holding lock B"
# or the other way around, but it is not allowed that both coroutines
diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -298,7 +298,10 @@
return w_iter
def next(space, w_obj):
- w_descr = space.lookup(w_obj, '__next__')
+ if space.type(w_obj).name == 'coroutine':
+ w_descr = space.lookup(w_obj, '__anext__')
+ else:
+ w_descr = space.lookup(w_obj, '__next__')
if w_descr is None:
raise oefmt(space.w_TypeError,
"'%T' object is not an iterator", w_obj)
More information about the pypy-commit
mailing list