[Python-checkins] CVS: python/dist/src/Python getargs.c,2.68,2.69
Fred L. Drake
fdrake@users.sourceforge.net
Tue, 23 Oct 2001 14:09:31 -0700
Update of /cvsroot/python/python/dist/src/Python
In directory usw-pr-cvs1:/tmp/cvs-serv19103/Python
Modified Files:
getargs.c
Log Message:
PyArg_UnpackTuple(): New argument unpacking function suggested by Jim
Fulton, based on code Jim supplied.
Index: getargs.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/getargs.c,v
retrieving revision 2.68
retrieving revision 2.69
diff -C2 -d -r2.68 -r2.69
*** getargs.c 2001/10/23 14:41:08 2.68
--- getargs.c 2001/10/23 21:09:29 2.69
***************
*** 1361,1362 ****
--- 1361,1422 ----
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;
+ }