mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 09:58:34 +00:00
Merge branch 'feat/global-variables' into feat/integer-signedness
This commit is contained in:
@ -221,16 +221,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}'")
|
||||
@ -404,11 +398,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)
|
||||
|
||||
@ -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):
|
||||
|
||||
Reference in New Issue
Block a user