From abbf17748d827a46796699331877435c452ee135 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Sat, 11 Oct 2025 21:33:39 +0530 Subject: [PATCH] format chore --- .pre-commit-config.yaml | 2 +- examples/clone-matplotlib.ipynb | 3 ++- examples/kprobes.py | 2 ++ examples/struct_and_perf.py | 2 +- examples/xdp_pass.py | 1 + pythonbpf/vmlinux_parser/vmlinux_class_handler.py | 4 +--- tests/failing_tests/globals.py | 12 ++++++++++-- tests/failing_tests/named_arg.py | 1 + tests/failing_tests/undeclared_values.py | 2 ++ tests/failing_tests/xdp_pass.py | 4 ++++ tests/passing_tests/return/typecast_var.py | 4 ++-- 11 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cfd7a8d..a8f1c37 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -41,7 +41,7 @@ repos: - id: ruff args: ["--fix", "--show-fixes"] - id: ruff-format - exclude: ^(docs)|^(tests)|^(examples) +# exclude: ^(docs)|^(tests)|^(examples) # Checking static types - repo: https://github.com/pre-commit/mirrors-mypy diff --git a/examples/clone-matplotlib.ipynb b/examples/clone-matplotlib.ipynb index 2bf7a33..44115e8 100644 --- a/examples/clone-matplotlib.ipynb +++ b/examples/clone-matplotlib.ipynb @@ -308,6 +308,7 @@ "def hist() -> HashMap:\n", " return HashMap(key=c_int32, value=c_uint64, max_entries=4096)\n", "\n", + "\n", "@bpf\n", "@section(\"tracepoint/syscalls/sys_enter_clone\")\n", "def hello(ctx: c_void_p) -> c_int64:\n", @@ -329,6 +330,7 @@ "def LICENSE() -> str:\n", " return \"GPL\"\n", "\n", + "\n", "b = BPF()" ] }, @@ -357,7 +359,6 @@ } ], "source": [ - "\n", "b.load_and_attach()\n", "hist = BpfMap(b, hist)\n", "print(\"Recording\")\n", diff --git a/examples/kprobes.py b/examples/kprobes.py index bb0a678..5977d36 100644 --- a/examples/kprobes.py +++ b/examples/kprobes.py @@ -8,12 +8,14 @@ def hello_world(ctx: c_void_p) -> c_int64: print("Hello, World!") return c_int64(0) + @bpf @section("kprobe/do_unlinkat") def hello_world2(ctx: c_void_p) -> c_int64: print("Hello, World!") return c_int64(0) + @bpf @bpfglobal def LICENSE() -> str: diff --git a/examples/struct_and_perf.py b/examples/struct_and_perf.py index 9c8f652..9a0dbf0 100644 --- a/examples/struct_and_perf.py +++ b/examples/struct_and_perf.py @@ -27,7 +27,7 @@ def hello(ctx: c_void_p) -> c_int32: dataobj.pid = pid() dataobj.ts = ktime() # dataobj.comm = strobj - print(f"clone called at {dataobj.ts} by pid" f"{dataobj.pid}, comm {strobj}") + print(f"clone called at {dataobj.ts} by pid{dataobj.pid}, comm {strobj}") events.output(dataobj) return c_int32(0) diff --git a/examples/xdp_pass.py b/examples/xdp_pass.py index a120a6f..1f0bc76 100644 --- a/examples/xdp_pass.py +++ b/examples/xdp_pass.py @@ -40,5 +40,6 @@ def hello_world(ctx: c_void_p) -> c_int64: def LICENSE() -> str: return "GPL" + compile_to_ir("xdp_pass.py", "xdp_pass.ll") compile() diff --git a/pythonbpf/vmlinux_parser/vmlinux_class_handler.py b/pythonbpf/vmlinux_parser/vmlinux_class_handler.py index dddc458..f2d2104 100644 --- a/pythonbpf/vmlinux_parser/vmlinux_class_handler.py +++ b/pythonbpf/vmlinux_parser/vmlinux_class_handler.py @@ -92,7 +92,5 @@ def identify_ctypes_type(t): elif issubclass(t, ctypes._SimpleCData): print("Scalar type") print("Base type:", t) - else: - print("Other ctypes type") else: - print("Instance, not type") + raise TypeError("Instance sent instead of Class") diff --git a/tests/failing_tests/globals.py b/tests/failing_tests/globals.py index 55d9740..20f072f 100644 --- a/tests/failing_tests/globals.py +++ b/tests/failing_tests/globals.py @@ -3,16 +3,19 @@ import logging from pythonbpf import compile, bpf, section, bpfglobal, compile_to_ir from ctypes import c_void_p, c_int64, c_int32 + @bpf @bpfglobal def somevalue() -> c_int32: return c_int32(42) + @bpf @bpfglobal def somevalue2() -> c_int64: return c_int64(69) + @bpf @bpfglobal def somevalue1() -> c_int32: @@ -21,12 +24,14 @@ def somevalue1() -> c_int32: # --- Passing examples --- + # Simple constant return @bpf @bpfglobal def g1() -> c_int64: return c_int64(42) + # Constructor with one constant argument @bpf @bpfglobal @@ -62,15 +67,17 @@ def g2() -> c_int64: # def g6() -> c_int64: # return c_int64(CONST) + # Constructor with multiple args -#TODO: this is not working. should it work ? +# TODO: this is not working. should it work ? @bpf @bpfglobal def g7() -> c_int64: return c_int64(1) + # Dataclass call -#TODO: fails with dataclass +# TODO: fails with dataclass # @dataclass # class Point: # x: c_int64 @@ -91,6 +98,7 @@ def sometag(ctx: c_void_p) -> c_int64: print(f"{somevalue}") return c_int64(1) + @bpf @bpfglobal def LICENSE() -> str: diff --git a/tests/failing_tests/named_arg.py b/tests/failing_tests/named_arg.py index 79ac830..19139df 100644 --- a/tests/failing_tests/named_arg.py +++ b/tests/failing_tests/named_arg.py @@ -11,6 +11,7 @@ from ctypes import c_void_p, c_int64 # We cannot allocate space for the intermediate type now. # We probably need to track the ref/deref chain for each variable. + @bpf @map def count() -> HashMap: diff --git a/tests/failing_tests/undeclared_values.py b/tests/failing_tests/undeclared_values.py index 1267f55..287f444 100644 --- a/tests/failing_tests/undeclared_values.py +++ b/tests/failing_tests/undeclared_values.py @@ -3,6 +3,7 @@ import logging from pythonbpf import compile, bpf, section, bpfglobal, compile_to_ir from ctypes import c_void_p, c_int64 + # This should not pass as somevalue is not declared at all. @bpf @section("tracepoint/syscalls/sys_enter_execve") @@ -11,6 +12,7 @@ def sometag(ctx: c_void_p) -> c_int64: print(f"{somevalue}") # noqa: F821 return c_int64(1) + @bpf @bpfglobal def LICENSE() -> str: diff --git a/tests/failing_tests/xdp_pass.py b/tests/failing_tests/xdp_pass.py index dddce92..57e4a36 100644 --- a/tests/failing_tests/xdp_pass.py +++ b/tests/failing_tests/xdp_pass.py @@ -2,6 +2,8 @@ from pythonbpf import bpf, map, section, bpfglobal, compile, compile_to_ir from pythonbpf.maps import HashMap from pythonbpf.helper import XDP_PASS from vmlinux import struct_xdp_md +from vmlinux import struct_ring_buffer_per_cpu # noqa: F401 +from vmlinux import struct_xdp_buff # noqa: F401 from ctypes import c_int64 # Instructions to how to run this program @@ -11,6 +13,7 @@ from ctypes import c_int64 # 4. Attach object file to any network device with something like ./check.sh xdp examples/xdp_pass.o tailscale0 # 5. send traffic through the device and observe effects + @bpf @map def count() -> HashMap: @@ -39,5 +42,6 @@ def hello_world(ctx: struct_xdp_md) -> c_int64: def LICENSE() -> str: return "GPL" + compile_to_ir("xdp_pass.py", "xdp_pass.ll") compile() diff --git a/tests/passing_tests/return/typecast_var.py b/tests/passing_tests/return/typecast_var.py index 1960edd..141323a 100644 --- a/tests/passing_tests/return/typecast_var.py +++ b/tests/passing_tests/return/typecast_var.py @@ -6,8 +6,8 @@ from ctypes import c_void_p, c_int32 @section("tracepoint/syscalls/sys_enter_execve") def hello_world(ctx: c_void_p) -> c_int32: print("Hello, World!") - a = 1 # int64 - return c_int32(a) # typecast to int32 + a = 1 # int64 + return c_int32(a) # typecast to int32 @bpf