Skip to content

Commit 814260e

Browse files
authored
Merge pull request RustPython#1332 from RustPython/load_global_loading_local
load_global shouldn't load outermost locals.
2 parents ba9736b + 527f3ff commit 814260e

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

compiler/src/symboltable.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,11 @@ impl SymbolTableAnalyzer {
251251
if found_in_outer_scope {
252252
// Symbol is in some outer scope.
253253
symbol.is_free = true;
254+
} else if self.tables.is_empty() {
255+
// Don't make assumptions when we don't know.
256+
symbol.scope = SymbolScope::Unknown;
254257
} else {
255-
// Well, it must be a global then :)
258+
// If there are scopes above we can assume global.
256259
symbol.scope = SymbolScope::Global;
257260
}
258261
}

tests/snippets/global_nonlocal.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ def b():
1515

1616
def x():
1717
def y():
18+
global a
1819
nonlocal b
20+
assert a == 4, a
1921
b = 3
22+
a = "no!" # a here shouldn't be seen by the global above.
2023
b = 2
2124
y()
2225
return b

vm/src/scope.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,6 @@ impl NameProtocol for Scope {
179179
#[cfg_attr(feature = "flame-it", flame("Scope"))]
180180
/// Load a global name.
181181
fn load_global(&self, vm: &VirtualMachine, name: &str) -> Option<PyObjectRef> {
182-
// First, take a look in the outmost local scope (the scope at top level)
183-
let last_local_dict = self.locals.iter().last();
184-
if let Some(local_dict) = last_local_dict {
185-
if let Some(value) = local_dict.get_item_option(name, vm).unwrap() {
186-
return Some(value);
187-
}
188-
}
189-
190-
// Now, take a look at the globals or builtins.
191182
if let Some(value) = self.globals.get_item_option(name, vm).unwrap() {
192183
Some(value)
193184
} else {

0 commit comments

Comments
 (0)