Lda: replace assert with direct exception raising

This commit is contained in:
Yann Sionneau 2015-02-20 17:15:55 +01:00 committed by Sebastien Bourdeauducq
parent 0dd5692c32
commit 5cfdac9c7c
1 changed files with 11 additions and 5 deletions

View File

@ -156,7 +156,8 @@ class Lda:
buf = struct.pack("BBB6s", 0, command, length, data) buf = struct.pack("BBB6s", 0, command, length, data)
res = self._check_error(self.hidapi.hid_write(self._dev, buf, res = self._check_error(self.hidapi.hid_write(self._dev, buf,
len(buf))) len(buf)))
assert res == len(buf), res if res != len(buf):
raise IOError
def set(self, command, data): def set(self, command, data):
"""Sends a SET command to the Lab Brick device. """Sends a SET command to the Lab Brick device.
@ -165,8 +166,10 @@ class Lda:
:param data: payload of the command. :param data: payload of the command.
""" """
assert command & 0x80 if not data:
assert data raise ValueError("Data is empty")
if not (command & 0x80):
raise ValueError("Set commands must have most significant bit set")
self.write(command, len(data), data) self.write(command, len(data), data)
def get(self, command, length, timeout=1000): def get(self, command, length, timeout=1000):
@ -179,14 +182,17 @@ class Lda:
:rtype: bytes :rtype: bytes
""" """
assert not command & 0x80 if command & 0x80:
raise ValueError("Get commands must not have most significant bit"
" set")
status = None status = None
self.write(command, length) self.write(command, length)
buf = ctypes.create_string_buffer(8) buf = ctypes.create_string_buffer(8)
while status != command: while status != command:
res = self._check_error(self.hidapi.hid_read_timeout(self._dev, res = self._check_error(self.hidapi.hid_read_timeout(self._dev,
buf, len(buf), timeout)) buf, len(buf), timeout))
assert res == len(buf), res if res != len(buf):
raise IOError
status, length, data = struct.unpack("BB6s", buf.raw) status, length, data = struct.unpack("BB6s", buf.raw)
data = data[:length] data = data[:length]
logger.info("%s %s %r", command, length, data) logger.info("%s %s %r", command, length, data)