From de7c1dc94b30c399610b149c019068dbbd6dff30 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 25 Jun 2023 16:08:10 +0800 Subject: [PATCH] ... --- python/collections.py | 6 ++++++ src/pocketpy.h | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/python/collections.py b/python/collections.py index a962f053..1c95d1c7 100644 --- a/python/collections.py +++ b/python/collections.py @@ -106,6 +106,12 @@ class defaultdict: return False return self._a == __o._a + def __iter__(self): + return iter(self._a) + + def __contains__(self, key): + return key in self._a + def __len__(self): return len(self._a) diff --git a/src/pocketpy.h b/src/pocketpy.h index 0a555746..81f53fdc 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -1021,6 +1021,14 @@ inline void init_builtins(VM* _vm) { return VAR(self.contains(key)); }); + _vm->bind__iter__(_vm->tp_dict, [](VM* vm, PyObject* obj) { + Dict& self = _CAST(Dict&, obj); + auto items = self.items(); + Tuple t(items.size()); + for(int i=0; ipy_iter(VAR(std::move(t))); + }); + _vm->bind_method<-1>("dict", "get", [](VM* vm, ArgsView args) { Dict& self = _CAST(Dict&, args[0]); if(args.size() == 1+1){