##// END OF EJS Templates
reformat with black
Matthias Bussonnier -
Show More
@@ -1,90 +1,95 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
13 12 import ast
14 13 import sys
15 14 import inspect
16 15 from textwrap import dedent, indent
17 16 from types import CodeType
18 17
19 18
20 19 def _asyncio_runner(coro):
21 20 """
22 21 Handler for asyncio autoawait
23 22 """
24 23 import asyncio
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 return curio.run(coroutine)
34 35
35 36
36 37 if sys.version_info > (3, 5):
37 38 # nose refuses to avoid this file and async def is invalidsyntax
38 s = dedent('''
39 s = dedent(
40 '''
39 41 def _trio_runner(function):
40 42 import trio
41 43 async def loc(coro):
42 44 """
43 45 We need the dummy no-op async def to protect from
44 46 trio's internal. See https://github.com/python-trio/trio/issues/89
45 47 """
46 48 return await coro
47 49 return trio.run(loc, function)
48 ''')
50 '''
51 )
49 52 exec(s, globals(), locals())
50 53
51 54
52 55 def _asyncify(code: str) -> str:
53 56 """wrap code in async def definition.
54 57
55 58 And setup a bit of context to run it later.
56 59 """
57 res = dedent("""
60 res = dedent(
61 """
58 62 async def __wrapper__():
59 63 try:
60 64 {usercode}
61 65 finally:
62 66 locals()
63 """).format(usercode=indent(code, ' ' * 8)[8:])
67 """
68 ).format(usercode=indent(code, " " * 8)[8:])
64 69 return res
65 70
66 71
67 72 def _should_be_async(cell: str) -> bool:
68 73 """Detect if a block of code need to be wrapped in an `async def`
69 74
70 75 Attempt to parse the block of code, it it compile we're fine.
71 76 Otherwise we wrap if and try to compile.
72 77
73 78 If it works, assume it should be async. Otherwise Return False.
74 79
75 80 Not handled yet: If the block of code has a return statement as the top
76 81 level, it will be seen as async. This is a know limitation.
77 82 """
78 83
79 84 try:
80 85 # we can't limit ourself to ast.parse, as it __accepts__ to parse on
81 86 # 3.7+, but just does not _compile_
82 compile(cell, '<>', 'exec')
87 compile(cell, "<>", "exec")
83 88 return False
84 89 except SyntaxError:
85 90 try:
86 91 ast.parse(_asyncify(cell))
87 92 except SyntaxError:
88 93 return False
89 94 return True
90 95 return False
General Comments 0
You need to be logged in to leave comments. Login now