-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncode.py
More file actions
310 lines (257 loc) · 11.1 KB
/
asyncode.py
File metadata and controls
310 lines (257 loc) · 11.1 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Emulating Python's interactive interpreter in asynchronous contexts.
"""
# Based on code standard module by Guido van Rossum and co-othors,
# and on asyncio.__main__ code by Yury Selivanov (differs from it in
# that these classes operate in already running asynchronous contexts)
import sys
import traceback
import code
import asyncio
import ast
import io
import types
__all__ = ["AsyncInteractiveInterpreter", "AsyncInteractiveConsole"]
class AsyncInteractiveInterpreter(code.InteractiveInterpreter):
"""Base class for AsyncInteractiveConsole.
This class inherits from :class:`code.InteractiveInterpreter`. It
- makes ``runsource``, ``runcode``, ``showsyntaxerror``,
``showtraceback`` and ``write`` methods asynchronous
(they return coroutines, that have to be awaited);
- tell the compiler to allow top-level ``await`` statements;
- awaits coroutines returned by such statements execution;
- catches :data:`sys.stdout` and :data:`sys.stderr` during code
execution, then send gathered content using :meth:`write`.
This class deals with parsing and interpreter state (the user's
namespace); it doesn't deal with input buffering or prompting or
input file naming (the filename is always passed in explicitly).
"""
def __init__(self, locals, *, reroute_stdout=True, reroute_stderr=True):
"""Constructor.
The optional ``locals`` argument specifies the dictionary in
which code will be executed; it defaults to a newly created
dictionary with key ``"__name__"`` set to ``"__console__"`` and
key ``"__doc__"`` set to ``None``.
The optional ``reroute_stdout`` and ``reroute_stderr`` keyword
arguments, if set to ``False``, disable catching
:data:`sys.stdout` and :data:`sys.stderr`, respectively.
"""
super().__init__(locals)
if "__builtins__" not in self.locals:
# Why should __builtins__ be specified here and not in
# code.InteractiveInterpreter? No idea, but necessary!
self.locals["__builtins__"] = __builtins__
self.compile.compiler.flags |= ast.PyCF_ALLOW_TOP_LEVEL_AWAIT
self.reroute_stdout = reroute_stdout
self.reroute_stderr = reroute_stderr
async def runsource(self, source, filename="<input>", symbol="single"):
"""Compile and run some source in the interpreter.
Arguments and behavior are as for :class:`code.InteractiveInterpreter`,
except that this is an asynchronous method.
Unless explicitly disabled, this method catches :data:`sys.stdout`
and :data:`sys.stderr` during code execution. If some data has
been written, it is send using :meth:`write` before returning.
"""
try:
code = self.compile(source, filename, symbol)
except (OverflowError, SyntaxError, ValueError):
# Case 1
await self.showsyntaxerror(filename)
return False
if code is None:
# Case 2
return True
# Case 3a
if self.reroute_stdout or self.reroute_stderr:
# Cache current stdout and stderr
_stdout = sys.stdout
_stderr = sys.stderr
# Create temporary IO buffer
buffer = io.StringIO()
try:
if self.reroute_stdout:
# Catch standard output
sys.stdout = buffer
if self.reroute_stderr:
# Catch error output
sys.stderr = buffer
await self.runcode(code)
return False
finally:
# Restore sys.stdout and sys.stderr
sys.stdout = _stdout
sys.stderr = _stderr
data = buffer.getvalue()
if data:
# Write gathered output (from print, repr...)
await self.write(data)
buffer.close()
# Case 3b
else:
await self.runcode(code)
return False
async def runcode(self, code):
"""Execute a code object.
Arguments and behavior are as for :class:`code.InteractiveInterpreter`,
except that this is an asynchronous method and that 'code' is
wrapped in a function object which is called instead of being
directly executed.
If the result is a coroutine, it is then awaited before this
method returns. Exceptions are processed in the same way as
during code execution.
"""
func = types.FunctionType(code, self.locals)
coro = None
try:
# Same as exec(code, self.locals) but return result
coro = func()
except SystemExit:
raise
except BaseException:
await self.showtraceback()
if asyncio.iscoroutine(coro):
# func() returned a coroutine
try:
# We await it
await coro
except SystemExit:
raise
except BaseException:
await self.showtraceback()
async def showsyntaxerror(self, filename=None):
"""Display the syntax error that just occurred.
Arguments and behavior are as for :class:`code.InteractiveInterpreter`,
except that this is an asynchronous method.
"""
type, value, tb = sys.exc_info()
sys.last_type = type
sys.last_value = value
sys.last_traceback = tb
if filename and type is SyntaxError:
# Work hard to stuff the correct filename in the exception
try:
msg, (dummy_filename, lineno, offset, line) = value.args
except ValueError:
# Not the format we expect; leave it alone
pass
else:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_value = value
if sys.excepthook is sys.__excepthook__:
lines = traceback.format_exception_only(type, value)
await self.write(''.join(lines))
else:
# If someone has set sys.excepthook, we let that take precedence
# over self.write
sys.excepthook(type, value, tb)
async def showtraceback(self):
"""Display the exception that just occurred.
Arguments and behavior are as for :class:`code.InteractiveInterpreter`,
except that this is an asynchronous method.
"""
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
sys.last_traceback = last_tb
try:
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
if sys.excepthook is sys.__excepthook__:
await self.write(''.join(lines))
else:
# If someone has set sys.excepthook, we let that take precedence
# over self.write
sys.excepthook(ei[0], ei[1], last_tb)
finally:
last_tb = ei = None
async def write(self, data):
"""Write a string.
Arguments are as for :class:`code.InteractiveInterpreter`.
While that this is an async method, the base implementation
still writes to :data:`sys.stderr`; a subclass should replace
this with a different (asynchronous) implementation.
"""
sys.stderr.write(data)
class AsyncInteractiveConsole(AsyncInteractiveInterpreter,
code.InteractiveConsole):
"""Emulate interactive Python interpreter in asynchronous contexts.
This class builds on :class:`AsyncInteractiveInterpreter` and adds
prompting using the familiar :data:`sys.ps1` and :data:`sys.ps2`,
and input buffering.
It does exactly the same job as :class:`code.InteractiveConsole`,
except that ``interact``, ``push`` and ``raw_input`` methods are
asynchronous (they return coroutines, that have to be awaited).
"""
def __init__(self, locals=None, filename="<console>", *,
reroute_stdout=True, reroute_stderr=True):
"""Constructor.
The optional ``locals``, ``reroute_stdout`` and
``reroute_stderr`` arguments will be passed to the
:class:`AsyncInteractiveInterpreter` base class.
The optional ``filename`` argument should specify the
(file)name of the input stream; it will show up in tracebacks.
"""
super().__init__(locals, reroute_stdout=reroute_stdout,
reroute_stderr=reroute_stderr)
self.filename = filename
self.resetbuffer()
async def interact(self, banner=None, exitmsg=None):
"""Closely emulate the interactive Python console.
Arguments and behavior are as for :class:`code.InteractiveConsole`,
except that this is an asynchronous method that awaits inputs.
"""
try:
sys.ps1
except AttributeError:
sys.ps1 = ">>> "
try:
sys.ps2
except AttributeError:
sys.ps2 = "... "
aw = 'async REPL: use "await" directly instead of "asyncio.run()".'
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
if banner is None:
await self.write("Python {} on {}\n{}\n{}\n({})\n".format(
sys.version, sys.platform, aw, cprt,
self.__class__.__name__))
elif banner:
await self.write("{}\n".format(banner))
more = 0
while 1:
try:
if more:
prompt = sys.ps2
else:
prompt = sys.ps1
try:
line = await self.raw_input(prompt)
except EOFError:
await self.write("\n")
break
else:
more = await self.push(line)
except KeyboardInterrupt:
await self.write("\nKeyboardInterrupt\n")
self.resetbuffer()
more = 0
if exitmsg is None:
await self.write('now exiting {}...\n'.format(
self.__class__.__name__))
elif exitmsg != '':
await self.write('{}\n'.format(exitmsg))
async def push(self, line):
"""Push a line to the interpreter.
Arguments and behavior are as for :class:`code.InteractiveConsole`,
except that this is an asynchronous method.
"""
self.buffer.append(line)
source = "\n".join(self.buffer)
more = await self.runsource(source, self.filename)
if not more:
self.resetbuffer()
return more
async def raw_input(self, prompt=""):
"""Write a prompt and read a line.
Arguments and behavior are as for :class:`code.InteractiveConsole`.
While that this is an async method, the base implementation
still uses the built-in function :func:`input`; a subclass should
replace this with a different (asynchronous) implementation.
"""
return input(prompt)