Skip to content

Commit 75d3ad4

Browse files
format chore
1 parent abbf177 commit 75d3ad4

File tree

6 files changed

+104
-78
lines changed

6 files changed

+104
-78
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#
1313
# See https://github.com/pre-commit/pre-commit
1414

15-
exclude: 'vmlinux.*\.py$'
15+
exclude: 'vmlinux.py'
1616

1717
ci:
1818
autoupdate_commit_msg: "chore: update pre-commit hooks"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
from .import_detector import vmlinux_proc
2+
3+
__all__ = ["vmlinux_proc"]

pythonbpf/vmlinux_parser/vmlinux_class_handler.py renamed to pythonbpf/vmlinux_parser/class_handler.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ast
21
import logging
32
from functools import lru_cache
43
import importlib
@@ -20,17 +19,19 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler):
2019
symbols_in_module, imported_module = get_module_symbols("vmlinux")
2120

2221
# Handle both node objects and type objects
23-
if hasattr(node, 'name'):
22+
if hasattr(node, "name"):
2423
current_symbol_name = node.name
25-
elif hasattr(node, '__name__'):
24+
elif hasattr(node, "__name__"):
2625
current_symbol_name = node.__name__
2726
else:
2827
current_symbol_name = str(node)
2928

3029
if current_symbol_name not in symbols_in_module:
3130
raise ImportError(f"{current_symbol_name} not present in module vmlinux")
3231
logger.info(f"Resolving vmlinux class {current_symbol_name}")
33-
logger.debug(f"Current handler state: {handler.is_ready} readiness and {handler.get_all_nodes()} all nodes")
32+
logger.debug(
33+
f"Current handler state: {handler.is_ready} readiness and {handler.get_all_nodes()} all nodes"
34+
)
3435
field_table = {} # should contain the field and it's type.
3536

3637
# Get the class object from the module
@@ -42,12 +43,12 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler):
4243
# Inspect the class fields
4344
# Assuming class_obj has fields stored in some standard way
4445
# If it's a ctypes-like structure with _fields_
45-
if hasattr(class_obj, '_fields_'):
46+
if hasattr(class_obj, "_fields_"):
4647
for field_name, field_type in class_obj._fields_:
4748
field_table[field_name] = field_type
4849

4950
# If it's using __annotations__
50-
elif hasattr(class_obj, '__annotations__'):
51+
elif hasattr(class_obj, "__annotations__"):
5152
for field_name, field_type in class_obj.__annotations__.items():
5253
field_table[field_name] = field_type
5354

@@ -69,17 +70,24 @@ def process_vmlinux_class(node, llvm_module, handler: DependencyHandler):
6970
print("elem_name:", elem_name, "elem_type:", elem_type)
7071
# currently fails when a non-normal type appears which is basically everytime
7172
identify_ctypes_type(elem_type)
72-
symbol_name = elem_type.__name__ if hasattr(elem_type, '__name__') else str(elem_type)
73+
symbol_name = (
74+
elem_type.__name__
75+
if hasattr(elem_type, "__name__")
76+
else str(elem_type)
77+
)
7378
vmlinux_symbol = getattr(imported_module, symbol_name)
7479
if process_vmlinux_class(vmlinux_symbol, llvm_module, handler):
7580
new_dep_node.set_field_ready(elem_name, True)
7681
else:
77-
raise ValueError(f"{elem_name} with type {elem_type} not supported in recursive resolver")
82+
raise ValueError(
83+
f"{elem_name} with type {elem_type} not supported in recursive resolver"
84+
)
7885
handler.add_node(new_dep_node)
7986
logger.info(f"added node: {current_symbol_name}")
8087

8188
return True
8289

90+
8391
def identify_ctypes_type(t):
8492
if isinstance(t, type): # t is a type/class
8593
if issubclass(t, ctypes.Array):

