# HG changeset patch # User Pauli Virtanen # Date 1292888028 -3600 # Branch py3k # Node ID 552e05fc2efbfa0600f53a310bf0aec9aab30990 # Parent dd49a99921679e986b171747826ec5e962c0816e ctypes: Fix PEP 3118 type codes for c_long and c_bool diff -r dd49a9992167 -r 552e05fc2efb Lib/ctypes/test/test_pep3118.py --- a/Lib/ctypes/test/test_pep3118.py Mon Dec 20 23:03:47 2010 +0100 +++ b/Lib/ctypes/test/test_pep3118.py Tue Dec 21 00:33:48 2010 +0100 @@ -103,11 +103,31 @@ PComplete = POINTER(Complete) Complete._fields_ = [("a", c_long)] +def fmt_code(fmt): + """Adjust the PEP 3118 type code according to the platform""" + import ctypes + if ctypes.sizeof(ctypes.c_long) == 8: + fmt = fmt.replace('l', '>q') + fmt = fmt.replace('>L', '>Q') + if ctypes.sizeof(ctypes.c_bool) == 2: + fmt = fmt.replace('?', '>H') + if ctypes.sizeof(ctypes.c_bool) == 4: + fmt = fmt.replace('?', '>L') + if ctypes.sizeof(ctypes.c_bool) == 8: + fmt = fmt.replace('?', '>Q') + return fmt + ################################################################ # # This table contains format strings as they look on little endian # machines. The test replaces '<' with '>' on big endian machines. # + native_types = [ # type format shape calc itemsize @@ -123,8 +143,8 @@ #(c_int, "l:x:>l:y:}", None, BEPoint), - (LEPoint, "T{l:x:>l:y:}", None, POINTER(BEPoint)), - (POINTER(LEPoint), "&T{l:x:>l:y:}"), None, BEPoint), + (LEPoint, fmt_code("T{l:x:>l:y:}"), None, POINTER(BEPoint)), + (POINTER(LEPoint), fmt_code("&T{' : '<'; + result[1] = pep_code; + result[2] = '\0'; + return result; +} + /* Allocate a memory block for a pep3118 format string, copy prefix (if non-null) and suffix into it. Returns NULL on failure, with the error @@ -1910,9 +1959,9 @@ stgdict->setfunc = fmt->setfunc; stgdict->getfunc = fmt->getfunc; #ifdef WORDS_BIGENDIAN - stgdict->format = _ctypes_alloc_format_string(">", proto_str); + stgdict->format = _ctypes_alloc_format_string_for_type(proto_str[0], 1); #else - stgdict->format = _ctypes_alloc_format_string("<", proto_str); + stgdict->format = _ctypes_alloc_format_string_for_type(proto_str[0], 0); #endif if (stgdict->format == NULL) { Py_DECREF(result);