From 5cfdac9c7c65755d682a30f12619435f67310341 Mon Sep 17 00:00:00 2001 From: Yann Sionneau Date: Fri, 20 Feb 2015 17:15:55 +0100 Subject: [PATCH] Lda: replace assert with direct exception raising --- artiq/devices/lda/driver.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/artiq/devices/lda/driver.py b/artiq/devices/lda/driver.py index 70ae5dbeb..70c199c20 100644 --- a/artiq/devices/lda/driver.py +++ b/artiq/devices/lda/driver.py @@ -156,7 +156,8 @@ class Lda: buf = struct.pack("BBB6s", 0, command, length, data) res = self._check_error(self.hidapi.hid_write(self._dev, buf, len(buf))) - assert res == len(buf), res + if res != len(buf): + raise IOError def set(self, command, data): """Sends a SET command to the Lab Brick device. @@ -165,8 +166,10 @@ class Lda: :param data: payload of the command. """ - assert command & 0x80 - assert data + if not 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) def get(self, command, length, timeout=1000): @@ -179,14 +182,17 @@ class Lda: :rtype: bytes """ - assert not command & 0x80 + if command & 0x80: + raise ValueError("Get commands must not have most significant bit" + " set") status = None self.write(command, length) buf = ctypes.create_string_buffer(8) while status != command: res = self._check_error(self.hidapi.hid_read_timeout(self._dev, buf, len(buf), timeout)) - assert res == len(buf), res + if res != len(buf): + raise IOError status, length, data = struct.unpack("BB6s", buf.raw) data = data[:length] logger.info("%s %s %r", command, length, data)