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 2 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
75 changes: 69 additions & 6 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 Down Expand Up @@ -258,7 +316,12 @@ def __init__(
self.in_raw_repl = False
self.use_raw_paste = True
if device.startswith("exec:"):
self.serial = ProcessToSerial(device[len("exec:") :])
# Class selector based on select module platform dependancy (available on Unix, but not on Windows)
import select
if hasattr(select, "poll"):
self.serial = ProcessToSerialPosix(device[len("exec:") :])
else:
self.serial = ProcessToSerialThreading(device[len("exec:") :])
Copy link
Member

Choose a reason for hiding this comment

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

The ProcessToSerial class is really part of the public API of this file, so I suggest to put this code in the global scope, so that ProcessToSerial is defined in the global scope. Then the code here doesn't need to change at all.

Copy link
Author

Choose a reason for hiding this comment

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

Excuse me, I don't have much experience with Python. If I keep the ProcessToSerial class, how and where I could declare ProcessToSerialPosix and ProcessToSerialThreading ? I appreciate very much your help and indications.
Thank you in advance.
Best regards,
Victor

Copy link
Member

Choose a reason for hiding this comment

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

class ProcessToSerialPosix:
    ...

class ProcessToSerialThreading:
    ...

if hasattr(select, "poll"):
    ProcessToSerial = ProcessToSerialPosix
else:
    ProcessToSerial = ProcessToSerialThreading

Copy link
Author

Choose a reason for hiding this comment

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

Thank you very much for your time and help ! I didn't know you can do that in Python. I made a commit with these changes.

elif device.startswith("execpty:"):
self.serial = ProcessPtyToTerminal(device[len("qemupty:") :])
elif device and device[0].isdigit() and device[-1].isdigit() and device.count(".") == 3:
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy