Commits

Ben Bass committed dd6a17c

more robust error handling

Comments (0)

Files changed (1)

pylibftdi/driver.py

             raise FtdiError(msg)
 
         if self.interface_select is not None:
-            self.fdll.ftdi_set_interface(byref(self.ctx),
+            res = self.fdll.ftdi_set_interface(byref(self.ctx),
                 self.interface_select)
+            if res != 0:
+                msg = "%s (%d)" % (self.get_error_string(), res)
+                del self.ctx
+                raise FtdiError(msg)
 
         # Try to open the device.  If this fails, reset things to how
         # they were, but we can't use self.close as that assumes things
         """
         buf = create_string_buffer(length)
         rlen = self.fdll.ftdi_read_data(byref(self.ctx), byref(buf), length)
-        if rlen == -1:
+        if rlen < 0:
             raise FtdiError(self.get_error_string())
         byte_data = buf.raw[:rlen]
 
         buf = create_string_buffer(byte_data)
         written = self.fdll.ftdi_write_data(byref(self.ctx),
                                             byref(buf), len(byte_data))
+        if written < 0:
+            raise FtdiError(self.get_error_string())
         return written
 
     def write(self, data):
                 start = written
                 length = min(remaining, self.chunk_size)
                 result = self._write(byte_data[start: start + length])
-                if result == -1:
-                    written == result
-                    break
-                elif result == 0:
+                if result == 0:
                     # don't continue to try writing forever if nothing
                     # is actually being written
                     break
                     remaining -= result
         else:
             written = self._write(byte_data)
-        if written == -1:
-            raise FtdiError(self.get_error_string())
         return written
 
     def flush(self, flush_what=FLUSH_BOTH):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.