Skip to content

Commit

Permalink
Tests: USB: Fix Python 3 compatibility
Browse files Browse the repository at this point in the history
Use integer division explicitly in basic test.
Convert bytes to str for Python 3 in serial test.
  • Loading branch information
Filip Jagodzinski committed Sep 20, 2019
1 parent d0b5ba6 commit 96581ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion TESTS/host_tests/pyusb_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,7 @@ def ep_test_abort(dev, log, verbose=False):
payload_size = (NUM_PACKETS_UNTIL_ABORT + NUM_PACKETS_AFTER_ABORT) * ep_out.wMaxPacketSize
num_bytes_written = 0
while num_bytes_written < payload_size:
payload_out = array.array('B', (num_bytes_written/ep_out.wMaxPacketSize
payload_out = array.array('B', (num_bytes_written//ep_out.wMaxPacketSize
for _ in range(ep_out.wMaxPacketSize)))
try:
num_bytes_written += ep_out.write(payload_out)
Expand Down
14 changes: 12 additions & 2 deletions TESTS/host_tests/usb_device_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def usb_serial_name(serial_number):
return None


if sys.version_info < (3,):
def bytes2str(source):
"""Return a string decoded from source."""
return source
else:
def bytes2str(source):
"""Return a string decoded from source."""
return source.decode()


class RetryError(Exception):
"""Exception raised by retry_fun_call()."""

Expand Down Expand Up @@ -268,7 +278,7 @@ def change_line_coding(self):
mbed_serial.reset_output_buffer()
mbed_serial.dtr = True
try:
payload = mbed_serial.read(LINE_CODING_STRLEN)
payload = bytes2str(mbed_serial.read(LINE_CODING_STRLEN))
while len(payload) == LINE_CODING_STRLEN:
baud, bits, parity, stop = (int(i) for i in payload.split(','))
new_line_coding = {
Expand All @@ -277,7 +287,7 @@ def change_line_coding(self):
'parity': self._PARITIES[parity],
'stopbits': self._STOPBITS[stop]}
mbed_serial.apply_settings(new_line_coding)
payload = mbed_serial.read(LINE_CODING_STRLEN)
payload = bytes2str(mbed_serial.read(LINE_CODING_STRLEN))
except serial.SerialException as exc:
self.log('TEST ERROR: {}'.format(exc))
self.notify_complete(False)
Expand Down

0 comments on commit 96581ff

Please sign in to comment.