Fix scope_for_name to catch NameError properly

This commit is contained in:
Dong-hee Na
2019-08-12 00:32:57 +09:00
parent 0129bb5c75
commit 6df3800055
3 changed files with 22 additions and 6 deletions

View File

@@ -1877,7 +1877,9 @@ impl<O: OutputStream> Compiler<O> {
fn lookup_name(&self, name: &str) -> &Symbol {
// println!("Looking up {:?}", name);
let scope = self.scope_stack.last().unwrap();
scope.lookup(name).unwrap()
scope.lookup(name).expect(
"The symbol must be present in the symbol table, even when it is undefined in python.",
)
}
// Low level helper functions:

View File

@@ -244,13 +244,11 @@ impl SymbolTableBuilder {
} => {
self.scan_expressions(decorator_list)?;
self.register_name(name, SymbolRole::Assigned)?;
self.enter_function(args)?;
self.scan_statements(body)?;
if let Some(expression) = returns {
self.scan_expression(expression)?;
}
self.enter_function(args)?;
self.scan_statements(body)?;
self.leave_scope();
}
ClassDef {

View File

@@ -1,4 +1,3 @@
__name__ = "function"
@@ -72,3 +71,20 @@ def f6():
f6()
def f7():
try:
def t() -> void: # noqa: F821
pass
except NameError:
return True
return False
assert f7()
def f8() -> int:
return 10
assert f8() == 10