##// END OF EJS Templates
Improve async detection mechanism with blacklist...
Paul Ganssle -
Show More
@@ -1,107 +1,145 b''
1 1 """
2 2 Async helper function that are invalid syntax on Python 3.5 and below.
3 3
4 4 Known limitation and possible improvement.
5 5
6 6 Top level code that contain a return statement (instead of, or in addition to
7 7 await) will be detected as requiring being wrapped in async calls. This should
8 8 be prevented as early return will not work.
9 9 """
10 10
11 11
12 12 import ast
13 13 import sys
14 14 import inspect
15 15 from textwrap import dedent, indent
16 16 from types import CodeType
17 17
18 18
19 19 def _asyncio_runner(coro):
20 20 """
21 21 Handler for asyncio autoawait
22 22 """
23 23 import asyncio
24 24
25 25 return asyncio.get_event_loop().run_until_complete(coro)
26 26
27 27
28 28 def _curio_runner(coroutine):
29 29 """
30 30 handler for curio autoawait
31 31 """
32 32 import curio
33 33
34 34 return curio.run(coroutine)
35 35
36 36
37 37 def _trio_runner(async_fn):
38 38 import trio
39 39 async def loc(coro):
40 40 """
41 41 We need the dummy no-op async def to protect from
42 42 trio's internal. See https://github.com/python-trio/trio/issues/89
43 43 """
44 44 return await coro
45 45 return trio.run(loc, async_fn)
46 46
47 47
48 48 def _pseudo_sync_runner(coro):
49 49 """
50 50 A runner that does not really allow async execution, and just advance the coroutine.
51 51
52 52 See discussion in https://github.com/python-trio/trio/issues/608,
53 53
54 54 Credit to Nathaniel Smith
55 55
56 56 """
57 57 try:
58 58 coro.send(None)
59 59 except StopIteration as exc:
60 60 return exc.value
61 61 else:
62 62 # TODO: do not raise but return an execution result with the right info.
63 63 raise RuntimeError("{coro_name!r} needs a real async loop".format(coro_name=coro.__name__))
64 64
65 65
66 66 def _asyncify(code: str) -> str:
67 67 """wrap code in async def definition.
68 68
69 69 And setup a bit of context to run it later.
70 70 """
71 71 res = dedent(
72 72 """
73 73 async def __wrapper__():
74 74 try:
75 75 {usercode}
76 76 finally:
77 77 locals()
78 78 """
79 79 ).format(usercode=indent(code, " " * 8)[8:])
80 80 return res
81 81
82 82
83 class _AsyncSyntaxErrorVisitor(ast.NodeVisitor):
84 """
85 Find syntax errors that would be an error in an async repl, but because
86 the implementation involves wrapping the repl in an async function, it
87 is erroneously allowed (e.g. yield or return at the top level)
88 """
89 def generic_visit(self, node):
90 func_types = (ast.FunctionDef, ast.AsyncFunctionDef)
91 invalid_types = (ast.Return, ast.Yield, ast.YieldFrom)
92
93 if isinstance(node, func_types):
94 return # Don't recurse into functions
95 elif isinstance(node, invalid_types):
96 raise SyntaxError()
97 else:
98 super().generic_visit(node)
99
100
101 def _async_parse_cell(cell: str) -> ast.AST:
102 """
103 This is a compatibility shim for pre-3.7 when async outside of a function
104 is a syntax error at the parse stage.
105
106 It will return an abstract syntax tree parsed as if async and await outside
107 of a function were not a syntax error.
108 """
109 if sys.version_info < (3, 7):
110 # Prior to 3.7 you need to asyncify before parse
111 wrapped_parse_tree = ast.parse(_asyncify(cell))
112 return wrapped_parse_tree.body[0].body[0]
113 else:
114 return ast.parse(cell)
115
116
83 117 def _should_be_async(cell: str) -> bool:
84 118 """Detect if a block of code need to be wrapped in an `async def`
85 119
86 120 Attempt to parse the block of code, it it compile we're fine.
87 121 Otherwise we wrap if and try to compile.
88 122
89 123 If it works, assume it should be async. Otherwise Return False.
90 124
91 125 Not handled yet: If the block of code has a return statement as the top
92 126 level, it will be seen as async. This is a know limitation.
93 127 """
94 128
95 129 try:
96 130 # we can't limit ourself to ast.parse, as it __accepts__ to parse on
97 131 # 3.7+, but just does not _compile_
98 132 compile(cell, "<>", "exec")
99 133 return False
100 134 except SyntaxError:
101 135 try:
102 ast.parse(_asyncify(cell))
103 # TODO verify ast has not "top level" return or yield.
136 parse_tree = _async_parse_cell(cell)
137
138 # Raise a SyntaxError if there are top-level return or yields
139 v = _AsyncSyntaxErrorVisitor()
140 v.visit(parse_tree)
141
104 142 except SyntaxError:
105 143 return False
106 144 return True
107 145 return False
General Comments 0
You need to be logged in to leave comments. Login now