@@ -232,6 +232,19 @@ impl PyDictRef {
232
232
self . entries . borrow_mut ( ) . pop ( vm, & key)
233
233
}
234
234
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
+
235
248
/// Take a python dictionary and convert it to attributes.
236
249
pub fn to_attributes ( self ) -> PyAttributes {
237
250
let mut attrs = PyAttributes :: new ( ) ;
@@ -463,6 +476,7 @@ pub fn init(context: &PyContext) {
463
476
"copy" => context. new_rustfunc( PyDictRef :: copy) ,
464
477
"update" => context. new_rustfunc( PyDictRef :: update) ,
465
478
"pop" => context. new_rustfunc( PyDictRef :: pop) ,
479
+ "popitem" => context. new_rustfunc( PyDictRef :: popitem) ,
466
480
} ) ;
467
481
468
482
PyDictKeys :: extend_class ( context, & context. dictkeys_type ) ;
0 commit comments