diff --git a/src/compile.rs b/src/compile.rs index 403b148..838afe3 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -955,12 +955,21 @@ impl Compiler { self.emit(Instruction::LoadName { name: "__name__".to_string(), - scope: bytecode::NameScope::Free, + scope: bytecode::NameScope::Global, }); self.emit(Instruction::StoreName { name: "__module__".to_string(), 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.emit(Instruction::LoadConst { value: bytecode::Constant::None, @@ -983,7 +992,7 @@ impl Compiler { // Turn code object into function object: self.emit(Instruction::MakeFunction { - flags: bytecode::FunctionOpArg::empty(), + flags: bytecode::FunctionOpArg::NO_NEW_LOCALS, }); self.emit(Instruction::LoadConst { diff --git a/src/symboltable.rs b/src/symboltable.rs index d64dd5b..61a76dd 100644 --- a/src/symboltable.rs +++ b/src/symboltable.rs @@ -387,6 +387,8 @@ impl SymbolTableBuilder { decorator_list, } => { 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.leave_scope(); self.scan_expressions(bases, &ExpressionContext::Load)?;