Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions Lib/ctypes/_endian.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,22 @@ def _other_endian(typ):
attributes which contain the types, for more complicated types
arrays and structures are supported.
"""
# check _OTHER_ENDIAN attribute (present if typ is primitive type)
if hasattr(typ, _OTHER_ENDIAN):
return getattr(typ, _OTHER_ENDIAN)
global _OTHER_ENDIAN
# if typ is array
if isinstance(typ, _array_type):
return _other_endian(typ._type_) * typ._length_
# check _OTHER_ENDIAN attribute (present if typ is primitive type)
if hasattr(typ, _OTHER_ENDIAN):
return getattr(typ, _OTHER_ENDIAN)
# if typ is structure or union
if issubclass(typ, (Structure, Union)):
return typ
raise TypeError("This type does not support other endian: %s" % typ)

class _swapped_meta:
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
if attrname == "_fields_":
value = [(desc[0], _other_endian(desc[1])) + desc[2:] for desc in value]
super().__setattr__(attrname, value)
class _swapped_struct_meta(_swapped_meta, type(Structure)): pass
class _swapped_union_meta(_swapped_meta, type(Union)): pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improved Clarity and Correctness: The global variable _OTHER_ENDIAN is now explicitly defined for enhanced clarity and correctness in endian handling.
Optimized Code Structure: List comprehension has been implemented in the __setattr__ method to streamline the processing of value, reducing unnecessary complexity and improving readability.
Loading