From 92e92f134abf194a28cf6654e7b9d2ca79bff522 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Mon, 20 Oct 2025 04:18:38 +0530 Subject: [PATCH] Add _make_repr to ir_to_ctypes --- pylibbpf/ir_to_ctypes.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pylibbpf/ir_to_ctypes.py b/pylibbpf/ir_to_ctypes.py index 1ad5a71..059e664 100644 --- a/pylibbpf/ir_to_ctypes.py +++ b/pylibbpf/ir_to_ctypes.py @@ -38,6 +38,19 @@ def ir_type_to_ctypes(ir_type): raise TypeError(f"Unsupported IR type: {ir_type}") +def _make_repr(struct_name: str, fields: list): + """Create a __repr__ function for a struct""" + + def __repr__(self): + field_strs = [] + for field_name, _ in fields: + value = getattr(self, field_name) + field_strs.append(f"{field_name}={value}") + return f"<{struct_name} {' '.join(field_strs)}>" + + return __repr__ + + def convert_structs_to_ctypes(structs_sym_tab) -> Dict[str, Type[ctypes.Structure]]: """Convert PythonBPF's structs_sym_tab to ctypes.Structure classes.""" if not structs_sym_tab: @@ -52,6 +65,8 @@ def convert_structs_to_ctypes(structs_sym_tab) -> Dict[str, Type[ctypes.Structur field_ctypes = ir_type_to_ctypes(field_ir_type) fields.append((field_name, field_ctypes)) + repr_func = _make_repr(struct_name, fields) + struct_class = type( struct_name, (ctypes.Structure,), @@ -59,13 +74,7 @@ def convert_structs_to_ctypes(structs_sym_tab) -> Dict[str, Type[ctypes.Structur "_fields_": fields, "__module__": "pylibbpf.ir_to_ctypes", "__doc__": f"Auto-generated ctypes structure for {struct_name}", - "__repr__": lambda self: ( - f"<{struct_name} " - + " ".join( - f"{name}={getattr(self, name)}" for name, _ in fields - ) - + ">" - ), + "__repr__": repr_func, }, )