Skip to content

Commit d197a11

Browse files
r41k0uclaude
andcommitted
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 <noreply@anthropic.com>
1 parent dc212c3 commit d197a11

2 files changed

Lines changed: 7 additions & 29 deletions

File tree

‎pythonbpf/functions/functions_pass.py‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,10 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
217217
f"cannot assign to '{name}': it is the context parameter"
218218
)
219219
elif name in compilation_context.bpf_globals:
220-
# Binding x anywhere in the body makes it local throughout, so this
221-
# statement reads an unbound local rather than the global. Python
222-
# raises UnboundLocalError; there is no runtime here in which to do
223-
# that, so the program is rejected.
220+
# `x += v` binds x as a local and reads it unbound: UnboundLocalError.
224221
raise SyntaxError(
225-
f"local variable '{name}' referenced before assignment: "
226-
f"'{name} += ...' binds '{name}' as a local, which shadows the "
227-
f"@bpfglobal of the same name, and reads it in the same "
228-
f"statement (Python raises UnboundLocalError here). Add "
229-
f"'global {name}' to update the global."
222+
f"local variable '{name}' referenced before assignment; "
223+
f"add 'global {name}' to update the @bpfglobal"
230224
)
231225
else:
232226
raise SyntaxError(f"augmented assignment to undefined variable '{name}'")
@@ -399,11 +393,6 @@ def process_stmt(
399393
elif isinstance(stmt, ast.AugAssign):
400394
handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab)
401395
elif isinstance(stmt, ast.Global):
402-
# Nothing to emit: `global x` binds a name for the whole function body,
403-
# so process_func_body collects every declaration before the first
404-
# statement is lowered (and before allocation, which would otherwise
405-
# give a declared name a stack slot). The branch exists so that a valid
406-
# declaration does not reach the unsupported-statement warning below.
407396
logger.debug(f"global declaration of {', '.join(stmt.names)} already bound")
408397
elif isinstance(stmt, ast.If):
409398
handle_if(func, compilation_context, builder, stmt, local_sym_tab)

‎pythonbpf/symbols.py‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,13 @@ class LocalSymbol(Symbol):
4242
shadows_global_from: int | None = None
4343

4444
def check_bound_at(self, name: str, lineno: int) -> None:
45-
"""Raise if `name` is read at `lineno` before its first binding.
46-
47-
Python's scoping is function-wide and static: assigning a name anywhere
48-
in a body makes it local everywhere in that body, so a read above the
49-
assignment is an UnboundLocalError rather than a read of the global.
50-
There is no runtime in which to raise that, so a program in this shape
51-
is rejected at compile time. The check applies only to locals that
52-
shadow a @bpfglobal, where staying silent would otherwise load an
53-
uninitialised slot from a name the author expected to be the global.
54-
"""
45+
"""Python's UnboundLocalError, at compile time: a local that shadows a
46+
@bpfglobal is unreadable above its binding. No-op for any other local."""
5547
if self.shadows_global_from is None or lineno > self.shadows_global_from:
5648
return
5749
raise SyntaxError(
58-
f"local variable '{name}' referenced before assignment: the "
59-
f"assignment on line {self.shadows_global_from} makes '{name}' a "
60-
f"local that shadows the @bpfglobal of the same name (Python "
61-
f"raises UnboundLocalError here). Add 'global {name}' if you meant "
62-
f"the global."
50+
f"local variable '{name}' referenced before its assignment on line "
51+
f"{self.shadows_global_from}; add 'global {name}' to use the @bpfglobal"
6352
)
6453

6554
def __iter__(self):

0 commit comments

Comments
 (0)