Skip to content

Commit 8290fec

Browse files
tests: Add loop and AnnAssign xfail tests (#103)
- process_stmt now raises NotImplementedError on an unsupported statement instead of warning: a dropped statement silently changes what the program means (a for/while loop compiled to nothing). - failing_tests/loops/: 8 xfail tests for for/while/break/continue/nested loops and map iteration - failing_tests/assign/annotated_assign.py: xfail for `x: c_int64 = 0` (ast.AnnAssign is not handled)
1 parent a949a90 commit 8290fec

11 files changed

Lines changed: 224 additions & 5 deletions

File tree

‎pythonbpf/functions/functions_pass.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,11 @@ def process_stmt(
401401
builder, stmt, local_sym_tab, ret_type, compilation_context
402402
)
403403
else:
404-
# Silently dropping a statement makes the program mean something other
405-
# than what it says, so say so.
406-
logger.warning(
407-
f"Unsupported statement on line {getattr(stmt, 'lineno', '?')}, "
408-
f"ignored: {type(stmt).__name__}"
404+
# Dropping a statement makes the program mean something other than what
405+
# it says (a loop compiles to nothing), so refuse rather than warn.
406+
raise NotImplementedError(
407+
f"Unsupported statement on line {getattr(stmt, 'lineno', '?')}: "
408+
f"{type(stmt).__name__}"
409409
)
410410
return did_return
411411

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_execve")
7+
def hello(ctx: c_void_p) -> c_int64:
8+
total: c_int64 = 0
9+
return total
10+
11+
12+
@bpf
13+
@bpfglobal
14+
def LICENSE() -> str:
15+
return "GPL"
16+
17+
18+
compile()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_execve")
7+
def hello(ctx: c_void_p) -> c_int64:
8+
total: c_int64 = 0
9+
for i in range(10):
10+
if i == 5:
11+
break
12+
total = total + 1
13+
return total
14+
15+
16+
@bpf
17+
@bpfglobal
18+
def LICENSE() -> str:
19+
return "GPL"
20+
21+
22+
compile()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_execve")
7+
def hello(ctx: c_void_p) -> c_int64:
8+
total: c_int64 = 0
9+
for i in range(10):
10+
if i % 2 == 0:
11+
continue
12+
total = total + i
13+
return total
14+
15+
16+
@bpf
17+
@bpfglobal
18+
def LICENSE() -> str:
19+
return "GPL"
20+
21+
22+
compile()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from pythonbpf import bpf, map, bpfglobal, section, compile
2+
from pythonbpf.maps import HashMap
3+
from ctypes import c_void_p, c_int64, c_int32, c_uint64
4+
5+
6+
@bpf
7+
@map
8+
def mymap() -> HashMap:
9+
return HashMap(key=c_int32, value=c_uint64, max_entries=16)
10+
11+
12+
# Imagined sugar over the kernel's bpf_for_each_map_elem() callback helper:
13+
# iterating a map's entries directly from a `for` statement.
14+
@bpf
15+
@section("tracepoint/syscalls/sys_enter_execve")
16+
def hello(ctx: c_void_p) -> c_int64:
17+
total: c_int64 = 0
18+
for k, v in mymap.items():
19+
total = total + 1
20+
return total
21+
22+
23+
@bpf
24+
@bpfglobal
25+
def LICENSE() -> str:
26+
return "GPL"
27+
28+
29+
compile()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_execve")
7+
def hello(ctx: c_void_p) -> c_int64:
8+
total: c_int64 = 0
9+
for i in range(0, 10, 2):
10+
total = total + i
11+
return total
12+
13+
14+
@bpf
15+
@bpfglobal
16+
def LICENSE() -> str:
17+
return "GPL"
18+
19+
20+
compile()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_execve")
7+
def hello(ctx: c_void_p) -> c_int64:
8+
total: c_int64 = 0
9+
for i in range(10):
10+
total = total + 1
11+
return total
12+
13+
14+
@bpf
15+
@bpfglobal
16+
def LICENSE() -> str:
17+
return "GPL"
18+
19+
20+
compile()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_execve")
7+
def hello(ctx: c_void_p) -> c_int64:
8+
total: c_int64 = 0
9+
for i in range(3):
10+
for j in range(3):
11+
total = total + 1
12+
return total
13+
14+
15+
@bpf
16+
@bpfglobal
17+
def LICENSE() -> str:
18+
return "GPL"
19+
20+
21+
compile()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_execve")
7+
def hello(ctx: c_void_p) -> c_int64:
8+
i: c_int64 = 0
9+
while i < 10:
10+
i = i + 1
11+
return i
12+
13+
14+
@bpf
15+
@bpfglobal
16+
def LICENSE() -> str:
17+
return "GPL"
18+
19+
20+
compile()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pythonbpf import bpf, section, bpfglobal, compile
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_execve")
7+
def hello(ctx: c_void_p) -> c_int64:
8+
total: c_int64 = 0
9+
while True:
10+
total = total + 1
11+
if total == 10:
12+
break
13+
return total
14+
15+
16+
@bpf
17+
@bpfglobal
18+
def LICENSE() -> str:
19+
return "GPL"
20+
21+
22+
compile()

0 commit comments

Comments
 (0)