Skip to content

Commit aad2e48

Browse files
pi-anldpgeorge
authored andcommitted
aiorepl: Use blocking reads for raw REPL and raw paste.
Raw REPL mode is generally used as a command channel where all stdio traffic is related directly to the raw commands and responses sent. For this to work in aiorepl we need to ensure background tasks don't sent/ receive anything on stdio else the command channel will be corrupted. The simplest way to achieve this is to make the raw commands blocking and atomic rather than asyncio, assuming the user wont leave the device in raw mode for too long at any one time. Signed-off-by: Andrew Leech <[email protected]>
1 parent 96bd01e commit aad2e48

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

micropython/aiorepl/aiorepl.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async def task(g=None, prompt="--> "):
161161
cmd = cmd[:-1]
162162
sys.stdout.write("\x08 \x08")
163163
elif c == CHAR_CTRL_A:
164-
await raw_repl(s, g)
164+
raw_repl(sys.stdin, g)
165165
break
166166
elif c == CHAR_CTRL_B:
167167
continue
@@ -239,7 +239,7 @@ async def task(g=None, prompt="--> "):
239239
micropython.kbd_intr(3)
240240

241241

242-
async def raw_paste(s, g, window=512):
242+
def raw_paste(s, window=512):
243243
sys.stdout.write("R\x01") # supported
244244
sys.stdout.write(bytearray([window & 0xFF, window >> 8, 0x01]).decode())
245245
eof = False
@@ -248,7 +248,7 @@ async def raw_paste(s, g, window=512):
248248
file = b""
249249
while not eof:
250250
for idx in range(window):
251-
b = await s.read(1)
251+
b = s.read(1)
252252
c = ord(b)
253253
if c == CHAR_CTRL_C or c == CHAR_CTRL_D:
254254
# end of file
@@ -267,7 +267,12 @@ async def raw_paste(s, g, window=512):
267267
return file
268268

269269

270-
async def raw_repl(s: asyncio.StreamReader, g: dict):
270+
def raw_repl(s, g: dict):
271+
"""
272+
This function is blocking to prevent other
273+
async tasks from writing to the stdio stream and
274+
breaking the raw repl session.
275+
"""
271276
heading = "raw REPL; CTRL-B to exit\n"
272277
line = ""
273278
sys.stdout.write(heading)
@@ -276,15 +281,15 @@ async def raw_repl(s: asyncio.StreamReader, g: dict):
276281
line = ""
277282
sys.stdout.write(">")
278283
while True:
279-
b = await s.read(1)
284+
b = s.read(1)
280285
c = ord(b)
281286
if c == CHAR_CTRL_A:
282287
rline = line
283288
line = ""
284289

285290
if len(rline) == 2 and ord(rline[0]) == CHAR_CTRL_E:
286291
if rline[1] == "A":
287-
line = await raw_paste(s, g)
292+
line = raw_paste(s)
288293
break
289294
else:
290295
# reset raw REPL

micropython/aiorepl/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
metadata(
2-
version="0.2.0",
2+
version="0.2.1",
33
description="Provides an asynchronous REPL that can run concurrently with an asyncio, also allowing await expressions.",
44
)
55

0 commit comments

Comments
 (0)