Skip to content

Commit 72989cd

Browse files
authored
Merge pull request RustPython#1580 from seeeturtle/doc-f
Set docstring of function as None if not declared
2 parents 24cc679 + 5381817 commit 72989cd

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

compiler/src/compile.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,22 +1015,23 @@ impl<O: OutputStream> Compiler<O> {
10151015
}
10161016

10171017
fn store_docstring(&mut self, doc_str: Option<String>) {
1018-
if let Some(doc_string) = doc_str {
1019-
// Duplicate top of stack (the function or class object)
1020-
self.emit(Instruction::Duplicate);
1018+
// Duplicate top of stack (the function or class object)
1019+
self.emit(Instruction::Duplicate);
10211020

1022-
// Doc string value:
1023-
self.emit(Instruction::LoadConst {
1024-
value: bytecode::Constant::String {
1025-
value: doc_string.to_string(),
1021+
// Doc string value:
1022+
self.emit(Instruction::LoadConst {
1023+
value: match doc_str {
1024+
Some(doc) => bytecode::Constant::String {
1025+
value: doc.to_string(),
10261026
},
1027-
});
1027+
None => bytecode::Constant::None, // set docstring None if not declared
1028+
},
1029+
});
10281030

1029-
self.emit(Instruction::Rotate { amount: 2 });
1030-
self.emit(Instruction::StoreAttr {
1031-
name: "__doc__".to_string(),
1032-
});
1033-
}
1031+
self.emit(Instruction::Rotate { amount: 2 });
1032+
self.emit(Instruction::StoreAttr {
1033+
name: "__doc__".to_string(),
1034+
});
10341035
}
10351036

10361037
fn compile_while(

tests/snippets/function.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,8 @@ def f8() -> int:
9595

9696
with assert_raises(SyntaxError):
9797
exec('print(keyword=10, 20)')
98+
99+
def f9():
100+
pass
101+
102+
assert f9.__doc__ == None

0 commit comments

Comments
 (0)