[New-bugs-announce] [issue25858] Structure field size/ofs __str__ wrong with large size fields

Charles Machalow report at bugs.python.org
Sun Dec 13 21:52:10 EST 2015


New submission from Charles Machalow:

Large sized fields in Structures lead to incorrect string representations because it is assuming that because the size is so large, it must be a bit field instead of a byte field.

class bugStruct(Structure):
    _pack_ = 1
    _fields_ = [
                ("correct", c_uint8 * 65535),
                ("wrongSizeAndOffset", c_uint8 * 999999),
               ]

print(str(bugStruct.correct))
<Field type=c_ubyte_Array_65535, ofs=0, size=65535> # Correct

print(str(bugStruct.wrongSizeAndOffset))
<Field type=c_ubyte_Array_999999, ofs=65535:16959, bits=15> # Incorrect
# Should be: <Field type=c_ubyte_Array_999999, ofs=65535, size=999999>

To get the math for this do the following on an incorrect size/offset:
  size = (bits << 16) + ofs.split(":")[1]
  ofs = ofs.split(":")[0]
    Though this isn't really safe because the field may actually be bit-sized

----------
components: ctypes
files: ctypesBug.py
messages: 256359
nosy: Charles Machalow
priority: normal
severity: normal
status: open
title: Structure field size/ofs __str__ wrong with large size fields
type: behavior
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6
Added file: http://bugs.python.org/file41297/ctypesBug.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25858>
_______________________________________


More information about the New-bugs-announce mailing list