[issue6069] casting error from ctypes array to structure

higstar report at bugs.python.org
Fri May 22 03:52:31 CEST 2009


higstar <adrian at higstar.com> added the comment:

Another example of this:
---
import ctypes

correct_data_dict = {
                     'Data0' : 0x55555555,
                     'Data1' : 0x02,
                     'Data2' : 0x0AAA,
                     }

class closest_fit(ctypes.BigEndianStructure):
    _pack_      = 1    # aligned to 8 bits, not ctypes default of 32
    _fields_    = [
                   ("Data0",   ctypes.c_uint32, 32),
                   ("Data1",   ctypes.c_uint8, 3),
                   ("Data2",   ctypes.c_uint16, 12),
                  ]


class closest_fit_min_16(ctypes.BigEndianStructure):
    _pack_      = 1    # aligned to 8 bits, not ctypes default of 32
    _fields_    = [
                   ("Data0",   ctypes.c_uint32, 32),
                   ("Data1",   ctypes.c_uint16, 3),
                   ("Data2",   ctypes.c_uint16, 12),
                  ]

class closest_fit_min_32(ctypes.BigEndianStructure):
    _pack_      = 1    # aligned to 8 bits, not ctypes default of 32
    _fields_    = [
                   ("Data0",   ctypes.c_uint32, 32),
                   ("Data1",   ctypes.c_uint32, 3),
                   ("Data2",   ctypes.c_uint32, 12),
                  ]

class uint32(ctypes.BigEndianStructure):
    _pack_      = 1    # aligned to 8 bits, not ctypes default of 32
    _fields_    = [
                   ("Data0",   ctypes.c_uint32, 32),
                   ("Data1",   ctypes.c_uint32, 3),
                   ("Data2",   ctypes.c_uint32, 12),
                  ]

class uint64(ctypes.BigEndianStructure):
    _pack_      = 1    # aligned to 8 bits, not ctypes default of 32
    _fields_    = [
                   ("Data0",   ctypes.c_uint64, 32),
                   ("Data1",   ctypes.c_uint64, 3),
                   ("Data2",   ctypes.c_uint64, 12),
                  ]

size_of_structures_in_bytes = 6

def castbytes(type):
    buffer = (ctypes.c_byte * size_of_structures_in_bytes)()
    for index in range(size_of_structures_in_bytes):
        buffer[index] = 0x55
    return ctypes.cast(ctypes.pointer(buffer),
ctypes.POINTER(type)).contents

def print_members(test):
    print("Data0 is 0x%X, Data1 is 0x%X, Data2 is 0x%X for
%s"%(test.Data0, test.Data1, test.Data2, test.__class__.__name__))



test_classes = [closest_fit, uint32, closest_fit_min_16,
closest_fit_min_32, uint64]

Test_Failed = False
tests = [castbytes(type) for type in test_classes]
for test in tests:
#    print_members(test)
    for data in correct_data_dict:
        if not test.__getattribute__(data) == correct_data_dict[data]:
            Test_Failed = True
            print("%s failed for %s, value was 0x%X but should have been
0x%X"%(data, test.__class__.__name__, test.__getattribute__(data),
correct_data_dict[data]))

if not Test_Failed:
    print("Passed")
---
>c:\python25\python.exe IssueNEW.py
Data2 failed for closest_fit, value was 0x550 but should have been 0xAAA

>c:\python26\python.exe IssueNEW.py
Data2 failed for closest_fit, value was 0x550 but should have been 0xAAA

>c:\python30\python.exe IssueNEW.py
Data2 failed for closest_fit, value was 0x550 but should have been 0xAAA

----------

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


More information about the Python-bugs-list mailing list