vpelletier / python-libusb1

Python ctype-based wrapper around libusb1
GNU Lesser General Public License v2.1
168 stars 65 forks source link

Document accepted types for `data` argument to *write methods #11

Closed wesQ3 closed 8 years ago

wesQ3 commented 8 years ago

The Pydoc for USBDeviceHandle.interruptWrite (and the other handle write methods) lists the data parameter but makes no mention that integers are not allowed (intentionally or not). Each write method calls create_binary_buffer with their write data. If an int is passed in, create_binary_buffer returns an empty buffer of length data.

bytesWrote = handle.interruptWrite(ENDPOINT, 0xf) # writes 15 bytes of \0
bytesWrote = handle.interruptWrite(ENDPOINT, '\x0f') # writes 0xf, 1 byte

Is this a bug? Or expected behavior? Either way I find surprising.

I can submit a pull request with doc changes if you like.

vpelletier commented 8 years ago

I do not remember if it was a concious decision to still allow this (as opposed to just propagating ctypes.create_string_buffer's behaviour).

It is more a bug than an undocumented feature, in the sense that it seems unlikely that one really wants to send X null bytes. So I guess raising TypeError would more likely help detecting bugs than get in the way, and would prefer this to a documentation change.

vpelletier commented 8 years ago

Fixed by #12 merge, thanks for the patch.

vpelletier commented 8 years ago

While documenting the change, I found a simpler way (marginally faster too) to implement this change. I also realised that proposed patch missed "long" type, which is also fixed by this new way.

wesQ3 commented 8 years ago

Ah, nice catch.