assert that data read/written on the receiver handle is of type bytes

This commit is contained in:
Daniel Pavel 2013-05-03 16:35:28 +02:00
parent 7d440c2430
commit a0a76f738b
2 changed files with 8 additions and 0 deletions

View File

@ -215,6 +215,8 @@ def write(device_handle, data):
the Control Endpoint (Endpoint 0).
"""
assert device_handle
assert data
assert isinstance(data, bytes), (repr(data), type(data))
bytes_written = _os.write(device_handle, data)
if bytes_written != len(data):
raise IOError(_errno.EIO, 'written %d bytes out of expected %d' % (bytes_written, len(data)))
@ -248,6 +250,7 @@ def read(device_handle, bytes_count, timeout_ms=-1):
assert rlist == [device_handle]
data = _os.read(device_handle, bytes_count)
assert data is not None
assert isinstance(data, bytes), (repr(data), type(data))
return data
else:
return b''

View File

@ -141,6 +141,9 @@ def write(handle, devnumber, data):
unloaded. The handle will be closed automatically.
"""
# the data is padded to either 5 or 18 bytes
assert data is not None
assert isinstance(data, bytes), (repr(data), type(data))
if len(data) > _SHORT_MESSAGE_SIZE - 2 or data[:1] == b'\x82':
wdata = _pack(b'!BB18s', 0x11, devnumber, data)
else:
@ -188,6 +191,7 @@ def _read(handle, timeout):
raise NoReceiver(reason=reason)
if data:
assert isinstance(data, bytes), (repr(data), type(data))
report_id = ord(data[:1])
assert (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE or
report_id == 0x11 and len(data) == _LONG_MESSAGE_SIZE or
@ -218,6 +222,7 @@ def _skip_incoming(handle, ihandle, notifications_hook):
raise NoReceiver(reason=reason)
if data:
assert isinstance(data, bytes), (repr(data), type(data))
if _log.isEnabledFor(_DEBUG):
report_id = ord(data[:1])
assert (report_id == 0x10 and len(data) == _SHORT_MESSAGE_SIZE or