Skip to content

micropython/aiorepl: Use blocking reads for raw_repl. #1005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions micropython/aiorepl/aiorepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async def task(g=None, prompt="--> "):
cmd = cmd[:-1]
sys.stdout.write("\x08 \x08")
elif c == CHAR_CTRL_A:
await raw_repl(s, g)
await raw_repl(g)
break
elif c == CHAR_CTRL_B:
continue
Expand Down Expand Up @@ -239,7 +239,7 @@ async def task(g=None, prompt="--> "):
micropython.kbd_intr(3)


async def raw_paste(s, g, window=512):
def raw_paste(window=512):
sys.stdout.write("R\x01") # supported
sys.stdout.write(bytearray([window & 0xFF, window >> 8, 0x01]).decode())
eof = False
Expand All @@ -248,7 +248,7 @@ async def raw_paste(s, g, window=512):
file = b""
while not eof:
for idx in range(window):
b = await s.read(1)
b = sys.stdin.read(1)
c = ord(b)
if c == CHAR_CTRL_C or c == CHAR_CTRL_D:
# end of file
Expand All @@ -267,7 +267,12 @@ async def raw_paste(s, g, window=512):
return file


async def raw_repl(s: asyncio.StreamReader, g: dict):
async def raw_repl(g: dict):
"""
This function is blocking to prevent other
async tasks from writing to the stdio stream and
breaking the raw repl session.
"""
heading = "raw REPL; CTRL-B to exit\n"
line = ""
sys.stdout.write(heading)
Expand All @@ -276,15 +281,15 @@ async def raw_repl(s: asyncio.StreamReader, g: dict):
line = ""
sys.stdout.write(">")
while True:
b = await s.read(1)
b = sys.stdin.read(1)
c = ord(b)
if c == CHAR_CTRL_A:
rline = line
line = ""

if len(rline) == 2 and ord(rline[0]) == CHAR_CTRL_E:
if rline[1] == "A":
line = await raw_paste(s, g)
line = raw_paste()
break
else:
# reset raw REPL
Expand Down
Loading