Skip to content

tools/pyboard.py: ProcessToSerial cross-platform fix #8435

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 3 commits 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
74 changes: 69 additions & 5 deletions tools/pyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,72 @@ def inWaiting(self):
else:
return n_waiting

# A thread that constantly reads from subp stdout and puts data into a queue (FIFO list)
# When tested, it reads double '\r' which messes up the further code, so if double '\r' detected, skip one
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you understand why it reads double \r?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to investigate the reason, but unfortunately I am still not able to understand why it reads double \r

def read_stream(stream, q):
data_prec = b""
while True:
data = stream.read(1)
if (data_prec == data) and (data_prec == b'\r'):
continue
else:
q.put(data)
data_prec = data

class ProcessToSerial:
"Execute a process and emulate serial connection using its stdin/stdout."
class ProcessToSerialThreading:
"Execute a process and emulate serial connection using its stdin/stdout. Communication with stdout is done through the read_stream thread"

def __init__(self, cmd):
def __init__(self, cmd):
import subprocess

# On Windows, preexec_fn parameter should be replaced by start_new_session
self.subp = subprocess.Popen(
cmd,
bufsize=0,
shell=True,
start_new_session=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)

# On Windows, select can not be used, a thread is used instead
# that constantly reads from subp.stdout and completes a queue
import threading
import queue
self.q = queue.Queue()
self.t = threading.Thread(target=read_stream, args=(self.subp.stdout, self.q))
self.t.daemon=True # Makes thread exit when main script exits
self.t.start()

def close(self):
import signal
os.kill(self.subp.pid, signal.SIGTERM)

def read(self, size=1):
data = b""
# On Windows, data will be read from the queue that is completed by the thread
i = 0
while i != size:
data += self.q.get()
i += 1
return data

def write(self, data):
self.subp.stdin.write(data)
return len(data)

def inWaiting(self):
if self.q.empty():
return 0
else:
return 1

class ProcessToSerialPosix:
"Execute a process and emulate serial connection using its stdin/stdout. Using select to check if any data available on stdout"

def __init__(self, cmd):
import subprocess

self.subp = subprocess.Popen(
cmd,
bufsize=0,
Expand All @@ -168,7 +227,7 @@ def __init__(self, cmd):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)

# Initially was implemented with selectors, but that adds Python3
# dependency. However, there can be race conditions communicating
# with a particular child process (like QEMU), and selectors may
Expand All @@ -185,7 +244,6 @@ def __init__(self, cmd):

def close(self):
import signal

os.killpg(os.getpgid(self.subp.pid), signal.SIGTERM)

def read(self, size=1):
Expand All @@ -205,6 +263,12 @@ def inWaiting(self):
return 1
return 0

# Class selector based on select module platform dependancy (available on Unix, but not on Windows)
import select
if hasattr(select, "poll"):
ProcessToSerial = ProcessToSerialPosix
else:
ProcessToSerial = ProcessToSerialThreading

class ProcessPtyToTerminal:
"""Execute a process which creates a PTY and prints slave PTY as
Expand Down