Skip to content

load_global shouldn't load outermost locals. #1332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,11 @@ impl SymbolTableAnalyzer {
if found_in_outer_scope {
// Symbol is in some outer scope.
symbol.is_free = true;
} else if self.tables.is_empty() {
// Don't make assumptions when we don't know.
symbol.scope = SymbolScope::Unknown;
} else {
// Well, it must be a global then :)
// If there are scopes above we can assume global.
symbol.scope = SymbolScope::Global;
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/snippets/global_nonlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ def b():

def x():
def y():
global a
nonlocal b
assert a == 4, a
b = 3
a = "no!" # a here shouldn't be seen by the global above.
b = 2
y()
return b
Expand Down
9 changes: 0 additions & 9 deletions vm/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,6 @@ impl NameProtocol for Scope {
#[cfg_attr(feature = "flame-it", flame("Scope"))]
/// Load a global name.
fn load_global(&self, vm: &VirtualMachine, name: &str) -> Option<PyObjectRef> {
// First, take a look in the outmost local scope (the scope at top level)
let last_local_dict = self.locals.iter().last();
if let Some(local_dict) = last_local_dict {
if let Some(value) = local_dict.get_item_option(name, vm).unwrap() {
return Some(value);
}
}

// Now, take a look at the globals or builtins.
if let Some(value) = self.globals.get_item_option(name, vm).unwrap() {
Some(value)
} else {
Expand Down