mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Tests: Pin augmented assignment on struct fields and the shadowing error
A struct-field test with a non-add operator and a sub-64-bit field (the narrow-on-store path), and a strict expected failure for counter += 1 without a global statement, which now fails as loudly as plain assignment does. Ruff flags that line as F823 (referenced before assignment) -- the same UnboundLocalError Python itself would raise, which is exactly the semantics the global statement requirement mirrors. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
This commit is contained in:
28
tests/failing_tests/globals_augassign_shadowing.py
Normal file
28
tests/failing_tests/globals_augassign_shadowing.py
Normal file
@ -0,0 +1,28 @@
|
||||
# `counter += 1` without `global counter` must be the same loud error as plain
|
||||
# assignment. Before direct lowering this slipped past the allocation pass
|
||||
# (which only walks Assign) and failed later with a misleading message.
|
||||
from pythonbpf import bpf, section, bpfglobal, compile
|
||||
from ctypes import c_void_p, c_int64, c_uint64
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def counter() -> c_uint64:
|
||||
return c_uint64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/raw_syscalls/sys_enter")
|
||||
def prog(ctx: c_void_p) -> c_int64:
|
||||
counter += 1 # noqa: F823, F841 -- missing `global counter` on purpose; this is
|
||||
# the UnboundLocalError shape, and the compiler must reject it just as loudly
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
35
tests/passing_tests/assign/augassign_struct_field.py
Normal file
35
tests/passing_tests/assign/augassign_struct_field.py
Normal file
@ -0,0 +1,35 @@
|
||||
# Augmented assignment to a struct field: one GEP, load, op, store. Also a
|
||||
# non-add operator and a sub-64-bit field, which exercises the narrow-on-store
|
||||
# path of the direct lowering.
|
||||
from pythonbpf import bpf, section, bpfglobal, compile, struct
|
||||
from ctypes import c_void_p, c_int64, c_uint64, c_uint32
|
||||
|
||||
|
||||
@bpf
|
||||
@struct
|
||||
class data_t:
|
||||
pid: c_uint64
|
||||
hits: c_uint32
|
||||
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/raw_syscalls/sys_enter")
|
||||
def prog(ctx: c_void_p) -> c_int64:
|
||||
x = 5
|
||||
x -= 1
|
||||
dat = data_t()
|
||||
dat.pid = 10
|
||||
dat.pid += x
|
||||
dat.hits = 1
|
||||
dat.hits <<= 2
|
||||
print(f"pid {dat.pid} hits {dat.hits} x {x}")
|
||||
return c_int64(0)
|
||||
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
compile()
|
||||
@ -38,3 +38,5 @@
|
||||
"failing_tests/globals_undeclared_name.py" = {reason = "`global x` naming something that is not a @bpfglobal is a compile error", level = "ir"}
|
||||
|
||||
"failing_tests/globals_bad_type.py" = {reason = "Non-integer-scalar globals are not supported in milestone 1 (NotImplementedError by design)", level = "ir"}
|
||||
|
||||
"failing_tests/globals_augassign_shadowing.py" = {reason = "Augmented assignment to a global name without a `global` statement is a deliberate compile error (would shadow the BPF global)", level = "ir"}
|
||||
|
||||
Reference in New Issue
Block a user