mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Core: Shadow a @bpfglobal with a local when a write has no global statement
Assigning a global's name without declaring it `global` was a compile error. Python makes it a local instead: the name is local for the whole function body, the global keeps its value, and only a `global` statement writes through to it. Do that, so the same program means the same thing in Python and in BPF. Python's scoping is function-wide and static, which the compiler gets for free: allocation runs as a pre-pass over the whole body, so a name bound anywhere is in local_sym_tab before the first statement is lowered, and reads resolve local-first. What does not come for free is the read above the binding, which Python answers with UnboundLocalError rather than the global's value. There is no runtime here in which to raise that, so the local records where its binding ends and a read above it is rejected, with the message Python would have given. Silence there would load an uninitialised stack slot through a name the author expected to be the global. `counter += 1` without `global counter` stays an error for the same reason: it binds a local and reads it in the same statement. Only its message changes, from a shadowing refusal to the UnboundLocalError it actually is. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -56,12 +56,14 @@ def handle_assign_allocation(compilation_context, builder, stmt, local_sym_tab):
|
||||
logger.debug(f"'{var_name}' already bound, no allocation needed")
|
||||
continue
|
||||
|
||||
# Not declared `global`, yet named like one: in real Python this would
|
||||
# create a shadowing local. Refuse rather than guess which was meant.
|
||||
if var_name in compilation_context.bpf_globals:
|
||||
raise SyntaxError(
|
||||
f"assignment to '{var_name}' shadows the BPF global of the same "
|
||||
f"name — add 'global {var_name}' to write to it"
|
||||
# Not declared `global`, yet named like one: Python creates a local
|
||||
# that shadows the global for the whole function body, and leaves the
|
||||
# global untouched. Do the same.
|
||||
shadows_global = var_name in compilation_context.bpf_globals
|
||||
if shadows_global:
|
||||
logger.info(
|
||||
f"'{var_name}' is assigned without a 'global' declaration, so it "
|
||||
f"is a local shadowing the @bpfglobal of the same name"
|
||||
)
|
||||
|
||||
# Determine type and allocate based on rval
|
||||
@ -86,6 +88,14 @@ def handle_assign_allocation(compilation_context, builder, stmt, local_sym_tab):
|
||||
f"Unsupported assignment value type for {var_name}: {type(rval).__name__}"
|
||||
)
|
||||
|
||||
if shadows_global and var_name in local_sym_tab:
|
||||
# Where the binding ends, so that a read above it is reported the
|
||||
# way Python reports it. end_lineno, not lineno, so a read on a
|
||||
# continuation line of a multi-line binding counts as above it too.
|
||||
local_sym_tab[var_name].shadows_global_from = (
|
||||
getattr(stmt, "end_lineno", None) or target.lineno
|
||||
)
|
||||
|
||||
|
||||
def _allocate_for_call(builder, var_name, rval, local_sym_tab, compilation_context):
|
||||
"""Allocate memory for variable assigned from a call."""
|
||||
|
||||
Reference in New Issue
Block a user