From d197a11deb2fe58e95f068274a84eb556063fc10 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Thu, 17 Sep 2026 20:46:04 +0530 Subject: [PATCH] Core: Shorten the unbound-local messages and drop a comment Review on the previous round: the UnboundLocalError-shaped messages said in five lines what one says, and the ast.Global branch of process_stmt explained itself at length for a no-op. Both messages now name the problem and the fix and stop; the branch keeps its debug log only. Co-Authored-By: Claude Fable 5.1 --- pythonbpf/functions/functions_pass.py | 17 +++-------------- pythonbpf/symbols.py | 19 ++++--------------- 2 files changed, 7 insertions(+), 29 deletions(-) diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index 192654d..b1d17b0 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -217,16 +217,10 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab): f"cannot assign to '{name}': it is the context parameter" ) elif name in compilation_context.bpf_globals: - # Binding x anywhere in the body makes it local throughout, so this - # statement reads an unbound local rather than the global. Python - # raises UnboundLocalError; there is no runtime here in which to do - # that, so the program is rejected. + # `x += v` binds x as a local and reads it unbound: UnboundLocalError. raise SyntaxError( - f"local variable '{name}' referenced before assignment: " - f"'{name} += ...' binds '{name}' as a local, which shadows the " - f"@bpfglobal of the same name, and reads it in the same " - f"statement (Python raises UnboundLocalError here). Add " - f"'global {name}' to update the global." + f"local variable '{name}' referenced before assignment; " + f"add 'global {name}' to update the @bpfglobal" ) else: raise SyntaxError(f"augmented assignment to undefined variable '{name}'") @@ -399,11 +393,6 @@ def process_stmt( elif isinstance(stmt, ast.AugAssign): handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab) elif isinstance(stmt, ast.Global): - # Nothing to emit: `global x` binds a name for the whole function body, - # so process_func_body collects every declaration before the first - # statement is lowered (and before allocation, which would otherwise - # give a declared name a stack slot). The branch exists so that a valid - # declaration does not reach the unsupported-statement warning below. logger.debug(f"global declaration of {', '.join(stmt.names)} already bound") elif isinstance(stmt, ast.If): handle_if(func, compilation_context, builder, stmt, local_sym_tab) diff --git a/pythonbpf/symbols.py b/pythonbpf/symbols.py index b942be6..e603d47 100644 --- a/pythonbpf/symbols.py +++ b/pythonbpf/symbols.py @@ -42,24 +42,13 @@ class LocalSymbol(Symbol): shadows_global_from: int | None = None def check_bound_at(self, name: str, lineno: int) -> None: - """Raise if `name` is read at `lineno` before its first binding. - - Python's scoping is function-wide and static: assigning a name anywhere - in a body makes it local everywhere in that body, so a read above the - assignment is an UnboundLocalError rather than a read of the global. - There is no runtime in which to raise that, so a program in this shape - is rejected at compile time. The check applies only to locals that - shadow a @bpfglobal, where staying silent would otherwise load an - uninitialised slot from a name the author expected to be the global. - """ + """Python's UnboundLocalError, at compile time: a local that shadows a + @bpfglobal is unreadable above its binding. No-op for any other local.""" if self.shadows_global_from is None or lineno > self.shadows_global_from: return raise SyntaxError( - f"local variable '{name}' referenced before assignment: the " - f"assignment on line {self.shadows_global_from} makes '{name}' a " - f"local that shadows the @bpfglobal of the same name (Python " - f"raises UnboundLocalError here). Add 'global {name}' if you meant " - f"the global." + f"local variable '{name}' referenced before its assignment on line " + f"{self.shadows_global_from}; add 'global {name}' to use the @bpfglobal" ) def __iter__(self):