Browse Source

some change about `mappingproxy`

blueloveTH 2 years ago
parent
commit
9179c31d6a
2 changed files with 10 additions and 2 deletions
  1. 1 1
      include/pocketpy/common.h
  2. 9 1
      src/pocketpy.cpp

+ 1 - 1
include/pocketpy/common.h

@@ -23,7 +23,7 @@
 #include <bitset>
 #include <deque>
 
-#define PK_VERSION				"1.3.1"
+#define PK_VERSION				"1.3.2"
 
 #include "config.h"
 #include "export.h"

+ 9 - 1
src/pocketpy.cpp

@@ -1050,8 +1050,16 @@ void init_builtins(VM* _vm) {
     _vm->bind__getitem__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj, PyObject* index) {
         MappingProxy& self = _CAST(MappingProxy&, obj);
         StrName key = CAST(Str&, index);
+        PyObject* ret = self.attr().try_get_likely_found(key);
+        if(ret == nullptr) vm->KeyError(index);
+        return ret;
+    });
+
+    _vm->bind(_vm->_t(_vm->tp_mappingproxy), "get(self, key, default=None)", [](VM* vm, ArgsView args) {
+        MappingProxy& self = _CAST(MappingProxy&, args[0]);
+        StrName key = CAST(Str&, args[1]);
         PyObject* ret = self.attr().try_get(key);
-        if(ret == nullptr) vm->AttributeError(key.sv());
+        if(ret == nullptr) return args[2];
         return ret;
     });