Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 1 | import sys |
| 2 | from ctypes import * |
| 3 | |
Meador Inge | 8582bb1 | 2012-02-04 20:36:48 -0600 | [diff] [blame] | 4 | _array_type = type(Array) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 5 | |
| 6 | def _other_endian(typ): |
| 7 | """Return the type with the 'other' byte order. Simple types like |
| 8 | c_int and so on already have __ctype_be__ and __ctype_le__ |
| 9 | attributes which contain the types, for more complicated types |
Victor Stinner | 6636121 | 2011-07-13 21:43:18 +0200 | [diff] [blame] | 10 | arrays and structures are supported. |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 11 | """ |
Victor Stinner | 6636121 | 2011-07-13 21:43:18 +0200 | [diff] [blame] | 12 | # check _OTHER_ENDIAN attribute (present if typ is primitive type) |
| 13 | if hasattr(typ, _OTHER_ENDIAN): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 14 | return getattr(typ, _OTHER_ENDIAN) |
Victor Stinner | 6636121 | 2011-07-13 21:43:18 +0200 | [diff] [blame] | 15 | # if typ is array |
| 16 | if isinstance(typ, _array_type): |
| 17 | return _other_endian(typ._type_) * typ._length_ |
| 18 | # if typ is structure |
| 19 | if issubclass(typ, Structure): |
| 20 | return typ |
| 21 | raise TypeError("This type does not support other endian: %s" % typ) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 22 | |
| 23 | class _swapped_meta(type(Structure)): |
| 24 | def __setattr__(self, attrname, value): |
| 25 | if attrname == "_fields_": |
| 26 | fields = [] |
| 27 | for desc in value: |
| 28 | name = desc[0] |
| 29 | typ = desc[1] |
| 30 | rest = desc[2:] |
| 31 | fields.append((name, _other_endian(typ)) + rest) |
| 32 | value = fields |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 33 | super().__setattr__(attrname, value) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 34 | |
| 35 | ################################################################ |
| 36 | |
| 37 | # Note: The Structure metaclass checks for the *presence* (not the |
| 38 | # value!) of a _swapped_bytes_ attribute to determine the bit order in |
| 39 | # structures containing bit fields. |
| 40 | |
| 41 | if sys.byteorder == "little": |
| 42 | _OTHER_ENDIAN = "__ctype_be__" |
| 43 | |
| 44 | LittleEndianStructure = Structure |
| 45 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 46 | class BigEndianStructure(Structure, metaclass=_swapped_meta): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 47 | """Structure with big endian byte order""" |
Antoine Pitrou | 5ce8f35 | 2014-08-30 00:37:18 +0200 | [diff] [blame] | 48 | __slots__ = () |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 49 | _swappedbytes_ = None |
| 50 | |
| 51 | elif sys.byteorder == "big": |
| 52 | _OTHER_ENDIAN = "__ctype_le__" |
| 53 | |
| 54 | BigEndianStructure = Structure |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 55 | class LittleEndianStructure(Structure, metaclass=_swapped_meta): |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 56 | """Structure with little endian byte order""" |
Antoine Pitrou | 5ce8f35 | 2014-08-30 00:37:18 +0200 | [diff] [blame] | 57 | __slots__ = () |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 58 | _swappedbytes_ = None |
| 59 | |
| 60 | else: |
| 61 | raise RuntimeError("Invalid byteorder") |