diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-01-17 01:33:55 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2017-01-17 01:33:55 (GMT) |
commit | fe54dd8a08b171c20bafe0759c17464a2642030d (patch) | |
tree | edefe68768c964102c2e249b6536624c65c998b3 /Python/getargs.c | |
parent | 259f0e4437ed30036578aba822560feb531b7735 (diff) | |
download | cpython-fe54dd8a08b171c20bafe0759c17464a2642030d.zip cpython-fe54dd8a08b171c20bafe0759c17464a2642030d.tar.gz cpython-fe54dd8a08b171c20bafe0759c17464a2642030d.tar.bz2 |
Add _PyArg_UnpackStack() function helper
Issue #29286.
Diffstat (limited to 'Python/getargs.c')
-rw-r--r-- | Python/getargs.c | 73 |
1 files changed, 55 insertions, 18 deletions
diff --git a/Python/getargs.c b/Python/getargs.c index e3611d8..47b00af 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -2363,21 +2363,16 @@ err: } -int -PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) +static int +PyArg_UnpackStack_impl(PyObject **args, Py_ssize_t l, const char *name, + Py_ssize_t min, Py_ssize_t max, va_list vargs) { - Py_ssize_t i, l; + Py_ssize_t i; PyObject **o; - va_list vargs; assert(min >= 0); assert(min <= max); - if (!PyTuple_Check(args)) { - PyErr_SetString(PyExc_SystemError, - "PyArg_UnpackTuple() argument list is not a tuple"); - return 0; - } - l = PyTuple_GET_SIZE(args); + if (l < min) { if (name != NULL) PyErr_Format( @@ -2392,8 +2387,11 @@ PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t m (min == max ? "" : "at least "), min, l); return 0; } - if (l == 0) + + if (l == 0) { return 1; + } + if (l > max) { if (name != NULL) PyErr_Format( @@ -2409,17 +2407,54 @@ PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t m return 0; } + for (i = 0; i < l; i++) { + o = va_arg(vargs, PyObject **); + *o = args[i]; + } + return 1; +} + +int +PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) +{ + PyObject **stack; + Py_ssize_t nargs; + int retval; + va_list vargs; + + if (!PyTuple_Check(args)) { + PyErr_SetString(PyExc_SystemError, + "PyArg_UnpackTuple() argument list is not a tuple"); + return 0; + } + stack = &PyTuple_GET_ITEM(args, 0); + nargs = PyTuple_GET_SIZE(args); + #ifdef HAVE_STDARG_PROTOTYPES va_start(vargs, max); #else va_start(vargs); #endif - for (i = 0; i < l; i++) { - o = va_arg(vargs, PyObject **); - *o = PyTuple_GET_ITEM(args, i); - } + retval = PyArg_UnpackStack_impl(stack, nargs, name, min, max, vargs); va_end(vargs); - return 1; + return retval; +} + +int +_PyArg_UnpackStack(PyObject **args, Py_ssize_t nargs, const char *name, + Py_ssize_t min, Py_ssize_t max, ...) +{ + int retval; + va_list vargs; + +#ifdef HAVE_STDARG_PROTOTYPES + va_start(vargs, max); +#else + va_start(vargs); +#endif + retval = PyArg_UnpackStack_impl(args, nargs, name, min, max, vargs); + va_end(vargs); + return retval; } @@ -2431,8 +2466,9 @@ PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t m int _PyArg_NoKeywords(const char *funcname, PyObject *kwargs) { - if (kwargs == NULL) + if (kwargs == NULL) { return 1; + } if (!PyDict_CheckExact(kwargs)) { PyErr_BadInternalCall(); return 0; @@ -2450,8 +2486,9 @@ _PyArg_NoKeywords(const char *funcname, PyObject *kwargs) int _PyArg_NoStackKeywords(const char *funcname, PyObject *kwnames) { - if (kwnames == NULL) + if (kwnames == NULL) { return 1; + } assert(PyTuple_CheckExact(kwnames)); if (PyTuple_GET_SIZE(kwnames) == 0) { return 1; |