From e4616e67525be58454db078594776c98450d8810 Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Tue, 23 Oct 2001 21:09:29 +0000 Subject: PyArg_UnpackTuple(): New argument unpacking function suggested by Jim Fulton, based on code Jim supplied. --- Include/modsupport.h | 1 + Python/getargs.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/Include/modsupport.h b/Include/modsupport.h index 8ef343c..ccbabff 100644 --- a/Include/modsupport.h +++ b/Include/modsupport.h @@ -13,6 +13,7 @@ extern DL_IMPORT(int) PyArg_Parse(PyObject *, char *, ...); extern DL_IMPORT(int) PyArg_ParseTuple(PyObject *, char *, ...); extern DL_IMPORT(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, char *, char **, ...); +extern DL_IMPORT(int) PyArg_UnpackTuple(PyObject *, char *, int, int, ...); extern DL_IMPORT(PyObject *) Py_BuildValue(char *, ...); extern DL_IMPORT(int) PyArg_VaParse(PyObject *, char *, va_list); diff --git a/Python/getargs.c b/Python/getargs.c index 592d247..6593a82 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -1360,3 +1360,63 @@ skipitem(char **p_format, va_list *p_va) *p_format = format; return NULL; } + + +int +PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...) +{ + int i, l; + PyObject **o; + va_list vargs; + +#ifdef HAVE_STDARG_PROTOTYPES + va_start(vargs, max); +#else + va_start(vargs); +#endif + + 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( + PyExc_TypeError, + "%s expected %s%d arguments, got %d", + name, (min == max ? "" : "at least "), min, l); + else + PyErr_Format( + PyExc_TypeError, + "unpacked tuple should have %s%d elements," + " but has %d", + (min == max ? "" : "at least "), min, l); + va_end(vargs); + return 0; + } + if (l > max) { + if (name != NULL) + PyErr_Format( + PyExc_TypeError, + "%s expected %s%d arguments, got %d", + name, (min == max ? "" : "at most "), max, l); + else + PyErr_Format( + PyExc_TypeError, + "unpacked tuple should have %s%d elements," + " but has %d", + (min == max ? "" : "at most "), max, l); + va_end(vargs); + return 0; + } + for (i = 0; i < l; i++) { + o = va_arg(vargs, PyObject **); + *o = PyTuple_GET_ITEM(args, i); + } + va_end(vargs); + return 1; +} -- cgit v0.12