From 30ab196fdc73390a3cd6d8d81c0e7638fac5b75a Mon Sep 17 00:00:00 2001 From: joshua1b Date: Sat, 2 Nov 2019 23:47:44 +0900 Subject: [PATCH] Set docstring of function as None if not declared In cpython, if there is no docstring declared in function definition, the `__doc__` attribute of the function is None. So this implements the behavior. Fix #1523 --- src/compile.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/compile.rs b/src/compile.rs index ca185e6..ac4f00a 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -1015,22 +1015,23 @@ impl Compiler { } fn store_docstring(&mut self, doc_str: Option) { - if let Some(doc_string) = doc_str { - // Duplicate top of stack (the function or class object) - self.emit(Instruction::Duplicate); + // Duplicate top of stack (the function or class object) + self.emit(Instruction::Duplicate); - // Doc string value: - self.emit(Instruction::LoadConst { - value: bytecode::Constant::String { - value: doc_string.to_string(), + // Doc string value: + self.emit(Instruction::LoadConst { + value: match doc_str { + Some(doc) => bytecode::Constant::String { + value: doc.to_string(), }, - }); + None => bytecode::Constant::None, // set docstring None if not declared + }, + }); - self.emit(Instruction::Rotate { amount: 2 }); - self.emit(Instruction::StoreAttr { - name: "__doc__".to_string(), - }); - } + self.emit(Instruction::Rotate { amount: 2 }); + self.emit(Instruction::StoreAttr { + name: "__doc__".to_string(), + }); } fn compile_while(