ยปCore Development>Code coverage>Lib/ctypes/_endian.py

Python code coverage for Lib/ctypes/_endian.py

#countcontent
1n/aimport sys
2n/afrom ctypes import *
3n/a
4n/a_array_type = type(Array)
5n/a
6n/adef _other_endian(typ):
7n/a """Return the type with the 'other' byte order. Simple types like
8n/a c_int and so on already have __ctype_be__ and __ctype_le__
9n/a attributes which contain the types, for more complicated types
10n/a arrays and structures are supported.
11n/a """
12n/a # check _OTHER_ENDIAN attribute (present if typ is primitive type)
13n/a if hasattr(typ, _OTHER_ENDIAN):
14n/a return getattr(typ, _OTHER_ENDIAN)
15n/a # if typ is array
16n/a if isinstance(typ, _array_type):
17n/a return _other_endian(typ._type_) * typ._length_
18n/a # if typ is structure
19n/a if issubclass(typ, Structure):
20n/a return typ
21n/a raise TypeError("This type does not support other endian: %s" % typ)
22n/a
23n/aclass _swapped_meta(type(Structure)):
24n/a def __setattr__(self, attrname, value):
25n/a if attrname == "_fields_":
26n/a fields = []
27n/a for desc in value:
28n/a name = desc[0]
29n/a typ = desc[1]
30n/a rest = desc[2:]
31n/a fields.append((name, _other_endian(typ)) + rest)
32n/a value = fields
33n/a super().__setattr__(attrname, value)
34n/a
35n/a################################################################
36n/a
37n/a# Note: The Structure metaclass checks for the *presence* (not the
38n/a# value!) of a _swapped_bytes_ attribute to determine the bit order in
39n/a# structures containing bit fields.
40n/a
41n/aif sys.byteorder == "little":
42n/a _OTHER_ENDIAN = "__ctype_be__"
43n/a
44n/a LittleEndianStructure = Structure
45n/a
46n/a class BigEndianStructure(Structure, metaclass=_swapped_meta):
47n/a """Structure with big endian byte order"""
48n/a __slots__ = ()
49n/a _swappedbytes_ = None
50n/a
51n/aelif sys.byteorder == "big":
52n/a _OTHER_ENDIAN = "__ctype_le__"
53n/a
54n/a BigEndianStructure = Structure
55n/a class LittleEndianStructure(Structure, metaclass=_swapped_meta):
56n/a """Structure with little endian byte order"""
57n/a __slots__ = ()
58n/a _swappedbytes_ = None
59n/a
60n/aelse:
61n/a raise RuntimeError("Invalid byteorder")