diff --git a/CMakeLists.txt b/CMakeLists.txt index 807ac3b..c8b690d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,6 @@ include_directories(${CMAKE_SOURCE_DIR}/src) add_subdirectory(pybind11) pybind11_add_module( pylibbpf - # Core src/core/bpf_program.h src/core/bpf_exception.h @@ -19,15 +18,12 @@ pybind11_add_module( src/core/bpf_program.cpp src/core/bpf_map.cpp src/core/bpf_object.cpp - # Maps src/maps/perf_event_array.h src/maps/perf_event_array.cpp - # Utils src/utils/struct_parser.h src/utils/struct_parser.cpp - # Bindings src/bindings/main.cpp) diff --git a/examples/execve.py b/examples/execve.py index c6a3f9f..d47f327 100644 --- a/examples/execve.py +++ b/examples/execve.py @@ -1,10 +1,11 @@ import time from ctypes import c_int32, c_int64, c_uint64, c_void_p -from pylibbpf import BpfMap from pythonbpf import BPF, bpf, bpfglobal, map, section from pythonbpf.maps import HashMap +from pylibbpf import BpfMap + @bpf @map diff --git a/pylibbpf/__init__.py b/pylibbpf/__init__.py index d679661..cac92df 100644 --- a/pylibbpf/__init__.py +++ b/pylibbpf/__init__.py @@ -1,14 +1,17 @@ import logging + +from .ir_to_ctypes import convert_structs_to_ctypes, is_pythonbpf_structs from .pylibbpf import ( - BpfObject as _BpfObject, # C++ object (internal) - BpfProgram, + BpfException, BpfMap, + BpfProgram, PerfEventArray, StructParser, - BpfException, +) +from .pylibbpf import ( + BpfObject as _BpfObject, # C++ object (internal) ) from .wrappers import BpfObjectWrapper -from .ir_to_ctypes import convert_structs_to_ctypes, is_pythonbpf_structs logger = logging.getLogger(__name__) diff --git a/pylibbpf/ir_to_ctypes.py b/pylibbpf/ir_to_ctypes.py index 455dda5..1ad5a71 100644 --- a/pylibbpf/ir_to_ctypes.py +++ b/pylibbpf/ir_to_ctypes.py @@ -1,7 +1,8 @@ import ctypes -from typing import Dict, Type -from llvmlite import ir import logging +from typing import Dict, Type + +from llvmlite import ir logger = logging.getLogger(__name__) @@ -54,12 +55,18 @@ def convert_structs_to_ctypes(structs_sym_tab) -> Dict[str, Type[ctypes.Structur struct_class = type( struct_name, (ctypes.Structure,), - {'_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) + - ">" - )} + { + "_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 + ) + + ">" + ), + }, ) ctypes_structs[struct_name] = struct_class @@ -80,9 +87,9 @@ def is_pythonbpf_structs(structs) -> bool: first_value = next(iter(structs.values())) return ( - hasattr(first_value, 'ir_type') and - hasattr(first_value, 'fields') and - hasattr(first_value, 'size') + hasattr(first_value, "ir_type") + and hasattr(first_value, "fields") + and hasattr(first_value, "size") ) diff --git a/pylibbpf/wrappers.py b/pylibbpf/wrappers.py index 4d231ae..aa7679c 100644 --- a/pylibbpf/wrappers.py +++ b/pylibbpf/wrappers.py @@ -13,7 +13,7 @@ class PerfEventArrayHelper: callback: Callable, struct_name: str = "", page_cnt: int = 8, - lost_callback: Optional[Callable] = None + lost_callback: Optional[Callable] = None, ): """Open perf buffer with auto-deserialization.""" from .pylibbpf import PerfEventArray @@ -24,14 +24,11 @@ class PerfEventArrayHelper: page_cnt, callback, struct_name, - lost_callback or (lambda cpu, cnt: None) + lost_callback or (lambda cpu, cnt: None), ) else: self._perf_buffer = PerfEventArray( - self._map, - page_cnt, - callback, - lost_callback or (lambda cpu, cnt: None) + self._map, page_cnt, callback, lost_callback or (lambda cpu, cnt: None) ) return self diff --git a/setup.py b/setup.py index ee8398e..41abc05 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ import subprocess import sys from pathlib import Path -from setuptools import Extension, setup, find_packages +from setuptools import Extension, find_packages, setup from setuptools.command.build_ext import build_ext # Convert distutils Windows platform specifiers to CMake -A arguments