1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
# mode: run
# tag: asyncio, pep492
"""
PYTHON setup.py build_ext -i
PYTHON test_from_import.py
PYTHON test_import.py
PYTHON test_async_def.py
PYTHON test_async_def_future.py
PYTHON test_all.py
"""
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from distutils.core import setup
setup(
ext_modules = cythonize("*.pyx"),
)
######## test_from_import.py ########
import from_asyncio_import
import asyncio
import sys
from contextlib import closing
new_event_loop = asyncio.new_event_loop if sys.version_info >= (3, 5) else asyncio.get_event_loop
def runloop(task):
with closing(new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
if sys.version_info < (3, 7):
runloop(from_asyncio_import.wait3)
######## test_import.py ########
import import_asyncio
import asyncio
import sys
from contextlib import closing
new_event_loop = asyncio.new_event_loop if sys.version_info >= (3, 5) else asyncio.get_event_loop
def runloop(task):
with closing(new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
if sys.version_info < (3, 7):
runloop(import_asyncio.wait3)
######## test_async_def.py ########
import sys
ASYNCIO_SUPPORTS_COROUTINE = sys.version_info[:2] >= (3, 5)
if ASYNCIO_SUPPORTS_COROUTINE:
import async_def
import asyncio
from contextlib import closing
def runloop(task):
with closing(asyncio.new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
runloop(async_def.wait3)
######## test_async_def_future.py ########
import sys
ASYNCIO_SUPPORTS_COROUTINE = sys.version_info[:2] >= (3, 5)
if ASYNCIO_SUPPORTS_COROUTINE:
from async_def_future import await_future
import asyncio
from contextlib import closing
def runloop():
with closing(asyncio.new_event_loop()) as loop:
task, events, expected = await_future(loop)
result = loop.run_until_complete(task())
assert events == expected, 'expected %s, got %s' % (expected, events)
runloop()
######## test_all.py ########
import sys
import asyncio
from contextlib import closing, contextmanager
new_event_loop = asyncio.new_event_loop if sys.version_info >= (3, 5) else asyncio.get_event_loop
if sys.version_info < (3, 5):
# don't close loop on Py 3.4
@contextmanager
def closing(o):
yield o
ASYNCIO_SUPPORTS_COROUTINE = sys.version_info[:2] >= (3, 5)
ASYNCIO_SUPPORTS_YIELD_FROM = sys.version_info[:2] < (3, 7)
def runloop(task):
with closing(new_event_loop()) as loop:
result = loop.run_until_complete(task())
assert 3 == result, result
import import_asyncio
if ASYNCIO_SUPPORTS_YIELD_FROM:
runloop(import_asyncio.wait3) # 1a)
import from_asyncio_import
if ASYNCIO_SUPPORTS_YIELD_FROM:
runloop(from_asyncio_import.wait3) # 1b)
import async_def
if ASYNCIO_SUPPORTS_COROUTINE:
runloop(async_def.wait3) # 1c)
if ASYNCIO_SUPPORTS_YIELD_FROM:
runloop(from_asyncio_import.wait3) # 2a)
runloop(import_asyncio.wait3) # 2b)
if ASYNCIO_SUPPORTS_COROUTINE:
runloop(async_def.wait3) # 2c)
import sys
if ASYNCIO_SUPPORTS_YIELD_FROM:
runloop(from_asyncio_import.wait3) # 3a)
runloop(import_asyncio.wait3) # 3b)
if ASYNCIO_SUPPORTS_COROUTINE:
runloop(async_def.wait3) # 3c)
try:
from collections.abc import Generator
except ImportError:
try:
from collections import Generator
except ImportError:
assert sys.version_info < (3, 5), "Python 3.5+ should have collections.abc.Generator"
Generator = object # easy win :)
assert isinstance(from_asyncio_import.wait3(), Generator)
assert isinstance(import_asyncio.wait3(), Generator)
assert isinstance((lambda:(yield))(), Generator)
try:
from collections.abc import Awaitable
except ImportError:
try:
from collections import Awaitable
except ImportError:
assert sys.version_info < (3, 5), "Python 3.5+ should have collections.abc.Awaitable"
Awaitable = object # easy win :)
assert isinstance(async_def.wait3(), Awaitable)
try:
from collections.abc import Coroutine
except ImportError:
try:
from collections import Coroutine
except ImportError:
assert sys.version_info < (3, 5), "Python 3.5+ should have collections.abc.Coroutine"
Coroutine = object # easy win :)
assert isinstance(async_def.wait3(), Coroutine)
######## import_asyncio.pyx ########
# cython: binding=True
try:
from types import coroutine as types_coroutine
except ImportError:
types_coroutine = lambda f:f
import asyncio
@types_coroutine
def wait3():
counter = 0
for i in range(3):
print(counter)
yield from asyncio.sleep(0.01)
counter += 1
return counter
######## from_asyncio_import.pyx ########
# cython: binding=True
try:
from types import coroutine as types_coroutine
except ImportError:
types_coroutine = lambda f:f
from asyncio import sleep
@types_coroutine
def wait3():
counter = 0
for i in range(3):
print(counter)
yield from sleep(0.01)
counter += 1
return counter
######## async_def.pyx ########
# cython: binding=True
import asyncio
async def wait3():
counter = 0
for i in range(3):
print(counter)
await asyncio.sleep(0.01)
counter += 1
return counter
######## async_def_future.pyx ########
# cython: binding=True
import asyncio
def await_future(loop):
events = []
async def worker():
fut = asyncio.Future()
def setval():
events.append('setval')
fut.set_result(123)
events.append('setup')
loop.call_later(0.2, setval)
events.append(await fut)
async def test():
await worker()
expected = ['setup', 'setval', 123]
return test, events, expected
|