Thanks to visit codestin.com
Credit goes to github.com

Skip to content

filter out doc strings with opt level 2 as final step to enable test_builtin.py test_compile #5274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,6 @@ def test_chr(self):
def test_cmp(self):
self.assertTrue(not hasattr(builtins, "cmp"))

# TODO: RUSTPYTHON optval=2 does not remove docstrings
@unittest.expectedFailure
def test_compile(self):
compile('print(1)\n', '', 'exec')
bom = b'\xef\xbb\xbf'
Expand Down
17 changes: 12 additions & 5 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl Compiler {
let size_before = self.code_stack.len();
self.symbol_table_stack.push(symbol_table);

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

let (doc_str, body) = split_doc(body);
let (doc_str, body) = split_doc(body, &self.opts);

self.current_code_info()
.constants
Expand Down Expand Up @@ -1383,7 +1383,7 @@ impl Compiler {

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

let (doc_str, body) = split_doc(body);
let (doc_str, body) = split_doc(body, &self.opts);

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

fn split_doc(body: &[located_ast::Stmt]) -> (Option<String>, &[located_ast::Stmt]) {
fn split_doc<'a>(
body: &'a [located_ast::Stmt],
opts: &CompileOpts,
) -> (Option<String>, &'a [located_ast::Stmt]) {
if let Some((located_ast::Stmt::Expr(expr), body_rest)) = body.split_first() {
if let Some(doc) = try_get_constant_string(std::slice::from_ref(&expr.value)) {
return (Some(doc), body_rest);
if opts.optimize < 2 {
return (Some(doc), body_rest);
} else {
return (None, body_rest);
}
}
}
(None, body)
Expand Down