Skip to content

Commit bfcfb6c

Browse files
author
Filip Jagodzinski
committed
Tests: USB: Clean up test failure output
Catch all pyusb errors and report failures to the device. Printing full tracebacks was more confusing than useful.
1 parent 1319843 commit bfcfb6c

File tree

1 file changed

+87
-15
lines changed

1 file changed

+87
-15
lines changed

TESTS/host_tests/pyusb_basic.py

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
mbed SDK
3-
Copyright (c) 2018-2018 ARM Limited
3+
Copyright (c) 2018-2019 ARM Limited
44
SPDX-License-Identifier: Apache-2.0
55
66
Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,6 +26,7 @@
2626
import array
2727
import random
2828
import os
29+
import traceback
2930

3031
import usb.core
3132
from usb.util import build_request_type
@@ -122,6 +123,35 @@ def get_interface(dev, interface, alternate=0):
122123
usb.ENDPOINT_TYPE_INTERRUPT: 'INTERRUPT',
123124
usb.ENDPOINT_TYPE_ISOCHRONOUS: 'ISOCHRONOUS'}
124125

126+
def format_local_error_msg(fmt):
127+
"""Return an error message formatted with the last traceback entry from this file.
128+
129+
The message is formatted according to fmt with data from the last traceback
130+
enrty internal to this file. There are 4 arguments supplied to the format
131+
function: filename, line_number, exc_type and exc_value.
132+
133+
Returns None if formatting fails.
134+
"""
135+
try:
136+
exc_type, exc_value, exc_traceback = sys.exc_info()
137+
# A list of 4-tuples (filename, line_number, function_name, text).
138+
tb_entries = traceback.extract_tb(exc_traceback)
139+
# Reuse the filename from the first tuple instead of relying on __file__:
140+
# 1. No need for path handling.
141+
# 2. No need for file extension handling (i.e. .py vs .pyc).
142+
name_of_this_file = tb_entries[0][0]
143+
last_internal_tb_entry = [tb for tb in tb_entries if tb[0] == name_of_this_file][-1]
144+
msg = fmt.format(
145+
filename=last_internal_tb_entry[0],
146+
line_number=last_internal_tb_entry[1],
147+
exc_type=str(exc_type).strip(),
148+
exc_value=str(exc_value).strip(),
149+
)
150+
except (IndexError, KeyError):
151+
msg = None
152+
return msg
153+
154+
125155
class PyusbBasicTest(BaseHostTest):
126156

127157
def _callback_control_basic_test(self, key, value, timestamp):
@@ -137,8 +167,11 @@ def _callback_control_basic_test(self, key, value, timestamp):
137167
try:
138168
control_basic_test(dev, int(vendor_id), int(product_id), log=print)
139169
self.report_success()
140-
except (RuntimeError) as exc:
170+
except RuntimeError as exc:
141171
self.report_error(exc)
172+
except usb.core.USBError as exc:
173+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
174+
self.report_error(error_msg if error_msg is not None else exc)
142175

143176

144177
def _callback_control_stall_test(self, key, value, timestamp):
@@ -151,8 +184,11 @@ def _callback_control_stall_test(self, key, value, timestamp):
151184
try:
152185
control_stall_test(dev, log=print)
153186
self.report_success()
154-
except (RuntimeError) as exc:
187+
except RuntimeError as exc:
155188
self.report_error(exc)
189+
except usb.core.USBError as exc:
190+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
191+
self.report_error(error_msg if error_msg is not None else exc)
156192

157193

158194
def _callback_control_sizes_test(self, key, value, timestamp):
@@ -165,8 +201,11 @@ def _callback_control_sizes_test(self, key, value, timestamp):
165201
try:
166202
control_sizes_test(dev, log=print)
167203
self.report_success()
168-
except (RuntimeError) as exc:
204+
except RuntimeError as exc:
169205
self.report_error(exc)
206+
except usb.core.USBError as exc:
207+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
208+
self.report_error(error_msg if error_msg is not None else exc)
170209

171210

172211
def _callback_control_stress_test(self, key, value, timestamp):
@@ -179,8 +218,11 @@ def _callback_control_stress_test(self, key, value, timestamp):
179218
try:
180219
control_stress_test(dev, log=print)
181220
self.report_success()
182-
except (RuntimeError) as exc:
221+
except RuntimeError as exc:
183222
self.report_error(exc)
223+
except usb.core.USBError as exc:
224+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
225+
self.report_error(error_msg if error_msg is not None else exc)
184226

185227
def _callback_device_reset_test(self, key, value, timestamp):
186228
self.log("Received serial %s" % (value))
@@ -192,8 +234,11 @@ def _callback_device_reset_test(self, key, value, timestamp):
192234
try:
193235
self.device_reset_test.send(dev)
194236
self.report_success()
195-
except (RuntimeError) as exc:
237+
except RuntimeError as exc:
196238
self.report_error(exc)
239+
except usb.core.USBError as exc:
240+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
241+
self.report_error(error_msg if error_msg is not None else exc)
197242

