You cannot make struct
do the masking for you; you'll need to manually provide integers that fit. If masking numbers with 0xff
works for your application, then that is what you'll have to do.
Python will not guess for you here, Python integers are unbounded and providing integers outside of the range of the struct
slots is not a job left to guessing. After all, it could be an application error if values outside the range are produced. And if values outside the range should be made to fit, it is up to you to decide how to do so; masking is one way, limiting the values to the boundaries (0 or 255) is another.
To quote two applicable lines from the Zen of Python:
Explicit is better than implicit.
[...]
In the face of ambiguity, refuse the temptation to guess.