New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UnicodeDecodeError #154
Comments
The lsusb -v output is:
Some more testing shows the error happens when I call: It now reports: Very odd indeed! |
Hmm - that is interesting (although I might be clutching at straws), by changing the encoding in util.py to read: I can see that at the end of the iProduct identifier, there is a single byte - hex 31 My guess is that it is a mistake on the part of the manufacturer as the actual model is BTP-R580(U)II Perhaps util.py needs to be able to pad out the string? |
After some testing, it is confirmed - this can be handled easily in util.py by changing the final lines from
To read:
Is it worth implementing this in the main code? |
I am not a unicode expert, but if I had to guess between a mistake on the python implementers and printer manufacturer, I would guess the manufacturer made a mistake. I don't feel comfortable implementing hacks to overcome a bug in a single case, as this may affect valid unicode strings I am not aware of. |
I have been speaking with the printer manufacturer about this and it will be interesting to see why the string has this extra character. I cannot see how padding out the string can do any harm, as any odd length series of bytes will always break the decode('utf-16-le') call and force an error in any event (because every character is represented by 2 bytes in utf-16-le, or 4 bytes in utf-16 - so surely better returning some sort of valid string rather than throwing an error I notice that when the buffer is fetched, there is a check to see if it is an even number of bytes for the whole buffer and an error is thrown if not. Possibly there should be a flag to allow programmers to decide on how to deal with these rare cases of odd length buffers / values... |
I see a odd sized two byte string as a device bug, and I prefer the bug be fixed in the right place, if possible. But if you want to submit a pull request for a new function that handle theses cases, I think it is ok. |
It's definitively a bug on the device side, but we could take the same approach as |
I have connected a USB receipt printer to a BananaPi and then use Python to read the descriptors for the printer.
The code contains the following lines:
Whilst my code works fine with a HP printer, with the USB receipt printer I get the error:
File "/g_printer_define.py", line 90, in
'IPNPstring="'. + IPNPstring + '"'
File "/usr/local/lib/python2.7/dist-packages/usb/util.py", line 330, in get_string
return buf[2:buf[0]].tostring().decode('utf-16-le')
File "/usr/lib/python2.7/encodings/utf_16_le.py", line 16, in decode
return codecs.utf+16+le+decode(input, errors, True)
UnicodeDecodeError: 'utf16' codec can't decode byte 0x31 in position 26: truncated data
I have asked the printer manufacturer for the details which their device passes for those strings, but should have thought that pyusb should be able to handle any characters permitted by the USB protocol?
The text was updated successfully, but these errors were encountered: