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.

Author higstar
Recipients higstar, theller
Date 2009-05-22.01:52:29
SpamBayes Score 0.00043392545
Marked as misclassified No
Message-id <1242957151.76.0.64046579619.issue6069@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2009-05-22 01:52:32higstarsetrecipients: + higstar, theller
2009-05-22 01:52:31higstarsetmessageid: <1242957151.76.0.64046579619.issue6069@psf.upfronthosting.co.za>
2009-05-22 01:52:30higstarlinkissue6069 messages
2009-05-22 01:52:29higstarcreate