From 9179c31d6a8703a2f9e5f93c14903450ff36f6dd Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 2 Dec 2023 16:22:08 +0800 Subject: [PATCH] some change about `mappingproxy` --- include/pocketpy/common.h | 2 +- src/pocketpy.cpp | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 6e25e533..dd178c47 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -23,7 +23,7 @@ #include #include -#define PK_VERSION "1.3.1" +#define PK_VERSION "1.3.2" #include "config.h" #include "export.h" diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 8eddd471..fa7bdc4b 100644 --- a/src/pocketpy.cpp +++ b/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; });