Only overwrite the locals in __build_class__ if it's a class

This commit is contained in:
Noah 2019-09-10 11:37:11 -05:00 committed by coolreader18
parent a29fb0de12
commit fee1b6f2c7
2 changed files with 13 additions and 2 deletions

View file

@ -955,12 +955,21 @@ impl<O: OutputStream> Compiler<O> {
self.emit(Instruction::LoadName { self.emit(Instruction::LoadName {
name: "__name__".to_string(), name: "__name__".to_string(),
scope: bytecode::NameScope::Free, scope: bytecode::NameScope::Global,
}); });
self.emit(Instruction::StoreName { self.emit(Instruction::StoreName {
name: "__module__".to_string(), name: "__module__".to_string(),
scope: bytecode::NameScope::Free, scope: bytecode::NameScope::Free,
}); });
self.emit(Instruction::LoadConst {
value: bytecode::Constant::String {
value: qualified_name.clone(),
},
});
self.emit(Instruction::StoreName {
name: "__qualname__".to_string(),
scope: bytecode::NameScope::Free,
});
self.compile_statements(new_body)?; self.compile_statements(new_body)?;
self.emit(Instruction::LoadConst { self.emit(Instruction::LoadConst {
value: bytecode::Constant::None, value: bytecode::Constant::None,
@ -983,7 +992,7 @@ impl<O: OutputStream> Compiler<O> {
// Turn code object into function object: // Turn code object into function object:
self.emit(Instruction::MakeFunction { self.emit(Instruction::MakeFunction {
flags: bytecode::FunctionOpArg::empty(), flags: bytecode::FunctionOpArg::NO_NEW_LOCALS,
}); });
self.emit(Instruction::LoadConst { self.emit(Instruction::LoadConst {

View file

@ -387,6 +387,8 @@ impl SymbolTableBuilder {
decorator_list, decorator_list,
} => { } => {
self.enter_scope(name, SymbolTableType::Class, statement.location.row()); self.enter_scope(name, SymbolTableType::Class, statement.location.row());
self.register_name("__module__", SymbolUsage::Assigned)?;
self.register_name("__qualname__", SymbolUsage::Assigned)?;
self.scan_statements(body)?; self.scan_statements(body)?;
self.leave_scope(); self.leave_scope();
self.scan_expressions(bases, &ExpressionContext::Load)?; self.scan_expressions(bases, &ExpressionContext::Load)?;