return the type with the other byte order simple types like cint and so on already have ctypebe and ctypele attributes which contain the types for more complicated types arrays and structures are supported check otherendian attribute present if typ is primitive type if typ is array if typ is structure note the structure metaclass checks for the presence not the value of a swappedbytes attribute to determine the bit order in structures containing bit fields structure with big endian byte order slots swappedbytes none littleendianunion union class bigendianunionunion metaclassswappedunionmeta structure with little endian byte order slots swappedbytes none bigendianunion union class littleendianunionunion metaclassswappedunionmeta return the type with the other byte order simple types like c_int and so on already have __ctype_be__ and __ctype_le__ attributes which contain the types for more complicated types arrays and structures are supported check _other_endian attribute present if typ is primitive type if typ is array if typ is structure note the structure metaclass checks for the presence not the value of a _swappedbytes_ attribute to determine the bit order in structures containing bit fields structure with big endian byte order union with big endian byte order structure with little endian byte order union with little endian byte order
import sys
from ctypes import Array, Structure, Union

_array_type = type(Array)

def _other_endian(typ):
    
    
    if hasattr(typ, _OTHER_ENDIAN):
        return getattr(typ, _OTHER_ENDIAN)
    
    if isinstance(typ, _array_type):
        return _other_endian(typ._type_) * typ._length_
    
    if issubclass(typ, Structure):
        return typ
    raise TypeError("This type does not support other endian: %s" % typ)

class _swapped_meta:
    def __setattr__(self, attrname, value):
        if attrname == "_fields_":
            fields = []
            for desc in value:
                name = desc[0]
                typ = desc[1]
                rest = desc[2:]
                fields.append((name, _other_endian(typ)) + rest)
            value = fields
        super().__setattr__(attrname, value)
class _swapped_struct_meta(_swapped_meta, type(Structure)): pass
class _swapped_union_meta(_swapped_meta, type(Union)): pass







if sys.byteorder == "little":
    _OTHER_ENDIAN = "__ctype_be__"

    LittleEndianStructure = Structure

    class BigEndianStructure(Structure, metaclass=_swapped_struct_meta):
        
        __slots__ = ()
        _swappedbytes_ = None

    LittleEndianUnion = Union

    class BigEndianUnion(Union, metaclass=_swapped_union_meta):
        
        __slots__ = ()
        _swappedbytes_ = None

elif sys.byteorder == "big":
    _OTHER_ENDIAN = "__ctype_le__"

    BigEndianStructure = Structure

    class LittleEndianStructure(Structure, metaclass=_swapped_struct_meta):
        
        __slots__ = ()
        _swappedbytes_ = None

    BigEndianUnion = Union

    class LittleEndianUnion(Union, metaclass=_swapped_union_meta):
        
        __slots__ = ()
        _swappedbytes_ = None

else:
    raise RuntimeError("Invalid byteorder")