diff options
author | Tor Norbye <tnorbye@google.com> | 2014-09-04 13:24:04 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2014-09-04 13:24:04 -0700 |
commit | c3d3a90f6b4ead083d63e28e6b9fcea93d675678 (patch) | |
tree | fc0dcd722b6d445468dbe7dad13b4c11781b1cbe /python/helpers/pydev/third_party/wrapped_for_pydev/ctypes/_endian.py | |
parent | 1aa2e09bdbd413eacb677e9fa4b50630530d0656 (diff) | |
download | idea-c3d3a90f6b4ead083d63e28e6b9fcea93d675678.tar.gz |
Snapshot idea/138.1980 from git://git.jetbrains.org/idea/community.git
Change-Id: Ib567c9c152d770212a7a3db20fbf591c210920bd
Diffstat (limited to 'python/helpers/pydev/third_party/wrapped_for_pydev/ctypes/_endian.py')
-rw-r--r-- | python/helpers/pydev/third_party/wrapped_for_pydev/ctypes/_endian.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/python/helpers/pydev/third_party/wrapped_for_pydev/ctypes/_endian.py b/python/helpers/pydev/third_party/wrapped_for_pydev/ctypes/_endian.py new file mode 100644 index 000000000000..7de037600717 --- /dev/null +++ b/python/helpers/pydev/third_party/wrapped_for_pydev/ctypes/_endian.py @@ -0,0 +1,58 @@ +#@PydevCodeAnalysisIgnore +import sys +from ctypes import * + +_array_type = type(c_int * 3) + +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 + only arrays are supported. + """ + try: + return getattr(typ, _OTHER_ENDIAN) + except AttributeError: + if type(typ) == _array_type: + return _other_endian(typ._type_) * typ._length_ + raise TypeError("This type does not support other endian: %s" % typ) + +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 + super(_swapped_meta, self).__setattr__(attrname, value) + +################################################################ + +# 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 + + class BigEndianStructure(Structure): + """Structure with big endian byte order""" + __metaclass__ = _swapped_meta + _swappedbytes_ = None + +elif sys.byteorder == "big": + _OTHER_ENDIAN = "__ctype_le__" + + BigEndianStructure = Structure + class LittleEndianStructure(Structure): + """Structure with little endian byte order""" + __metaclass__ = _swapped_meta + _swappedbytes_ = None + +else: + raise RuntimeError("Invalid byteorder") |