patch 9.1.1251: if_python: build error with tuples and dynamic python

Problem:  if_python: build error with tuples and dynamic python
          (after v9.1.1239)
Solution: Fix build error and test failures (Yee Cheng Cin)

closes: #16992

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Yee Cheng Chin <ychin.git@gmail.com>
Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/if_py_both.h b/src/if_py_both.h
index 641c159..ed66254 100644
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -1097,12 +1097,6 @@
 	if (ret == NULL)
 	    return NULL;
 
-	if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
-	{
-	    Py_DECREF(ret);
-	    return NULL;
-	}
-
 	for (int idx = 0; idx < len; idx++)
 	{
 	    typval_T	*item_tv = TUPLE_ITEM(tuple, idx);
@@ -1113,7 +1107,13 @@
 		Py_DECREF(ret);
 		return NULL;
 	    }
-	    PyTuple_SET_ITEM(ret, idx, newObj);
+	    PyTuple_SetItem(ret, idx, newObj);
+	}
+
+	if (PyDict_SetItemString(lookup_dict, ptrBuf, ret))
+	{
+	    Py_DECREF(ret);
+	    return NULL;
 	}
     }
     else if (our_tv->v_type == VAR_DICT)
@@ -3440,7 +3440,7 @@
 	    return NULL;
 	}
 
-	PyTuple_SET_ITEM(tuple, i, item);
+	PyTuple_SetItem(tuple, i, item);
     }
 
     return tuple;