_endian.py (1361B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\ctypes\_endian.py 2 import sys 3 from ctypes import * 4 _array_type = type(c_int * 3) 5 6 def _other_endian(typ): 7 try: 8 return getattr(typ, _OTHER_ENDIAN) 9 except AttributeError: 10 if type(typ) == _array_type: 11 return _other_endian(typ._type_) * typ._length_ 12 raise TypeError('This type does not support other endian: %s' % typ) 13 14 15 class _swapped_meta(type(Structure)): 16 17 def __setattr__(self, attrname, value): 18 if attrname == '_fields_': 19 fields = [] 20 for desc in value: 21 name = desc[0] 22 typ = desc[1] 23 rest = desc[2:] 24 fields.append((name, _other_endian(typ)) + rest) 25 26 value = fields 27 super(_swapped_meta, self).__setattr__(attrname, value) 28 29 30 if sys.byteorder == 'little': 31 _OTHER_ENDIAN = '__ctype_be__' 32 LittleEndianStructure = Structure 33 34 class BigEndianStructure(Structure): 35 __metaclass__ = _swapped_meta 36 _swappedbytes_ = None 37 38 39 elif sys.byteorder == 'big': 40 _OTHER_ENDIAN = '__ctype_le__' 41 BigEndianStructure = Structure 42 43 class LittleEndianStructure(Structure): 44 __metaclass__ = _swapped_meta 45 _swappedbytes_ = None 46 47 48 else: 49 raise RuntimeError('Invalid byteorder')