mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-19 19:10:17 +00:00
add dict.pop
This commit is contained in:
parent
274f74818f
commit
940159e1ab
@ -117,3 +117,5 @@ class defaultdict:
|
||||
def items(self):
|
||||
return self._a.items()
|
||||
|
||||
def pop(self, key):
|
||||
return self._a.pop(key)
|
||||
|
@ -898,6 +898,14 @@ inline void init_builtins(VM* _vm) {
|
||||
self.erase(key);
|
||||
});
|
||||
|
||||
_vm->bind_method<1>("dict", "pop", [](VM* vm, ArgsView args) {
|
||||
Dict& self = _CAST(Dict&, args[0]);
|
||||
PyObject* value = self.try_get(args[1]);
|
||||
if(value == nullptr) vm->KeyError(args[1]);
|
||||
self.erase(args[1]);
|
||||
return value;
|
||||
});
|
||||
|
||||
_vm->bind__contains__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* key) {
|
||||
Dict& self = _CAST(Dict&, obj);
|
||||
return self.contains(key);
|
||||
|
@ -47,4 +47,9 @@ assert d1 == d2
|
||||
assert d1 != d3
|
||||
|
||||
a = dict([(1, 2), (3, 4)])
|
||||
assert a == {1: 2, 3: 4}
|
||||
assert a == {1: 2, 3: 4}
|
||||
|
||||
assert a.pop(1) == 2
|
||||
assert a == {3: 4}
|
||||
assert a.pop(3) == 4
|
||||
assert a == {}
|
Loading…
x
Reference in New Issue
Block a user