blueloveTH 2 недель назад
Родитель
Сommit
5681326b82
2 измененных файлов с 20 добавлено и 1 удалено
  1. 19 0
      src/public/PyTuple.c
  2. 1 1
      tests/060_tuple.py

+ 19 - 0
src/public/PyTuple.c

@@ -24,6 +24,24 @@ void py_tuple_setitem(py_Ref self, int i, py_Ref val) { py_setslot(self, i, val)
 int py_tuple_len(py_Ref self) { return self->_obj->slots; }
 
 //////////////
+static bool tuple__add__(int argc, py_Ref argv) {
+    PY_CHECK_ARGC(2);
+    if(!py_istype(py_arg(0), tp_tuple) || !py_istype(py_arg(1), tp_tuple)) {
+        py_newnotimplemented(py_retval());
+        return true;
+    }
+    int lhs_length = py_tuple_len(py_arg(0));
+    int rhs_length = py_tuple_len(py_arg(1));
+    py_Ref p = py_newtuple(py_retval(), lhs_length + rhs_length);
+    for(int i = 0; i < lhs_length; i++) {
+        p[i] = *py_tuple_getitem(py_arg(0), i);
+    }
+    for(int i = 0; i < rhs_length; i++) {
+        p[lhs_length + i] = *py_tuple_getitem(py_arg(1), i);
+    }
+    return true;
+}
+
 static bool tuple__len__(int argc, py_Ref argv) {
     py_newint(py_retval(), py_tuple_len(argv));
     return true;
@@ -176,6 +194,7 @@ static bool tuple__hash__(int argc, py_Ref argv) {
 py_Type pk_tuple__register() {
     py_Type type = pk_newtype("tuple", tp_object, NULL, NULL, false, true);
 
+    py_bindmagic(type, __add__, tuple__add__);
     py_bindmagic(type, __len__, tuple__len__);
     py_bindmagic(type, __repr__, tuple__repr__);
     py_bindmagic(type, __new__, tuple__new__);

+ 1 - 1
tests/060_tuple.py

@@ -51,4 +51,4 @@ assert (1,2) < (1,2,3)
 assert (1,2) < (1,2,1)
 assert (1,2) < (1,2,2)
 
-
+assert (1, 2) + (3, 4) == (1, 2, 3, 4)