Skip to content

Commit

Permalink
StructurePrintMixin: Print inherited fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mborgerson committed Jul 25, 2024
1 parent 64d5432 commit a168575
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions xbe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ class StructurePrintMixin:
A simple mixin to __repr__ ctypes structures
"""

_fields_: Sequence[Union[Tuple[str, Any], Tuple[str, Any, int]]]
_fields_: List[Union[Tuple[str, Any], Tuple[str, Any, int]]]

def __repr__(self) -> str:
return self.dumps()
Expand All @@ -461,8 +461,14 @@ def dumps(self, indent: int = 0) -> str:
Pretty-print all fields and values of the structure, return a string
"""
s = ""
max_name_len = max(len(item[0]) for item in self._fields_)
for item in self._fields_:

fields: List[Union[Tuple[str, Any], Tuple[str, Any, int]]] = []
for cls in self.__class__.mro():
if issubclass(cls, StructurePrintMixin) and hasattr(cls, "_fields_"):
fields.extend(cls._fields_)

max_name_len = max(len(item[0]) for item in fields)
for item in fields:
fname, ftype = item[0], item[1]
fval = getattr(self, fname)
s += " " * indent + ("%s: " % fname).ljust(max_name_len + 2)
Expand Down

0 comments on commit a168575

Please sign in to comment.