From 69de71b2557509d6b3f914a36596fee833d0594b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 16 Jan 2017 23:50:53 +0100 Subject: Add _PyStack_AsTupleSlice() helper --- Include/abstract.h | 6 ++++++ Objects/abstract.c | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/Include/abstract.h b/Include/abstract.h index 3ca283a..d96e8a5 100644 --- a/Include/abstract.h +++ b/Include/abstract.h @@ -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. diff --git a/Objects/abstract.c b/Objects/abstract.c index 5726160..bba946e 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -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; } -- cgit v0.12