##// END OF EJS Templates
Prototype async REPL using IPython, take III...
Prototype async REPL using IPython, take III This is a squash and a rebase of a large number of commits from Min and I. For simplicity of managing it, history has been reduced to a single commit, but more historical versions can be found, in particular in PR 11155, or commit aedb5d6d3a441dcdb7180ac9b5cc03f91329117b to be more exact.

File last commit:

r24463:6f33fcd4
r24463:6f33fcd4
Show More
async_helpers.py
88 lines | 2.2 KiB | text/x-python | PythonLexer
"""
Async helper function that are invalid syntax on Python 3.5 and below.
Known limitation and possible improvement.
Top level code that contain a return statement (instead of, or in addition to
await) will be detected as requiring being wrapped in async calls. This should
be prevented as early return will not work.
"""
import ast
import sys
import inspect
from textwrap import dedent, indent
from types import CodeType
def _asyncio_runner(coro):
"""
Handler for asyncio autoawait
"""
import asyncio
return asyncio.get_event_loop().run_until_complete(coro)
def _curio_runner(coroutine):
"""
handler for curio autoawait
"""
import curio
return curio.run(coroutine)
if sys.version_info > (3, 5):
# nose refuses to avoid this file and async def is invalidsyntax
s = dedent('''
def _trio_runner(function):
import trio
async def loc(coro):
"""
We need the dummy no-op async def to protect from
trio's internal. See https://github.com/python-trio/trio/issues/89
"""
return await coro
return trio.run(loc, function)
''')
exec(s, globals(), locals())
def _asyncify(code: str) -> str:
"""wrap code in async def definition.
And setup a bit of context to run it later.
"""
res = dedent("""
async def __wrapper__():
try:
{usercode}
finally:
locals()
""").format(usercode=indent(code, ' ' * 8)[8:])
return res
def _should_be_async(cell: str) -> bool:
"""Detect if a block of code need to be wrapped in an `async def`
Attempt to parse the block of code, it it compile we're fine.
Otherwise we wrap if and try to compile.
If it works, assume it should be async. Otherwise Return False.
Not handled yet: If the block of code has a return statement as the top
level, it will be seen as async. This is a know limitation.
"""
try:
ast.parse(cell)
return False
except SyntaxError:
try:
ast.parse(_asyncify(cell))
except SyntaxError:
return False
return True
return False