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
This commit is contained in:
joshua1b 2019-11-02 23:47:44 +09:00
parent 0fd098569e
commit 30ab196fdc

View file

@ -1015,14 +1015,16 @@ impl<O: OutputStream> Compiler<O> {
} }
fn store_docstring(&mut self, doc_str: Option<String>) { fn store_docstring(&mut self, doc_str: Option<String>) {
if let Some(doc_string) = doc_str {
// Duplicate top of stack (the function or class object) // Duplicate top of stack (the function or class object)
self.emit(Instruction::Duplicate); self.emit(Instruction::Duplicate);
// Doc string value: // Doc string value:
self.emit(Instruction::LoadConst { self.emit(Instruction::LoadConst {
value: bytecode::Constant::String { value: match doc_str {
value: doc_string.to_string(), Some(doc) => bytecode::Constant::String {
value: doc.to_string(),
},
None => bytecode::Constant::None, // set docstring None if not declared
}, },
}); });
@ -1031,7 +1033,6 @@ impl<O: OutputStream> Compiler<O> {
name: "__doc__".to_string(), name: "__doc__".to_string(),
}); });
} }
}
fn compile_while( fn compile_while(
&mut self, &mut self,