diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index baf4aa56..ea8e5d4e 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -543,7 +543,11 @@ PK_EXPORT bool py_dict_setitem(py_Ref self, py_Ref key, py_Ref val) PY_RAISE; /// -1: error, 0: not found, 1: found (and deleted) PK_EXPORT int py_dict_delitem(py_Ref self, py_Ref key) PY_RAISE; /// -1: error, 0: not found, 1: found -PK_EXPORT int py_dict_contains(py_Ref self, py_Ref key) PY_RAISE; +PK_EXPORT int py_dict_getitem_by_str(py_Ref self, const char* key) PY_RAISE PY_RETURN; +/// true: success, false: error +PK_EXPORT bool py_dict_setitem_by_str(py_Ref self, const char* key, py_Ref val) PY_RAISE; +/// -1: error, 0: not found, 1: found (and deleted) +PK_EXPORT int py_dict_delitem_by_str(py_Ref self, const char* key) PY_RAISE; /// true: success, false: error PK_EXPORT bool py_dict_apply(py_Ref self, bool (*f)(py_Ref key, py_Ref val, void* ctx), void* ctx) PY_RAISE; diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 704edba8..533d3820 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -392,11 +392,9 @@ static bool co->name->data); } else { // add to **kwargs - py_Ref tmp = py_pushtmp(); - c11_sv key_sv = py_name2sv(key); - py_newstrn(tmp, key_sv.data, key_sv.size); - bool ok = py_dict_setitem(&buffer[decl->starred_kwarg], tmp, &p1[2 * j + 1]); - py_pop(); + bool ok = py_dict_setitem_by_str(&buffer[decl->starred_kwarg], + py_name2str(key), + &p1[2 * j + 1]); if(!ok) return false; } } diff --git a/src/public/py_dict.c b/src/public/py_dict.c index 1c6ab3e4..62d7cf12 100644 --- a/src/public/py_dict.c +++ b/src/public/py_dict.c @@ -521,25 +521,40 @@ int py_dict_getitem(py_Ref self, py_Ref key) { return 0; } -int py_dict_delitem(py_Ref self, py_Ref key) { - assert(py_isdict(self)); - Dict* ud = py_touserdata(self); - return Dict__pop(ud, key); -} - bool py_dict_setitem(py_Ref self, py_Ref key, py_Ref val) { assert(py_isdict(self)); Dict* ud = py_touserdata(self); return Dict__set(ud, key, val); } -int py_dict_contains(py_Ref self, py_Ref key) { +int py_dict_delitem(py_Ref self, py_Ref key) { assert(py_isdict(self)); Dict* ud = py_touserdata(self); - DictEntry* entry; - bool ok = Dict__try_get(ud, key, &entry); - if(!ok) return -1; - return entry ? 1 : 0; + return Dict__pop(ud, key); +} + +int py_dict_getitem_by_str(py_Ref self, const char *key){ + py_Ref tmp = py_pushtmp(); + py_newstr(tmp, key); + int res = py_dict_getitem(self, tmp); + py_pop(); + return res; +} + +bool py_dict_setitem_by_str(py_Ref self, const char *key, py_Ref val){ + py_Ref tmp = py_pushtmp(); + py_newstr(tmp, key); + bool res = py_dict_setitem(self, tmp, val); + py_pop(); + return res; +} + +int py_dict_delitem_by_str(py_Ref self, const char *key){ + py_Ref tmp = py_pushtmp(); + py_newstr(tmp, key); + int res = py_dict_delitem(self, tmp); + py_pop(); + return res; } int py_dict_len(py_Ref self) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt deleted file mode 100644 index a44dff55..00000000 --- a/tests/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -# @szdytom favored testing, set BUILD_TESTING to enable it -# You can use scripts/run_tests.py as an alternative -# Note: the CI uses scripts/run_tests.py to run the tests - -cmake_minimum_required(VERSION 3.10) - -function(pkpy_add_test pyfile) - get_filename_component(test_name ${pyfile} NAME_WE) - add_test( - NAME ${test_name} - COMMAND $ ${pyfile} - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/.. - ) -endfunction() - -message("Testing enabled") - -file(GLOB PK_PYTHON_TESTCASES_FILES RELATIVE ${CMAKE_CURRENT_LIST_DIR}/.. "*.py") - -foreach(pyfile ${PK_PYTHON_TESTCASES_FILES}) - pkpy_add_test(${pyfile}) -endforeach()