mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
solve static typing issues
This commit is contained in:
@ -240,28 +240,47 @@ class DependencyNode:
|
|||||||
size_of_field = ctypes.sizeof(processing_field.type)
|
size_of_field = ctypes.sizeof(processing_field.type)
|
||||||
return size_of_field
|
return size_of_field
|
||||||
elif processing_field.type.__module__ == "vmlinux":
|
elif processing_field.type.__module__ == "vmlinux":
|
||||||
size_of_field: int = 0
|
|
||||||
if processing_field.ctype_complex_type is not None:
|
if processing_field.ctype_complex_type is not None:
|
||||||
if issubclass(processing_field.ctype_complex_type, ctypes.Array):
|
if issubclass(processing_field.ctype_complex_type, ctypes.Array):
|
||||||
if processing_field.containing_type.__module__ == ctypes.__name__:
|
if processing_field.containing_type.__module__ == ctypes.__name__:
|
||||||
size_of_field = (
|
if (
|
||||||
ctypes.sizeof(processing_field.containing_type)
|
processing_field.containing_type is not None
|
||||||
* processing_field.type_size
|
and processing_field.type_size is not None
|
||||||
)
|
):
|
||||||
|
size_of_field = (
|
||||||
|
ctypes.sizeof(processing_field.containing_type)
|
||||||
|
* processing_field.type_size
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"{processing_field} has no containing_type or type_size"
|
||||||
|
)
|
||||||
return size_of_field
|
return size_of_field
|
||||||
elif processing_field.containing_type.__module__ == "vmlinux":
|
elif processing_field.containing_type.__module__ == "vmlinux":
|
||||||
size_of_field = (
|
if (
|
||||||
size_of_containing_type * processing_field.type_size
|
size_of_containing_type is not None
|
||||||
)
|
and processing_field.type_size is not None
|
||||||
|
):
|
||||||
|
size_of_field = (
|
||||||
|
size_of_containing_type * processing_field.type_size
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"{processing_field} has no containing_type or type_size"
|
||||||
|
)
|
||||||
return size_of_field
|
return size_of_field
|
||||||
elif issubclass(processing_field.ctype_complex_type, ctypes._Pointer):
|
elif issubclass(processing_field.ctype_complex_type, ctypes._Pointer):
|
||||||
return ctypes.sizeof(ctypes.pointer())
|
return ctypes.sizeof(ctypes.c_void_p)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"This subclass of ctype not supported yet"
|
"This subclass of ctype not supported yet"
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# search up pre-created stuff and get size
|
# search up pre-created stuff and get size
|
||||||
|
if size_of_containing_type is None:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Size of containing type {size_of_containing_type} is None"
|
||||||
|
)
|
||||||
return size_of_containing_type
|
return size_of_containing_type
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import ast
|
import ast
|
||||||
import logging
|
import logging
|
||||||
from typing import List, Tuple, Dict
|
from typing import List, Tuple, Any
|
||||||
import importlib
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ def vmlinux_proc(tree: ast.AST, module):
|
|||||||
# initialise dependency handler
|
# initialise dependency handler
|
||||||
handler = DependencyHandler()
|
handler = DependencyHandler()
|
||||||
# initialise assignment dictionary of name to type
|
# initialise assignment dictionary of name to type
|
||||||
assignments: Dict[str, type] = {}
|
assignments: dict[str, tuple[type, Any]] = {}
|
||||||
|
|
||||||
if not import_statements:
|
if not import_statements:
|
||||||
logger.info("No vmlinux imports found")
|
logger.info("No vmlinux imports found")
|
||||||
@ -132,10 +132,16 @@ def vmlinux_proc(tree: ast.AST, module):
|
|||||||
return assignments
|
return assignments
|
||||||
|
|
||||||
|
|
||||||
def process_vmlinux_assign(node, module, assignments: Dict[str, type]):
|
def process_vmlinux_assign(node, module, assignments: dict[str, tuple[type, Any]]):
|
||||||
# Check if this is a simple assignment with a constant value
|
# Check if this is a simple assignment with a constant value
|
||||||
if len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
|
if len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
|
||||||
target_name = node.targets[0].id
|
target_name = node.targets[0].id
|
||||||
if isinstance(node.value, ast.Constant):
|
if isinstance(node.value, ast.Constant):
|
||||||
assignments[target_name] = node.value.value
|
assignments[target_name] = (type(node.value.value), node.value.value)
|
||||||
logger.info(f"Added assignment: {target_name} = {node.value.value}")
|
logger.info(
|
||||||
|
f"Added assignment: {target_name} = {node.value.value!r} of type {type(node.value.value)}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unsupported assignment type for {target_name}")
|
||||||
|
else:
|
||||||
|
raise ValueError("Not a simple assignment")
|
||||||
|
|||||||
Reference in New Issue
Block a user