Show More
@@ -1,107 +1,107 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 |
raise RuntimeError( |
|
|
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 | 83 | def _should_be_async(cell: str) -> bool: |
|
84 | 84 | """Detect if a block of code need to be wrapped in an `async def` |
|
85 | 85 | |
|
86 | 86 | Attempt to parse the block of code, it it compile we're fine. |
|
87 | 87 | Otherwise we wrap if and try to compile. |
|
88 | 88 | |
|
89 | 89 | If it works, assume it should be async. Otherwise Return False. |
|
90 | 90 | |
|
91 | 91 | Not handled yet: If the block of code has a return statement as the top |
|
92 | 92 | level, it will be seen as async. This is a know limitation. |
|
93 | 93 | """ |
|
94 | 94 | |
|
95 | 95 | try: |
|
96 | 96 | # we can't limit ourself to ast.parse, as it __accepts__ to parse on |
|
97 | 97 | # 3.7+, but just does not _compile_ |
|
98 | 98 | compile(cell, "<>", "exec") |
|
99 | 99 | return False |
|
100 | 100 | except SyntaxError: |
|
101 | 101 | try: |
|
102 | 102 | ast.parse(_asyncify(cell)) |
|
103 | 103 | # TODO verify ast has not "top level" return or yield. |
|
104 | 104 | except SyntaxError: |
|
105 | 105 | return False |
|
106 | 106 | return True |
|
107 | 107 | return False |
General Comments 0
You need to be logged in to leave comments.
Login now