This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Structure field size/ofs __str__ wrong with large size fields
Type: behavior Stage:
Components: ctypes Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Charles Machalow
Priority: normal Keywords:

Created on 2015-12-14 02:52 by Charles Machalow, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
ctypesBug.py Charles Machalow, 2015-12-14 03:01
Messages (2)
msg256359 - (view) Author: Charles Machalow (Charles Machalow) Date: 2015-12-14 02:52
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
msg256361 - (view) Author: Charles Machalow (Charles Machalow) Date: 2015-12-14 02:58
Adding file with code to reproduce.
History
Date User Action Args
2022-04-11 14:58:24adminsetgithub: 70045
2020-11-08 18:15:58iritkatrielsetversions: + Python 3.8, Python 3.9, Python 3.10, - Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6
2015-12-14 03:01:11Charles Machalowsetfiles: + ctypesBug.py
2015-12-14 03:01:03Charles Machalowsetfiles: - ctypesBug.py
2015-12-14 02:58:46Charles Machalowsetfiles: + ctypesBug.py

messages: + msg256361
2015-12-14 02:58:08Charles Machalowsetfiles: - ctypesBug.py
2015-12-14 02:52:10Charles Machalowcreate