kplindegaard / smbus2

A drop-in replacement for smbus-cffi/smbus-python in pure Python
MIT License
243 stars 68 forks source link

Added implicit i2c_msg -> bytes convertion #29

Closed paulo-raca closed 5 years ago

paulo-raca commented 5 years ago

because bytes(msg) is easier and more efficient than bytes(list(msg))

And, while we are at it, add __str__/__repr__/__len__

paulo-raca commented 5 years ago

Thanks for the quick review :)

kplindegaard commented 5 years ago

I should have spotted the unfortunate Python 2.7 vs Python 3.X inconsistencies this one introduced before the merge.

msg = i2c_msg.write(80, "ABC")
b = bytes(msg)[0]  # In Python 2.7 bytes == str
isinstance(b, str) # True in Python 2.7
isinstance(b, int)  # True in Python 3.x

A more important issue in Python 2.7, which there wasn't a unit test for, was that whenever are str(msg) or any form of string generation of a msg took place, __str__ would end up calling itself recursively. There is a fix on master now, but i won't release a new version just yet in case something more consistent comes up. Any ideas, @paulo-raca ?

paulo-raca commented 5 years ago

Sorry, I completely ignored python 2.x 😅

Your fix seems good 👍