A C enum declaration is a syntax wrapper around some integer type. See Is sizeof (enum) == sizeof (int), always? . How big an int will depend on the particular C compiler. I would probably start with 16 bits.
union reserves a block of memory the size of the largest of the contained data types. Again, the exact size will depend on the implementation of C, but I would expect 32 bits for a 32-bit architecture or 64-bit if it was compiled as native 64-bit code. Generally speaking, you can store the contents of a union in integer or long Python, regardless of whether the pointer or offset was stored in it.
A more interesting question is why the pointer is ever written to the disk file. You may find that the union field is only treated as a pointer when the TEEG struct is in memory, but when writing to disk, it is always an integer offset.
As for the notation: 4, as several people have noted, these are โbit fieldsโ, which means a sequence of bits, some of which can be packed into one space. If I remember correctly, the bit fields in C are packed into int s, so both of these 4-bit fields will be packed into one. They can be unpacked with the appropriate use of Python & (bitwise and) and "โ" (right shift). Again, exactly how the fields were packed into an integer and the size of the integer field itself will depend on the specific implementation of C.
Perhaps the following code snippet will help you:
SIZEOF_TEEG_TYPE = 2 # First guess for enum is two bytes FMT_TEEG_TYPE = "h" # Could be "b", "B", "h", "H", "l", "L", "q" or "Q" SIZEOF_LONG = 4 # Use 8 in 64-bit Unix architectures FMT_LONG = "l" # Use "q" in 64-bit Unix architectures # Life gets more interesting if you are reading 64-bit # using 32-bit Python SIZEOF_PTR_LONG_UNION = 4 # Use 8 in any 64-bit architecture FMT_PTR_LONG_UNION = "l" # Use "q" in any 64-bit architecture # Life gets more interesting if you are reading 64-bit # using 32-bit Python SIZEOF_TEEG_STRUCT = SIZEOF_TEEG_TYPE + SIZEOF_LONG + SIZEOF_PTR_LONG_UNION FMT_TEEG_STRUCT = FMT_TEEG_TYPE + FMT_LONG + FMT_PTR_LONG_UNION # Constants for TEEG_EVENTs TEEG_EVENT_TAB1 = 1 TEEG_EVENT_TAB2 = 2 . . . # Read a TEEG structure teeg_raw = file_handle.read( SIZEOF_TEEG_STRUCT ) teeg_type, teeg_size, teeg_offset = struct.unpack( FMT_TEEG_STRUCT, teeg_raw ) . . . # Use TEEG_TYPE information if teeg_type == TEEG_EVENT_TAB1: Do something useful elif teeg_type == TEEG_EVENT_TAB2: Do something else useful else: raise ValueError( "Encountered illegal TEEG_EVENT type %d" % teeg_type )
source share