Skip to content

Commit 752ac1d

Browse files
Merge pull request RustPython#1232 from corona10/RustPythongh-1212
RustPythongh-1212: Fix scope_for_name to catch NameError properly
2 parents d83cfb4 + 6df3800 commit 752ac1d

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

compiler/src/compile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,9 @@ impl<O: OutputStream> Compiler<O> {
19121912
fn lookup_name(&self, name: &str) -> &Symbol {
19131913
// println!("Looking up {:?}", name);
19141914
let scope = self.scope_stack.last().unwrap();
1915-
scope.lookup(name).unwrap()
1915+
scope.lookup(name).expect(
1916+
"The symbol must be present in the symbol table, even when it is undefined in python.",
1917+
)
19161918
}
19171919

19181920
// Low level helper functions:

compiler/src/symboltable.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,11 @@ impl SymbolTableBuilder {
244244
} => {
245245
self.scan_expressions(decorator_list)?;
246246
self.register_name(name, SymbolRole::Assigned)?;
247-
248-
self.enter_function(args)?;
249-
250-
self.scan_statements(body)?;
251247
if let Some(expression) = returns {
252248
self.scan_expression(expression)?;
253249
}
250+
self.enter_function(args)?;
251+
self.scan_statements(body)?;
254252
self.leave_scope();
255253
}
256254
ClassDef {

tests/snippets/function.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
__name__ = "function"
32

43

@@ -72,3 +71,20 @@ def nested():
7271

7372

7473
f6()
74+
75+
76+
def f7():
77+
try:
78+
def t() -> void: # noqa: F821
79+
pass
80+
except NameError:
81+
return True
82+
return False
83+
84+
assert f7()
85+
86+
87+
def f8() -> int:
88+
return 10
89+
90+
assert f8() == 10

0 commit comments

Comments
 (0)