Closed gotzl closed 5 years ago
Indeed, with C the number of transferred bytes would be available to caller, while here it is kept local and lost on exception.
I am tempted to special-case the timeout exception to hold this value, which would mean python-libusb1 caller code would become something like:
try:
transferred = handle.bulkRead(...)
except usb1.USBErrorTimeout, exc:
transferred = exc.transferred
# timeout-specific handling, if any
Which means the exception has to be specially handled in _bulkTransfer
, just a bit differently.
How does this look to you ?
This looks very good. Having both the exception and the partial data is preferable. Thanks!
I just pushed an implementation to master, would you mind giving it a try ?
Also, I botched my example, as the exception on Read
variants needs to hold the receive buffer (truncated to the number of received bytes, as would have been the returned value), the new received
property. It is the Write
variants which only get a transferred
property.
Your implementation works perfectly for me (although I've only tested bulkRead). Thanks for the fast response!
Thanks for testing. This change is now released as part of version 1.7 .
When performing bulk reads from the device by using e.g. USBDeviceHandle.bulkRead(0x86, 0x1000, timeout=1000) and the timeout is hit, a 'libusb1.USBError' is raised and the partially transferred data is lost. I would've expected that bulkRead (or _bulkTransfer which actually calls the underlying libusb1) ignores the timeout error and yields the partial data.
My (hacky) workaround looks like this: