New submission from James <j.schult...@gmail.com>:
Creating a Bitfield from a ctypes union and structure results in unexpected behaviour. It seems when you set the bit-width of a structure field to be greater than 8 bits it results in the subsequent bits being set to zero. class BitFieldStruct(ctypes.LittleEndianStructure): _fields_ = [ ("long_field", C_UINT32, 29), ("short_field_0", C_UINT8, 1), ("short_field_1", C_UINT8, 1), ("short_field_2", C_UINT8, 1), ] class BitField(ctypes.Union): _anonymous_ = ("fields",) _fields_ = [ ("fields", BitFieldStruct), ("as32bit", C_UINT32) ] def test_bit_field_union(): f = BitField() f.as32bit = int.from_bytes([255, 255, 255, 255], byteorder='little') assert f.long_field == int.from_bytes([255, 255, 255, 31], byteorder='little') assert f.short_field_0 == 1 assert f.short_field_1 == 1 assert f.short_field_2 == 1 test_bit_field_union() # this call will fail with an assertion error Equivalent C which does not fail https://rextester.com/FWV78514 I'm running on Ubuntu 16.04 with python3.6 but I have tested on 3.5, 3.7 and on repl.it with the same behaviour. It seems as though setting any of the struct fields to be greater than 8 bit width results in any of the following fields being set to zero. ---------- components: ctypes files: python_struct_union_bug.py messages: 360372 nosy: jschulte priority: normal severity: normal status: open title: Bitfield Union does not work for bit widths greater than 8 bits type: behavior versions: Python 3.5, Python 3.6, Python 3.7 Added file: https://bugs.python.org/file48856/python_struct_union_bug.py _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue39407> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com