Skip to content

[pull] main from RustPython:main #539

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 2 commits into from
Aug 9, 2025
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: 1 addition & 1 deletion compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4398,7 +4398,7 @@ impl Compiler {

for item in unpacked {
self.compile_expression(&item.value)?;
emit!(self, Instruction::DictUpdate);
emit!(self, Instruction::DictUpdate { index: 1 });
}

Ok(())
Expand Down
8 changes: 5 additions & 3 deletions compiler/core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,9 @@ pub enum Instruction {
BuildMapForCall {
size: Arg<u32>,
},
DictUpdate,
DictUpdate {
index: Arg<u32>,
},
BuildSlice {
/// whether build a slice with a third step argument
step: Arg<bool>,
Expand Down Expand Up @@ -1397,7 +1399,7 @@ impl Instruction {
let nargs = size.get(arg);
-(nargs as i32) + 1
}
DictUpdate => -1,
DictUpdate { .. } => -1,
BuildSlice { step } => -2 - (step.get(arg) as i32) + 1,
ListAppend { .. } | SetAdd { .. } => -1,
MapAdd { .. } => -2,
Expand Down Expand Up @@ -1586,7 +1588,7 @@ impl Instruction {
BuildSetFromTuples { size } => w!(BuildSetFromTuples, size),
BuildMap { size } => w!(BuildMap, size),
BuildMapForCall { size } => w!(BuildMapForCall, size),
DictUpdate => w!(DictUpdate),
DictUpdate { index } => w!(DictUpdate, index),
BuildSlice { step } => w!(BuildSlice, step),
ListAppend { i } => w!(ListAppend, i),
SetAdd { i } => w!(SetAdd, i),
Expand Down
32 changes: 23 additions & 9 deletions vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,26 +800,40 @@ impl ExecutingFrame<'_> {
bytecode::Instruction::BuildMapForCall { size } => {
self.execute_build_map_for_call(vm, size.get(arg))
}
bytecode::Instruction::DictUpdate => {
let other = self.pop_value();
let dict = self
.top_value()
.downcast_ref::<PyDict>()
.expect("exact dict expected");
bytecode::Instruction::DictUpdate { index } => {
// Stack before: [..., dict, ..., source] (source at TOS)
// Stack after: [..., dict, ...] (source consumed)
// The dict to update is at position TOS-i (before popping source)

let idx = index.get(arg);

// Pop the source from TOS
let source = self.pop_value();

// Get the dict to update (it's now at TOS-(i-1) after popping source)
let dict = if idx <= 1 {
// DICT_UPDATE 0 or 1: dict is at TOS (after popping source)
self.top_value()
} else {
// DICT_UPDATE n: dict is at TOS-(n-1)
self.nth_value(idx - 1)
};

let dict = dict.downcast_ref::<PyDict>().expect("exact dict expected");

// For dictionary unpacking {**x}, x must be a mapping
// Check if the object has the mapping protocol (keys method)
if vm
.get_method(other.clone(), vm.ctx.intern_str("keys"))
.get_method(source.clone(), vm.ctx.intern_str("keys"))
.is_none()
{
return Err(vm.new_type_error(format!(
"'{}' object is not a mapping",
other.class().name()
source.class().name()
)));
}

dict.merge_object(other, vm)?;
dict.merge_object(source, vm)?;
Ok(None)
}
bytecode::Instruction::BuildSlice { step } => {
Expand Down
Loading