_endian.py 1.95 KB
Newer Older
1 2 3
import sys
from ctypes import *

4
_array_type = type(Array)
5 6 7 8 9

def _other_endian(typ):
    """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
10
    arrays and structures are supported.
11
    """
12 13
    # check _OTHER_ENDIAN attribute (present if typ is primitive type)
    if hasattr(typ, _OTHER_ENDIAN):
14
        return getattr(typ, _OTHER_ENDIAN)
15 16 17 18 19 20 21
    # if typ is array
    if isinstance(typ, _array_type):
        return _other_endian(typ._type_) * typ._length_
    # if typ is structure
    if issubclass(typ, Structure):
        return typ
    raise TypeError("This type does not support other endian: %s" % typ)
22 23 24 25 26 27 28 29 30 31 32

class _swapped_meta(type(Structure)):
    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
33
        super().__setattr__(attrname, value)
34 35 36 37 38 39 40 41 42 43 44 45

################################################################

# Note: The Structure metaclass checks for the *presence* (not the
# value!) of a _swapped_bytes_ attribute to determine the bit order in
# structures containing bit fields.

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

    LittleEndianStructure = Structure

46
    class BigEndianStructure(Structure, metaclass=_swapped_meta):
47
        """Structure with big endian byte order"""
48
        __slots__ = ()
49 50 51 52 53 54
        _swappedbytes_ = None

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

    BigEndianStructure = Structure
55
    class LittleEndianStructure(Structure, metaclass=_swapped_meta):
56
        """Structure with little endian byte order"""
57
        __slots__ = ()
58 59 60 61
        _swappedbytes_ = None

else:
    raise RuntimeError("Invalid byteorder")