From 2d2fb620ca96b6d3071de57314bbefee429d8e32 Mon Sep 17 00:00:00 2001 From: tanisha Date: Sat, 28 Feb 2026 21:43:53 +0530 Subject: [PATCH] Added popitem() --- src/public/PyDict.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/public/PyDict.c b/src/public/PyDict.c index 1e65e13b..c0e47b8b 100644 --- a/src/public/PyDict.c +++ b/src/public/PyDict.c @@ -568,7 +568,22 @@ static bool dict_pop(int argc, py_Ref argv) { if(res == 0) py_assign(py_retval(), default_val); return true; } - +static bool dict_popitem(int argc, py_Ref argv) { + PY_CHECK_ARGC(1); + Dict* self = py_touserdata(argv); + if(self->length == 0) return KeyError(py_None()); + for(int i = self->entries.length - 1; i >= 0; i--) { + DictEntry* entry = c11__at(DictEntry, &self->entries, i); + if(py_isnil(&entry->key)) continue; + py_Ref p = py_newtuple(py_retval(), 2); + p[0] = entry->key; + p[1] = entry->val; + py_TValue key_copy = entry->key; + Dict__pop(self, &key_copy); + return true; + } + return KeyError(py_None()); +} static bool dict_keys(int argc, py_Ref argv) { PY_CHECK_ARGC(1); Dict* self = py_touserdata(argv); @@ -616,6 +631,7 @@ py_Type pk_dict__register() { py_bindmethod(type, "update", dict_update); py_bindmethod(type, "get", dict_get); py_bindmethod(type, "pop", dict_pop); + py_bindmethod(type, "popitem", dict_popitem); py_bindmethod(type, "keys", dict_keys); py_bindmethod(type, "values", dict_values); py_bindmethod(type, "items", dict_items);