Skip to content

Commit 5d0a888

Browse files
committed
Remove deadcode and seperate modules for pythonbpf.functions
1 parent 0042280 commit 5d0a888

File tree

3 files changed

+210
-209
lines changed

3 files changed

+210
-209
lines changed

pythonbpf/functions/func_registry_handlers.py

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import ast
2+
3+
4+
def get_probe_string(func_node):
5+
"""Extract the probe string from the decorator of the function node"""
6+
# TODO: right now we have the whole string in the section decorator
7+
# But later we can implement typed tuples for tracepoints and kprobes
8+
# For helper functions, we return "helper"
9+
10+
for decorator in func_node.decorator_list:
11+
if isinstance(decorator, ast.Name) and decorator.id == "bpfglobal":
12+
return None
13+
if isinstance(decorator, ast.Call) and isinstance(decorator.func, ast.Name):
14+
if decorator.func.id == "section" and len(decorator.args) == 1:
15+
arg = decorator.args[0]
16+
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
17+
return arg.value
18+
return "helper"
19+
20+
21+
def is_global_function(func_node):
22+
"""Check if the function is a global"""
23+
for decorator in func_node.decorator_list:
24+
if isinstance(decorator, ast.Name) and decorator.id in (
25+
"map",
26+
"bpfglobal",
27+
"struct",
28+
):
29+
return True
30+
return False
31+
32+
33+
def infer_return_type(func_node: ast.FunctionDef):
34+
if not isinstance(func_node, (ast.FunctionDef, ast.AsyncFunctionDef)):
35+
raise TypeError("Expected ast.FunctionDef")
36+
if func_node.returns is not None:
37+
try:
38+
return ast.unparse(func_node.returns)
39+
except Exception:
40+
node = func_node.returns
41+
if isinstance(node, ast.Name):
42+
return node.id
43+
if isinstance(node, ast.Attribute):
44+
return getattr(node, "attr", type(node).__name__)
45+
try:
46+
return str(node)
47+
except Exception:
48+
return type(node).__name__
49+
found_type = None
50+
51+
def _expr_type(e):
52+
if e is None:
53+
return "None"
54+
if isinstance(e, ast.Constant):
55+
return type(e.value).__name__
56+
if isinstance(e, ast.Name):
57+
return e.id
58+
if isinstance(e, ast.Call):
59+
f = e.func
60+
if isinstance(f, ast.Name):
61+
return f.id
62+
if isinstance(f, ast.Attribute):
63+
try:
64+
return ast.unparse(f)
65+
except Exception:
66+
return getattr(f, "attr", type(f).__name__)
67+
try:
68+
return ast.unparse(f)
69+
except Exception:
70+
return type(f).__name__
71+
if isinstance(e, ast.Attribute):
72+
try:
73+
return ast.unparse(e)
74+
except Exception:
75+
return getattr(e, "attr", type(e).__name__)
76+
try:
77+
return ast.unparse(e)
78+
except Exception:
79+
return type(e).__name__
80+
81+
for walked_node in ast.walk(func_node):
82+
if isinstance(walked_node, ast.Return):
83+
t = _expr_type(walked_node.value)
84+
if found_type is None:
85+
found_type = t
86+
elif found_type != t:
87+
raise ValueError(f"Conflicting return types: {found_type} vs {t}")
88+
return found_type or "None"

0 commit comments

Comments
 (0)