]> granicus.if.org Git - python/commitdiff
Add _PyStack_AsTupleSlice() helper
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 16 Jan 2017 22:50:53 +0000 (23:50 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 16 Jan 2017 22:50:53 +0000 (23:50 +0100)
Include/abstract.h
Objects/abstract.c

index 3ca283aa12a92ee52575dc0fdca30ac88a796779..d96e8a542477eefa550249b33935e144921f97c1 100644 (file)
@@ -160,6 +160,12 @@ PyAPI_FUNC(PyObject*) _PyStack_AsTuple(
     PyObject **stack,
     Py_ssize_t nargs);
 
+PyAPI_FUNC(PyObject*) _PyStack_AsTupleSlice(
+    PyObject **stack,
+    Py_ssize_t nargs,
+    Py_ssize_t start,
+    Py_ssize_t end);
+
 /* Convert keyword arguments from the (stack, kwnames) format to a Python
    dictionary.
 
index 5726160c08b4eeca9dfcb68141408343a107a1c4..bba946e8ef68436c967d07b944c7a28a7e5f6a25 100644 (file)
@@ -2274,7 +2274,30 @@ _PyStack_AsTuple(PyObject **stack, Py_ssize_t nargs)
         Py_INCREF(item);
         PyTuple_SET_ITEM(args, i, item);
     }
+    return args;
+}
+
+PyObject*
+_PyStack_AsTupleSlice(PyObject **stack, Py_ssize_t nargs,
+                      Py_ssize_t start, Py_ssize_t end)
+{
+    PyObject *args;
+    Py_ssize_t i;
+
+    assert(0 <= start);
+    assert(end <= nargs);
+    assert(start <= end);
 
+    args = PyTuple_New(end - start);
+    if (args == NULL) {
+        return NULL;
+    }
+
+    for (i=start; i < end; i++) {
+        PyObject *item = stack[i];
+        Py_INCREF(item);
+        PyTuple_SET_ITEM(args, i - start, item);
+    }
     return args;
 }