pythonbpf/vmlinux_parser/dependency_node.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@dataclass
66
class Field:
77
"""Represents a field in a dependency node with its type and readiness state."""
8+
89
name: str
910
type: type
1011
value: Any = None
@@ -64,13 +65,22 @@ class DependencyNode:
6465
ready_fields = somestruct.get_ready_fields()
6566
print(f"Ready fields: {[field.name for field in ready_fields.values()]}") # ['field_1', 'field_2']
6667
"""
68+
6769
name: str
6870
fields: Dict[str, Field] = field(default_factory=dict)
6971
_ready_cache: Optional[bool] = field(default=None, repr=False)
7072

71-
def add_field(self, name: str, field_type: type, initial_value: Any = None, ready: bool = False) -> None:
73+
def add_field(
74+
self,
75+
name: str,
76+
field_type: type,
77+
initial_value: Any = None,
78+
ready: bool = False,
79+
) -> None:
7280
"""Add a field to the node with an optional initial value and readiness state."""
73-
self.fields[name] = Field(name=name, type=field_type, value=initial_value, ready=ready)
81+
self.fields[name] = Field(
82+
name=name, type=field_type, value=initial_value, ready=ready
83+
)
7484
# Invalidate readiness cache
7585
self._ready_cache = None
7686

pythonbpf/vmlinux_parser/import_detector.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from .dependency_handler import DependencyHandler
88
from .ir_generation import IRGenerator
9-
from .vmlinux_class_handler import process_vmlinux_class
9+
from .class_handler import process_vmlinux_class
1010

1111
logger = logging.getLogger(__name__)
1212

@@ -58,8 +58,8 @@ def detect_import_statement(tree: ast.AST) -> List[Tuple[str, ast.ImportFrom]]:
5858
# Valid single import
5959
for alias in node.names:
6060
import_name = alias.name
61-
# Use alias if provided, otherwise use the original name
62-
as_name = alias.asname if alias.asname else alias.name
61+
# Use alias if provided, otherwise use the original name (commented)
62+
# as_name = alias.asname if alias.asname else alias.name
6363
vmlinux_imports.append(("vmlinux", node))
6464
logger.info(f"Found vmlinux import: {import_name}")
6565

@@ -68,13 +68,14 @@ def detect_import_statement(tree: ast.AST) -> List[Tuple[str, ast.ImportFrom]]:
6868
for alias in node.names:
6969
if alias.name == "vmlinux" or alias.name.startswith("vmlinux."):
7070
raise SyntaxError(
71-
f"Direct import of vmlinux module is not supported. "
72-
f"Use 'from vmlinux import <type>' instead."
71+
"Direct import of vmlinux module is not supported. "
72+
"Use 'from vmlinux import <type>' instead."
7373
)
7474

7575
logger.info(f"Total vmlinux imports detected: {len(vmlinux_imports)}")
7676
return vmlinux_imports
7777

78+
7879
def vmlinux_proc(tree: ast.AST, module):
7980
import_statements = detect_import_statement(tree)
8081

@@ -107,7 +108,10 @@ def vmlinux_proc(tree: ast.AST, module):
107108
imported_name = alias.name
108109
found = False
109110
for mod_node in mod_ast.body:
110-
if isinstance(mod_node, ast.ClassDef) and mod_node.name == imported_name:
111+
if (
112+
isinstance(mod_node, ast.ClassDef)
113+
and mod_node.name == imported_name
114+
):
111115
process_vmlinux_class(mod_node, module, handler)
112116
found = True
113117
break
@@ -120,9 +124,12 @@ def vmlinux_proc(tree: ast.AST, module):
120124
if found:
121125
break
122126
if not found:
123-
logger.info(f"{imported_name} not found as ClassDef or Assign in vmlinux")
127+
logger.info(
128+
f"{imported_name} not found as ClassDef or Assign in vmlinux"
129+
)
124130

125131
IRGenerator(module, handler)
126132

133+
127134
def process_vmlinux_assign(node, module, assignments: Dict[str, type]):
128135
raise NotImplementedError("Assignment handling has not been implemented yet")

0 commit comments

Comments
 (0)