Skip to content

Commit af8bed6

Browse files
hydrogen602youknowone
authored andcommitted
filter out doc strings with opt level 2
1 parent 462f54f commit af8bed6

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Lib/test/test_builtin.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,6 @@ def test_chr(self):
327327
def test_cmp(self):
328328
self.assertTrue(not hasattr(builtins, "cmp"))
329329

330-
# TODO: RUSTPYTHON optval=2 does not remove docstrings
331-
@unittest.expectedFailure
332330
def test_compile(self):
333331
compile('print(1)\n', '', 'exec')
334332
bom = b'\xef\xbb\xbf'

compiler/codegen/src/compile.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl Compiler {
349349
let size_before = self.code_stack.len();
350350
self.symbol_table_stack.push(symbol_table);
351351

352-
let (doc, statements) = split_doc(body);
352+
let (doc, statements) = split_doc(body, &self.opts);
353353
if let Some(value) = doc {
354354
self.emit_constant(ConstantData::Str { value });
355355
let doc = self.name("__doc__");
@@ -1187,7 +1187,7 @@ impl Compiler {
11871187
let qualified_name = self.qualified_path.join(".");
11881188
self.push_qualified_path("<locals>");
11891189

1190-
let (doc_str, body) = split_doc(body);
1190+
let (doc_str, body) = split_doc(body, &self.opts);
11911191

11921192
self.current_code_info()
11931193
.constants
@@ -1383,7 +1383,7 @@ impl Compiler {
13831383

13841384
self.push_output(bytecode::CodeFlags::empty(), 0, 0, 0, name.to_owned());
13851385

1386-
let (doc_str, body) = split_doc(body);
1386+
let (doc_str, body) = split_doc(body, &self.opts);
13871387

13881388
let dunder_name = self.name("__name__");
13891389
emit!(self, Instruction::LoadGlobal(dunder_name));
@@ -2875,10 +2875,17 @@ impl EmitArg<bytecode::Label> for ir::BlockIdx {
28752875
}
28762876
}
28772877

2878-
fn split_doc(body: &[located_ast::Stmt]) -> (Option<String>, &[located_ast::Stmt]) {
2878+
fn split_doc<'a>(
2879+
body: &'a [located_ast::Stmt],
2880+
opts: &CompileOpts,
2881+
) -> (Option<String>, &'a [located_ast::Stmt]) {
28792882
if let Some((located_ast::Stmt::Expr(expr), body_rest)) = body.split_first() {
28802883
if let Some(doc) = try_get_constant_string(std::slice::from_ref(&expr.value)) {
2881-
return (Some(doc), body_rest);
2884+
if opts.optimize < 2 {
2885+
return (Some(doc), body_rest);
2886+
} else {
2887+
return (None, body_rest);
2888+
}
28822889
}
28832890
}
28842891
(None, body)

0 commit comments

Comments
 (0)