blueloveTH пре 1 година
родитељ
комит
2f14689e2f
4 измењених фајлова са 34 додато и 39 уклоњено
  1. 5 1
      include/pocketpy/pocketpy.h
  2. 3 5
      src/interpreter/vm.c
  3. 26 11
      src/public/py_dict.c
  4. 0 22
      tests/CMakeLists.txt

+ 5 - 1
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;

+ 3 - 5
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;
             }
         }

+ 26 - 11
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) {

+ 0 - 22
tests/CMakeLists.txt

@@ -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 $<TARGET_FILE:main> ${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()