Compile the class body into a code object = compiler_class_body
(
&mut self,
name: &str,
body: &[ast::Stmt],
type_params: Option<&ast::TypeParams>,
firstlineno: u32,
)
| 4970 | /// Compile the class body into a code object |
| 4971 | // = compiler_class_body |
| 4972 | fn compile_class_body( |
| 4973 | &mut self, |
| 4974 | name: &str, |
| 4975 | body: &[ast::Stmt], |
| 4976 | type_params: Option<&ast::TypeParams>, |
| 4977 | firstlineno: u32, |
| 4978 | ) -> CompileResult<CodeObject> { |
| 4979 | // 1. Enter class scope |
| 4980 | let key = self.symbol_table_stack.len(); |
| 4981 | self.push_symbol_table()?; |
| 4982 | self.enter_scope(name, CompilerScope::Class, key, firstlineno)?; |
| 4983 | |
| 4984 | // Set qualname using the new method |
| 4985 | let qualname = self.set_qualname(); |
| 4986 | |
| 4987 | // For class scopes, set u_private to the class name for name mangling |
| 4988 | self.code_stack.last_mut().unwrap().private = Some(name.to_owned()); |
| 4989 | |
| 4990 | // 2. Set up class namespace |
| 4991 | let (doc_str, body) = split_doc(body, &self.opts); |
| 4992 | |
| 4993 | // Load __name__ and store as __module__ |
| 4994 | let dunder_name = self.name("__name__"); |
| 4995 | emit!(self, Instruction::LoadName { namei: dunder_name }); |
| 4996 | let dunder_module = self.name("__module__"); |
| 4997 | emit!( |
| 4998 | self, |
| 4999 | Instruction::StoreName { |
| 5000 | namei: dunder_module |
| 5001 | } |
| 5002 | ); |
| 5003 | |
| 5004 | // Store __qualname__ |
| 5005 | self.emit_load_const(ConstantData::Str { |
| 5006 | value: qualname.into(), |
| 5007 | }); |
| 5008 | let qualname_name = self.name("__qualname__"); |
| 5009 | emit!( |
| 5010 | self, |
| 5011 | Instruction::StoreName { |
| 5012 | namei: qualname_name |
| 5013 | } |
| 5014 | ); |
| 5015 | |
| 5016 | // Store __firstlineno__ before __doc__ |
| 5017 | self.emit_load_const(ConstantData::Integer { |
| 5018 | value: BigInt::from(firstlineno), |
| 5019 | }); |
| 5020 | let firstlineno_name = self.name("__firstlineno__"); |
| 5021 | emit!( |
| 5022 | self, |
| 5023 | Instruction::StoreName { |
| 5024 | namei: firstlineno_name |
| 5025 | } |
| 5026 | ); |
| 5027 | |
| 5028 | // PEP 649: Initialize __classdict__ cell (before __doc__) |
| 5029 | if self.current_symbol_table().needs_classdict { |
no test coverage detected