198243
def _callback_device_soft_reconnection_test(self, key, value, timestamp):
199244
self.log("Received serial %s" % (value))
@@ -205,8 +250,11 @@ def _callback_device_soft_reconnection_test(self, key, value, timestamp):
205250
try:
206251
self.device_soft_reconnection_test.send(dev)
207252
self.report_success()
208-
except (RuntimeError) as exc:
253+
except RuntimeError as exc:
209254
self.report_error(exc)
255+
except usb.core.USBError as exc:
256+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
257+
self.report_error(error_msg if error_msg is not None else exc)
210258

211259

212260
def _callback_device_suspend_resume_test(self, key, value, timestamp):
@@ -219,8 +267,11 @@ def _callback_device_suspend_resume_test(self, key, value, timestamp):
219267
try:
220268
self.device_suspend_resume_test.send(dev)
221269
self.report_success()
222-
except (RuntimeError) as exc:
270+
except RuntimeError as exc:
223271
self.report_error(exc)
272+
except usb.core.USBError as exc:
273+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
274+
self.report_error(error_msg if error_msg is not None else exc)
224275

225276

226277
def _callback_repeated_construction_destruction_test(self, key, value, timestamp):
@@ -233,8 +284,11 @@ def _callback_repeated_construction_destruction_test(self, key, value, timestamp
233284
try:
234285
self.repeated_construction_destruction_test.send(dev)
235286
self.report_success()
236-
except (RuntimeError) as exc:
287+
except RuntimeError as exc:
237288
self.report_error(exc)
289+
except usb.core.USBError as exc:
290+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
291+
self.report_error(error_msg if error_msg is not None else exc)
238292

239293
def _callback_ep_test_data_correctness(self, key, value, timestamp):
240294
self.log("Received serial %s" % (value))
@@ -246,8 +300,11 @@ def _callback_ep_test_data_correctness(self, key, value, timestamp):
246300
try:
247301
ep_test_data_correctness(dev, log=print)
248302
self.report_success()
249-
except (RuntimeError) as exc:
303+
except RuntimeError as exc:
250304
self.report_error(exc)
305+
except usb.core.USBError as exc:
306+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
307+
self.report_error(error_msg if error_msg is not None else exc)
251308

252309
def _callback_ep_test_halt(self, key, value, timestamp):
253310
self.log("Received serial %s" % (value))
@@ -259,8 +316,11 @@ def _callback_ep_test_halt(self, key, value, timestamp):
259316
try:
260317
ep_test_halt(dev, log=print)
261318
self.report_success()
262-
except (RuntimeError) as exc:
319+
except RuntimeError as exc:
263320
self.report_error(exc)
321+
except usb.core.USBError as exc:
322+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
323+
self.report_error(error_msg if error_msg is not None else exc)
264324

265325
def _callback_ep_test_parallel_transfers(self, key, value, timestamp):
266326
self.log("Received serial %s" % (value))
@@ -272,8 +332,11 @@ def _callback_ep_test_parallel_transfers(self, key, value, timestamp):
272332
try:
273333
ep_test_parallel_transfers(dev, log=print)
274334
self.report_success()
275-
except (RuntimeError) as exc:
335+
except RuntimeError as exc:
276336
self.report_error(exc)
337+
except usb.core.USBError as exc:
338+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
339+
self.report_error(error_msg if error_msg is not None else exc)
277340

278341
def _callback_ep_test_parallel_transfers_ctrl(self, key, value, timestamp):
279342
self.log("Received serial %s" % (value))
@@ -285,8 +348,11 @@ def _callback_ep_test_parallel_transfers_ctrl(self, key, value, timestamp):
285348
try:
286349
ep_test_parallel_transfers_ctrl(dev, log=print)
287350
self.report_success()
288-
except (RuntimeError) as exc:
351+
except RuntimeError as exc:
289352
self.report_error(exc)
353+
except usb.core.USBError as exc:
354+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
355+
self.report_error(error_msg if error_msg is not None else exc)
290356

291357
def _callback_ep_test_abort(self, key, value, timestamp):
292358
self.log("Received serial %s" % (value))
@@ -298,8 +364,11 @@ def _callback_ep_test_abort(self, key, value, timestamp):
298364
try:
299365
ep_test_abort(dev, log=print)
300366
self.report_success()
301-
except (RuntimeError) as exc:
367+
except RuntimeError as exc:
302368
self.report_error(exc)
369+
except usb.core.USBError as exc:
370+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
371+
self.report_error(error_msg if error_msg is not None else exc)
303372

304373
def _callback_ep_test_data_toggle(self, key, value, timestamp):
305374
self.log("Received serial %s" % (value))
@@ -311,8 +380,11 @@ def _callback_ep_test_data_toggle(self, key, value, timestamp):
311380
try:
312381
ep_test_data_toggle(dev, log=print)
313382
self.report_success()
314-
except (RuntimeError) as exc:
383+
except RuntimeError as exc:
315384
self.report_error(exc)
385+
except usb.core.USBError as exc:
386+
error_msg = format_local_error_msg('[{filename}]:{line_number}, Dev-host transfer error ({exc_value}).')
387+
self.report_error(error_msg if error_msg is not None else exc)
316388

317389
def _callback_reset_support(self, key, value, timestamp):
318390
status = "false" if sys.platform == "darwin" else "true"

0 commit comments

Comments
 (0)