Skip to content

Commit ceac014

Browse files
committed
Add dict.popitem.
1 parent 1081ea0 commit ceac014

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

tests/snippets/dict.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,9 @@ def __eq__(self, other):
174174

175175
with assertRaises(KeyError):
176176
x.pop("not here")
177+
178+
x = {1: 'a'}
179+
assert (1, 'a') == x.popitem()
180+
with assertRaises(KeyError):
181+
x.popitem()
182+
assert x == {}

vm/src/obj/objdict.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,19 @@ impl PyDictRef {
232232
self.entries.borrow_mut().pop(vm, &key)
233233
}
234234

235+
fn popitem(self, vm: &VirtualMachine) -> PyResult {
236+
let mut entries = self.entries.borrow_mut();
237+
let (key, value) = match entries.next_entry(&mut 0) {
238+
Some((key, value)) => (key.clone(), value.clone()),
239+
None => {
240+
return Err(vm.new_key_error("popitem(): dictionary is empty".to_string()));
241+
}
242+
};
243+
244+
entries.delete(vm, &key)?;
245+
Ok(vm.ctx.new_tuple(vec![key, value]))
246+
}
247+
235248
/// Take a python dictionary and convert it to attributes.
236249
pub fn to_attributes(self) -> PyAttributes {
237250
let mut attrs = PyAttributes::new();
@@ -463,6 +476,7 @@ pub fn init(context: &PyContext) {
463476
"copy" => context.new_rustfunc(PyDictRef::copy),
464477
"update" => context.new_rustfunc(PyDictRef::update),
465478
"pop" => context.new_rustfunc(PyDictRef::pop),
479+
"popitem" => context.new_rustfunc(PyDictRef::popitem),
466480
});
467481

468482
PyDictKeys::extend_class(context, &context.dictkeys_type);

0 commit comments

Comments
 (0)