Skip to content

Commit fdbd232

Browse files
projectgusdpgeorge
authored andcommitted
tests/run-multitests.py: Escape encoding errors instead of crashing.
It's possible for a test to output non-ASCII characters (for example, due to a hard fault or serial noise or memory corruption). Rather than crashing the test runner, backslash escape those characters and treat them as program output. Refactors the string encoding step to a single helper to avoid copy-paste. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 4bdf2a2 commit fdbd232

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

tests/run-multitests.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def get_host_ip(_ip_cache=[]):
132132
return _ip_cache[0]
133133

134134

135+
def decode(output):
136+
# Convenience function to convert raw process or serial output to ASCII
137+
return str(output, "ascii", "backslashreplace")
138+
139+
135140
class PyInstance:
136141
def __init__(self):
137142
pass
@@ -190,7 +195,7 @@ def run_script(self, script):
190195
output = p.stdout
191196
except subprocess.CalledProcessError as er:
192197
err = er
193-
return str(output.strip(), "ascii"), err
198+
return decode(output.strip()), err
194199

195200
def start_script(self, script):
196201
self.popen = subprocess.Popen(
@@ -217,7 +222,7 @@ def readline(self):
217222
self.finished = self.popen.poll() is not None
218223
return None, None
219224
else:
220-
return str(out.rstrip(), "ascii"), None
225+
return decode(out.rstrip()), None
221226

222227
def write(self, data):
223228
self.popen.stdin.write(data)
@@ -229,7 +234,7 @@ def is_finished(self):
229234
def wait_finished(self):
230235
self.popen.wait()
231236
out = self.popen.stdout.read()
232-
return str(out, "ascii"), ""
237+
return decode(out), ""
233238

234239

235240
class PyInstancePyboard(PyInstance):
@@ -264,7 +269,7 @@ def run_script(self, script):
264269
output = self.pyb.exec_(script)
265270
except pyboard.PyboardError as er:
266271
err = er
267-
return str(output.strip(), "ascii"), err
272+
return decode(output.strip()), err
268273

269274
def start_script(self, script):
270275
self.pyb.enter_raw_repl()
@@ -283,13 +288,13 @@ def readline(self):
283288
if out.endswith(b"\x04"):
284289
self.finished = True
285290
out = out[:-1]
286-
err = str(self.pyb.read_until(1, b"\x04"), "ascii")
291+
err = decode(self.pyb.read_until(1, b"\x04"))
287292
err = err[:-1]
288293
if not out and not err:
289294
return None, None
290295
else:
291296
err = None
292-
return str(out.rstrip(), "ascii"), err
297+
return decode(out.rstrip()), err
293298

294299
def write(self, data):
295300
self.pyb.serial.write(data)
@@ -299,7 +304,7 @@ def is_finished(self):
299304

300305
def wait_finished(self):
301306
out, err = self.pyb.follow(10, None)
302-
return str(out, "ascii"), str(err, "ascii")
307+
return decode(out), decode(err)
303308

304309

305310
def prepare_test_file_list(test_files):

0 commit comments

Comments
 (0)