Skip to content

Commit ddecd7f

Browse files
committed
Resolve test_inspect bytecode parity gaps
1 parent d8dee81 commit ddecd7f

2 files changed

Lines changed: 101 additions & 46 deletions

File tree

‎Lib/test/test_inspect/test_inspect.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,6 @@ def test_getsource_on_generated_class(self):
888888
self.assertRaises(OSError, inspect.getsourcelines, A)
889889
self.assertIsNone(inspect.getcomments(A))
890890

891-
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: OSError not raised by getsource
892891
def test_getsource_on_class_without_firstlineno(self):
893892
__firstlineno__ = 1
894893
class C:

‎crates/codegen/src/compile.rs‎

Lines changed: 101 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,7 @@ impl Compiler {
27232723
let mut parent_idx = stack_size - 2;
27242724
let mut parent = &self.code_stack[parent_idx];
27252725

2726-
let parent_scope = self
2726+
let mut parent_scope = self
27272727
.symbol_table_stack
27282728
.get(parent_idx)
27292729
.map(|table| table.typ);
@@ -2742,6 +2742,10 @@ impl Compiler {
27422742
// Use grandparent
27432743
parent_idx = stack_size - 3;
27442744
parent = &self.code_stack[parent_idx];
2745+
parent_scope = self
2746+
.symbol_table_stack
2747+
.get(parent_idx)
2748+
.map(|table| table.typ);
27452749
}
27462750

27472751
// Check if this is a global class/function
@@ -2779,9 +2783,12 @@ impl Compiler {
27792783
let parent_obj_name = &parent.metadata.name;
27802784

27812785
// Determine if parent is a function-like scope
2782-
let is_function_parent = parent.flags.contains(bytecode::CodeFlags::OPTIMIZED)
2783-
&& !parent_obj_name.starts_with("<") // Not a special scope like <lambda>, <listcomp>, etc.
2784-
&& parent_obj_name != "<module>"; // Not the module scope
2786+
let is_function_parent = matches!(
2787+
parent_scope,
2788+
Some(
2789+
CompilerScope::Function | CompilerScope::AsyncFunction | CompilerScope::Lambda
2790+
)
2791+
);
27852792

27862793
if is_function_parent {
27872794
// For functions, append .<locals> to parent qualname
@@ -3251,6 +3258,8 @@ impl Compiler {
32513258
CompilerScope::Annotation | CompilerScope::TypeParams
32523259
) {
32533260
SymbolScope::GlobalImplicit
3261+
} else if name.starts_with('_') {
3262+
SymbolScope::Unknown
32543263
} else {
32553264
return Err(self.error(CodegenErrorType::SyntaxError(format!(
32563265
"the symbol '{name}' must be present in the symbol table"
@@ -6613,39 +6622,20 @@ impl Compiler {
66136622
let (doc_str, body) = split_doc(body, &self.opts);
66146623

66156624
// Load __name__ and store as __module__
6616-
let dunder_name = self.name("__name__");
6617-
emit!(self, Instruction::LoadName { namei: dunder_name });
6618-
let dunder_module = self.name("__module__");
6619-
emit!(
6620-
self,
6621-
Instruction::StoreName {
6622-
namei: dunder_module
6623-
}
6624-
);
6625+
self.load_name("__name__")?;
6626+
self.store_name("__module__")?;
66256627

66266628
// Store __qualname__
66276629
self.emit_load_const(ConstantData::Str {
66286630
value: qualname.into(),
66296631
});
6630-
let qualname_name = self.name("__qualname__");
6631-
emit!(
6632-
self,
6633-
Instruction::StoreName {
6634-
namei: qualname_name
6635-
}
6636-
);
6632+
self.store_name("__qualname__")?;
66376633

66386634
// Store __firstlineno__ before __doc__
66396635
self.emit_load_const(ConstantData::Integer {
66406636
value: BigInt::from(firstlineno),
66416637
});
6642-
let firstlineno_name = self.name("__firstlineno__");
6643-
emit!(
6644-
self,
6645-
Instruction::StoreName {
6646-
namei: firstlineno_name
6647-
}
6648-
);
6638+
self.store_name("__firstlineno__")?;
66496639

66506640
// Set __type_params__ from the enclosing type-params closure when
66516641
// compiling a generic class body.
@@ -6677,8 +6667,7 @@ impl Compiler {
66776667
// Store __doc__ only if there's an explicit docstring.
66786668
if let Some(doc) = doc_str {
66796669
self.emit_load_const(ConstantData::Str { value: doc.into() });
6680-
let doc_name = self.name("__doc__");
6681-
emit!(self, Instruction::StoreName { namei: doc_name });
6670+
self.store_name("__doc__")?;
66826671
}
66836672

66846673
// 3. Compile the class body
@@ -6716,13 +6705,7 @@ impl Compiler {
67166705
.collect(),
67176706
});
67186707
self.set_no_location();
6719-
let static_attrs_name = self.name("__static_attributes__");
6720-
emit!(
6721-
self,
6722-
Instruction::StoreName {
6723-
namei: static_attrs_name
6724-
}
6725-
);
6708+
self.store_name("__static_attributes__")?;
67266709
self.set_no_location();
67276710
}
67286711

@@ -6731,13 +6714,7 @@ impl Compiler {
67316714
let classdict_idx = u32::from(self.get_cell_var_index("__classdict__"));
67326715
emit!(self, PseudoInstruction::LoadClosure { i: classdict_idx });
67336716
self.set_no_location();
6734-
let classdictcell = self.name("__classdictcell__");
6735-
emit!(
6736-
self,
6737-
Instruction::StoreName {
6738-
namei: classdictcell
6739-
}
6740-
);
6717+
self.store_name("__classdictcell__")?;
67416718
self.set_no_location();
67426719
}
67436720

@@ -6751,8 +6728,7 @@ impl Compiler {
67516728
self.set_no_location();
67526729
emit!(self, Instruction::Copy { i: 1 });
67536730
self.set_no_location();
6754-
let classcell = self.name("__classcell__");
6755-
emit!(self, Instruction::StoreName { namei: classcell });
6731+
self.store_name("__classcell__")?;
67566732
self.set_no_location();
67576733
} else {
67586734
self.emit_load_const(ConstantData::None);
@@ -20934,6 +20910,86 @@ class C:
2093420910
}
2093520911
}
2093620912

20913+
#[test]
20914+
fn test_class_firstlineno_store_uses_name_resolution() {
20915+
let code = compile_exec(
20916+
"\
20917+
def f():
20918+
__firstlineno__ = 1
20919+
class C:
20920+
nonlocal __firstlineno__
20921+
return C
20922+
",
20923+
);
20924+
let class_code = find_code(&code, "C").expect("missing class code");
20925+
20926+
assert!(
20927+
class_code
20928+
.freevars
20929+
.iter()
20930+
.any(|name| name == "__firstlineno__"),
20931+
"class should close over nonlocal __firstlineno__, got freevars={:?}",
20932+
class_code.freevars
20933+
);
20934+
assert!(
20935+
class_code
20936+
.instructions
20937+
.iter()
20938+
.any(|unit| matches!(unit.op, Instruction::StoreDeref { .. })),
20939+
"CPython routes __firstlineno__ through name resolution and emits STORE_DEREF, got ops={:?}",
20940+
class_code.instructions
20941+
);
20942+
assert!(
20943+
!class_code.instructions.iter().any(|unit| {
20944+
matches!(
20945+
unit.op,
20946+
Instruction::StoreName { namei }
20947+
if class_code.names
20948+
[namei.get(OpArg::new(u32::from(u8::from(unit.arg)))) as usize]
20949+
.as_str()
20950+
== "__firstlineno__"
20951+
)
20952+
}),
20953+
"nonlocal __firstlineno__ should not be stored with STORE_NAME, got ops={:?}",
20954+
class_code.instructions
20955+
);
20956+
}
20957+
20958+
#[test]
20959+
fn test_lambda_parent_qualname_includes_locals() {
20960+
let code = compile_exec(
20961+
"\
20962+
def f():
20963+
return lambda: (lambda: None)
20964+
",
20965+
);
20966+
let mut lambda_qualnames = Vec::new();
20967+
fn collect_lambda_qualnames(code: &CodeObject, out: &mut Vec<String>) {
20968+
if code.obj_name == "<lambda>" {
20969+
out.push(code.qualname.to_string());
20970+
}
20971+
for constant in code.constants.iter() {
20972+
if let ConstantData::Code { code } = constant {
20973+
collect_lambda_qualnames(code, out);
20974+
}
20975+
}
20976+
}
20977+
collect_lambda_qualnames(&code, &mut lambda_qualnames);
20978+
20979+
assert!(
20980+
lambda_qualnames
20981+
.iter()
20982+
.any(|name| name == "f.<locals>.<lambda>"),
20983+
"missing outer lambda qualname, got {lambda_qualnames:?}"
20984+
);
20985+
assert!(
20986+
lambda_qualnames
20987+
.iter()
20988+
.any(|name| name == "f.<locals>.<lambda>.<locals>.<lambda>"),
20989+
"nested lambda parent should include .<locals> like CPython, got {lambda_qualnames:?}"
20990+
);
20991+
}
20992+
2093720993
#[test]
2093820994
fn test_future_annotations_class_uses_direct_annotation_store() {
2093920995
let code = compile_exec(

0 commit comments

Comments
 (0)