blueloveTH il y a 1 an
Parent
commit
5a783d81f5
4 fichiers modifiés avec 36 ajouts et 1 suppressions
  1. 8 0
      include/pocketpy/pocketpy.h
  2. 9 1
      src/modules/pickle.c
  3. 18 0
      src/public/py_dict.c
  4. 1 0
      tests/90_pickle.py

+ 8 - 0
include/pocketpy/pocketpy.h

@@ -610,12 +610,20 @@ PK_API int py_dict_getitem(py_Ref self, py_Ref key) PY_RAISE PY_RETURN;
 PK_API 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_API int py_dict_delitem(py_Ref self, py_Ref key) PY_RAISE;
+
 /// -1: error, 0: not found, 1: found
 PK_API int py_dict_getitem_by_str(py_Ref self, const char* key) PY_RAISE PY_RETURN;
+/// -1: error, 0: not found, 1: found
+PK_API int py_dict_getitem_by_int(py_Ref self, py_i64 key) PY_RAISE PY_RETURN;
 /// true: success, false: error
 PK_API bool py_dict_setitem_by_str(py_Ref self, const char* key, py_Ref val) PY_RAISE;
+/// true: success, false: error
+PK_API bool py_dict_setitem_by_int(py_Ref self, py_i64 key, py_Ref val) PY_RAISE;
 /// -1: error, 0: not found, 1: found (and deleted)
 PK_API int py_dict_delitem_by_str(py_Ref self, const char* key) PY_RAISE;
+/// -1: error, 0: not found, 1: found (and deleted)
+PK_API int py_dict_delitem_by_int(py_Ref self, py_i64 key) PY_RAISE;
+
 /// true: success, false: error
 PK_API bool
     py_dict_apply(py_Ref self, bool (*f)(py_Ref key, py_Ref val, void* ctx), void* ctx) PY_RAISE;

+ 9 - 1
src/modules/pickle.c

@@ -8,7 +8,7 @@
 
 typedef enum {
     // clang-format off
-    PKL_NONE,
+    PKL_NONE, PKL_ELLIPSIS,
     PKL_INT8, PKL_INT16, PKL_INT32, PKL_INT64,
     PKL_FLOAT32, PKL_FLOAT64,
     PKL_TRUE, PKL_FALSE,
@@ -149,6 +149,10 @@ static bool pickle__write_object(PickleObject* buf, py_TValue* obj) {
             pkl__emit_op(buf, PKL_NONE);
             return true;
         }
+        case tp_ellipsis: {
+            pkl__emit_op(buf, PKL_ELLIPSIS);
+            return true;
+        }
         case tp_int: {
             py_i64 val = obj->_i64;
             pkl__emit_int(buf, val);
@@ -269,6 +273,10 @@ bool py_pickle_loads(const unsigned char* data, int size) {
                 py_pushnone();
                 break;
             }
+            case PKL_ELLIPSIS: {
+                py_newellipsis(py_pushtmp());
+                break;
+            }
             case PKL_INT8: {
                 int8_t val;
                 UNALIGNED_READ(&val, p);

+ 18 - 0
src/public/py_dict.c

@@ -632,6 +632,24 @@ int py_dict_delitem_by_str(py_Ref self, const char* key) {
     return res;
 }
 
+int py_dict_getitem_by_int(py_Ref self, py_i64 key) {
+    py_TValue tmp;
+    py_newint(&tmp, key);
+    return py_dict_getitem(self, &tmp);
+}
+
+bool py_dict_setitem_by_int(py_Ref self, py_i64 key, py_Ref val) {
+    py_TValue tmp;
+    py_newint(&tmp, key);
+    return py_dict_setitem(self, &tmp, val);
+}
+
+int py_dict_delitem_by_int(py_Ref self, py_i64 key) {
+    py_TValue tmp;
+    py_newint(&tmp, key);
+    return py_dict_delitem(self, &tmp);
+}
+
 int py_dict_len(py_Ref self) {
     assert(py_isdict(self));
     Dict* ud = py_touserdata(self);

+ 1 - 0
tests/90_pickle.py

@@ -9,6 +9,7 @@ def test(data): # type: ignore
     assert data == o
 
 test(None)                      # PKL_NONE
+test(...)                       # PKL_ELLIPSIS
 test(1)                         # PKL_INT8
 test(277)                       # PKL_INT16
 test(-66666)                    # PKL_INT32