Skip to content

Commit 6788f18

Browse files
committed
Use StoreAttr for __doc__
1 parent ae2c2f6 commit 6788f18

File tree

3 files changed

+36
-63
lines changed

3 files changed

+36
-63
lines changed

vm/src/builtins.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,6 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
766766
pub fn builtin_build_class_(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {
767767
let function = args.shift();
768768
let name_arg = args.shift();
769-
let doc = args.shift();
770769
let bases = args.args.clone();
771770
let mut metaclass = if let Some(metaclass) = args.get_optional_kwarg("metaclass") {
772771
PyClassRef::try_from_object(vm, metaclass)?
@@ -799,6 +798,5 @@ pub fn builtin_build_class_(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResu
799798
vec![name_arg, bases, namespace.into_object()],
800799
)?;
801800
cells.set_item(&vm.ctx, "__class__", class.clone());
802-
vm.ctx.set_attr(&class, "__doc__", doc);
803801
Ok(class)
804802
}

vm/src/compile.rs

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -598,17 +598,7 @@ impl Compiler {
598598
self.in_function_def = true;
599599
let mut flags = self.enter_function(name, args)?;
600600

601-
let doc = get_doc(body);
602-
let (new_body, doc_str) = match doc {
603-
Some(val) => {
604-
if let Some((_, body_rest)) = body.split_first() {
605-
(body_rest, val)
606-
} else {
607-
(body, "".to_string())
608-
}
609-
}
610-
None => (body, "".to_string()),
611-
};
601+
let (new_body, doc_str) = get_doc(body);
612602

613603
self.compile_statements(new_body)?;
614604

@@ -657,10 +647,6 @@ impl Compiler {
657647
});
658648
}
659649

660-
self.emit(Instruction::LoadConst {
661-
value: bytecode::Constant::String { value: doc_str },
662-
});
663-
664650
self.emit(Instruction::LoadConst {
665651
value: bytecode::Constant::Code {
666652
code: Box::new(code),
@@ -679,6 +665,20 @@ impl Compiler {
679665
self.emit(Instruction::StoreName {
680666
name: name.to_string(),
681667
});
668+
669+
if let Some(doc_string) = doc_str {
670+
self.emit(Instruction::LoadConst {
671+
value: bytecode::Constant::String {
672+
value: doc_string.to_string(),
673+
},
674+
});
675+
self.emit(Instruction::LoadName {
676+
name: name.to_string(),
677+
});
678+
self.emit(Instruction::StoreAttr {
679+
name: "__doc__".to_string(),
680+
});
681+
}
682682
self.in_loop = was_in_loop;
683683
self.in_function_def = was_in_function_def;
684684
Ok(())
@@ -707,17 +707,7 @@ impl Compiler {
707707
name.to_string(),
708708
));
709709

710-
let doc = get_doc(body);
711-
let (new_body, doc_str) = match doc {
712-
Some(val) => {
713-
if let Some((_, body_rest)) = body.split_first() {
714-
(body_rest, val)
715-
} else {
716-
(body, "".to_string())
717-
}
718-
}
719-
None => (body, "".to_string()),
720-
};
710+
let (new_body, doc_str) = get_doc(body);
721711

722712
self.compile_statements(new_body)?;
723713
self.emit(Instruction::LoadConst {
@@ -727,13 +717,6 @@ impl Compiler {
727717

728718
let code = self.pop_code_object();
729719

730-
// function doc
731-
self.emit(Instruction::LoadConst {
732-
value: bytecode::Constant::String {
733-
value: "".to_string(),
734-
},
735-
});
736-
737720
self.emit(Instruction::LoadConst {
738721
value: bytecode::Constant::Code {
739722
code: Box::new(code),
@@ -756,11 +739,6 @@ impl Compiler {
756739
},
757740
});
758741

759-
// class doc
760-
self.emit(Instruction::LoadConst {
761-
value: bytecode::Constant::String { value: doc_str },
762-
});
763-
764742
for base in bases {
765743
self.compile_expression(base)?;
766744
}
@@ -785,11 +763,11 @@ impl Compiler {
785763
},
786764
});
787765
self.emit(Instruction::CallFunction {
788-
typ: CallType::Keyword(3 + keywords.len() + bases.len()),
766+
typ: CallType::Keyword(2 + keywords.len() + bases.len()),
789767
});
790768
} else {
791769
self.emit(Instruction::CallFunction {
792-
typ: CallType::Positional(3 + bases.len()),
770+
typ: CallType::Positional(2 + bases.len()),
793771
});
794772
}
795773

@@ -798,6 +776,19 @@ impl Compiler {
798776
self.emit(Instruction::StoreName {
799777
name: name.to_string(),
800778
});
779+
if let Some(doc_string) = doc_str {
780+
self.emit(Instruction::LoadConst {
781+
value: bytecode::Constant::String {
782+
value: doc_string.to_string(),
783+
},
784+
});
785+
self.emit(Instruction::LoadName {
786+
name: name.to_string(),
787+
});
788+
self.emit(Instruction::StoreAttr {
789+
name: "__doc__".to_string(),
790+
});
791+
}
801792
self.in_loop = was_in_loop;
802793
Ok(())
803794
}
@@ -1168,14 +1159,6 @@ impl Compiler {
11681159
self.compile_expression(body)?;
11691160
self.emit(Instruction::ReturnValue);
11701161
let code = self.pop_code_object();
1171-
1172-
// function doc
1173-
self.emit(Instruction::LoadConst {
1174-
value: bytecode::Constant::String {
1175-
value: "".to_string(),
1176-
},
1177-
});
1178-
11791162
self.emit(Instruction::LoadConst {
11801163
value: bytecode::Constant::Code {
11811164
code: Box::new(code),
@@ -1462,13 +1445,6 @@ impl Compiler {
14621445
// Fetch code for listcomp function:
14631446
let code = self.pop_code_object();
14641447

1465-
// function doc
1466-
self.emit(Instruction::LoadConst {
1467-
value: bytecode::Constant::String {
1468-
value: "".to_string(),
1469-
},
1470-
});
1471-
14721448
// List comprehension code:
14731449
self.emit(Instruction::LoadConst {
14741450
value: bytecode::Constant::Code {
@@ -1569,17 +1545,19 @@ impl Compiler {
15691545
}
15701546
}
15711547

1572-
fn get_doc(body: &[ast::LocatedStatement]) -> Option<String> {
1548+
fn get_doc(body: &[ast::LocatedStatement]) -> (&[ast::LocatedStatement], Option<String>) {
15731549
if let Some(val) = body.get(0) {
15741550
if let ast::Statement::Expression { ref expression } = val.node {
15751551
if let ast::Expression::String { ref value } = expression {
15761552
if let ast::StringGroup::Constant { ref value } = value {
1577-
return Some(value.to_string());
1553+
if let Some((_, body_rest)) = body.split_first() {
1554+
return (body_rest, Some(value.to_string()));
1555+
}
15781556
}
15791557
}
15801558
}
15811559
}
1582-
None
1560+
(body, None)
15831561
}
15841562

15851563
#[cfg(test)]

vm/src/frame.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,6 @@ impl Frame {
568568
.downcast()
569569
.expect("Second to top value on the stack must be a code object");
570570

571-
let doc = self.pop_value();
572-
573571
let annotations = if flags.contains(bytecode::FunctionOpArg::HAS_ANNOTATIONS) {
574572
self.pop_value()
575573
} else {
@@ -588,7 +586,6 @@ impl Frame {
588586
let obj = vm.ctx.new_function(code_obj, scope, defaults);
589587

590588
vm.ctx.set_attr(&obj, "__annotations__", annotations);
591-
vm.ctx.set_attr(&obj, "__doc__", doc);
592589

593590
self.push_value(obj);
594591
Ok(None)

0 commit comments

Comments
 (0)