Skip to content

Commit 28034cd

Browse files
committed
codeobj.qualname
1 parent c195473 commit 28034cd

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

compiler/codegen/src/compile.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ impl<'src> Compiler<'src> {
310310
kwonlyarg_count: 0,
311311
source_path: source_code.path.to_owned(),
312312
first_line_number: OneIndexed::MIN,
313-
obj_name: code_name,
314-
313+
obj_name: code_name.clone(),
314+
qualname: Some(code_name),
315315
blocks: vec![ir::Block::default()],
316316
current_block: ir::BlockIdx(0),
317317
constants: IndexSet::default(),
@@ -402,6 +402,13 @@ impl Compiler<'_> {
402402
.map(|(var, _)| var.clone())
403403
.collect();
404404

405+
// Calculate qualname based on the current qualified path
406+
let qualname = if self.qualified_path.is_empty() {
407+
Some(obj_name.clone())
408+
} else {
409+
Some(self.qualified_path.join("."))
410+
};
411+
405412
let info = ir::CodeInfo {
406413
flags,
407414
posonlyarg_count,
@@ -410,6 +417,7 @@ impl Compiler<'_> {
410417
source_path,
411418
first_line_number,
412419
obj_name,
420+
qualname,
413421

414422
blocks: vec![ir::Block::default()],
415423
current_block: ir::BlockIdx(0),
@@ -1496,6 +1504,10 @@ impl Compiler<'_> {
14961504

14971505
self.push_qualified_path(name);
14981506
let qualified_name = self.qualified_path.join(".");
1507+
1508+
// Update the qualname in the current code info
1509+
self.code_stack.last_mut().unwrap().qualname = Some(qualified_name.clone());
1510+
14991511
self.push_qualified_path("<locals>");
15001512

15011513
let (doc_str, body) = split_doc(body, &self.opts);
@@ -1720,6 +1732,9 @@ impl Compiler<'_> {
17201732

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

1735+
// Update the qualname in the current code info
1736+
self.code_stack.last_mut().unwrap().qualname = Some(qualified_name.clone());
1737+
17231738
let (doc_str, body) = split_doc(body, &self.opts);
17241739

17251740
let dunder_name = self.name("__name__");
@@ -3495,6 +3510,9 @@ impl Compiler<'_> {
34953510
let mut func_flags = self
34963511
.enter_function(&name, parameters.as_deref().unwrap_or(&Default::default()))?;
34973512

3513+
// Lambda qualname should be <lambda>
3514+
self.code_stack.last_mut().unwrap().qualname = Some(name.clone());
3515+
34983516
self.ctx = CompileContext {
34993517
loop_data: Option::None,
35003518
in_class: prev_ctx.in_class,
@@ -3956,6 +3974,10 @@ impl Compiler<'_> {
39563974

39573975
// Create magnificent function <listcomp>:
39583976
self.push_output(flags, 1, 1, 0, name.to_owned());
3977+
3978+
// Set qualname for comprehension
3979+
self.code_stack.last_mut().unwrap().qualname = Some(name.to_owned());
3980+
39593981
let arg0 = self.varname(".0")?;
39603982

39613983
let return_none = init_collection.is_none();

compiler/codegen/src/ir.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub struct CodeInfo {
7373
pub source_path: String,
7474
pub first_line_number: OneIndexed,
7575
pub obj_name: String, // Name of the object that created this code object
76+
pub qualname: Option<String>, // Qualified name of the object
7677

7778
pub blocks: Vec<Block>,
7879
pub current_block: BlockIdx,
@@ -99,6 +100,7 @@ impl CodeInfo {
99100
source_path,
100101
first_line_number,
101102
obj_name,
103+
qualname,
102104

103105
mut blocks,
104106
current_block: _,
@@ -162,7 +164,8 @@ impl CodeInfo {
162164
kwonlyarg_count,
163165
source_path,
164166
first_line_number: Some(first_line_number),
165-
obj_name,
167+
obj_name: obj_name.clone(),
168+
qualname: qualname.unwrap_or(obj_name),
166169

167170
max_stackdepth,
168171
instructions: instructions.into_boxed_slice(),

compiler/core/src/bytecode.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ pub struct CodeObject<C: Constant = ConstantData> {
115115
pub max_stackdepth: u32,
116116
pub obj_name: C::Name,
117117
// Name of the object that created this code object
118+
pub qualname: C::Name,
119+
// Qualified name of the object (like CPython's co_qualname)
118120
pub cell2arg: Option<Box<[i32]>>,
119121
pub constants: Box<[C]>,
120122
pub names: Box<[C::Name]>,
@@ -1140,6 +1142,7 @@ impl<C: Constant> CodeObject<C> {
11401142
freevars: map_names(self.freevars),
11411143
source_path: bag.make_name(self.source_path.as_ref()),
11421144
obj_name: bag.make_name(self.obj_name.as_ref()),
1145+
qualname: bag.make_name(self.qualname.as_ref()),
11431146

11441147
instructions: self.instructions,
11451148
locations: self.locations,
@@ -1169,6 +1172,7 @@ impl<C: Constant> CodeObject<C> {
11691172
freevars: map_names(&self.freevars),
11701173
source_path: bag.make_name(self.source_path.as_ref()),
11711174
obj_name: bag.make_name(self.obj_name.as_ref()),
1175+
qualname: bag.make_name(self.qualname.as_ref()),
11721176

11731177
instructions: self.instructions.clone(),
11741178
locations: self.locations.clone(),

compiler/core/src/marshal.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ pub fn deserialize_code<R: Read, Bag: ConstantBag>(
210210
let len = rdr.read_u32()?;
211211
let obj_name = bag.make_name(rdr.read_str(len)?);
212212

213+
let len = rdr.read_u32()?;
214+
let qualname = bag.make_name(rdr.read_str(len)?);
215+
213216
let len = rdr.read_u32()?;
214217
let cell2arg = (len != 0)
215218
.then(|| {
@@ -250,6 +253,7 @@ pub fn deserialize_code<R: Read, Bag: ConstantBag>(
250253
first_line_number,
251254
max_stackdepth,
252255
obj_name,
256+
qualname,
253257
cell2arg,
254258
constants,
255259
names,
@@ -609,6 +613,7 @@ pub fn serialize_code<W: Write, C: Constant>(buf: &mut W, code: &CodeObject<C>)
609613
buf.write_u32(code.max_stackdepth);
610614

611615
write_vec(buf, code.obj_name.as_ref().as_bytes());
616+
write_vec(buf, code.qualname.as_ref().as_bytes());
612617

613618
let cell2arg = code.cell2arg.as_deref().unwrap_or(&[]);
614619
write_len(buf, cell2arg.len());

vm/src/builtins/code.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ impl PyCode {
298298
fn co_name(&self) -> PyStrRef {
299299
self.code.obj_name.to_owned()
300300
}
301+
#[pygetset]
302+
fn co_qualname(&self) -> PyStrRef {
303+
self.code.qualname.to_owned()
304+
}
301305

302306
#[pygetset]
303307
fn co_names(&self, vm: &VirtualMachine) -> PyTupleRef {
@@ -401,6 +405,7 @@ impl PyCode {
401405
source_path: source_path.as_object().as_interned_str(vm).unwrap(),
402406
first_line_number,
403407
obj_name: obj_name.as_object().as_interned_str(vm).unwrap(),
408+
qualname: self.code.qualname,
404409

405410
max_stackdepth: self.code.max_stackdepth,
406411
instructions: self.code.instructions.clone(),

0 commit comments

Comments
 (0)