blob: 86fd471a87c5e6d3812770ca088762db371a0493 [file] [log] [blame]
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000029
30******************************************************************/
31
32/* New getargs implementation */
33
34/* XXX There are several unchecked sprintf or strcat calls in this file.
35 XXX The only way these can become a danger is if some C code in the
36 XXX Python source (or in an extension) uses ridiculously long names
37 XXX or riduculously deep nesting in format strings. */
38
Guido van Rossum79f25d91997-04-29 20:08:16 +000039#include "Python.h"
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000040
Guido van Rossumc1d50531996-08-21 23:38:24 +000041#include <ctype.h>
42
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000043
Guido van Rossum79f25d91997-04-29 20:08:16 +000044int PyArg_Parse Py_PROTO((PyObject *, char *, ...));
45int PyArg_ParseTuple Py_PROTO((PyObject *, char *, ...));
Guido van Rossum13454c31997-05-05 21:57:29 +000046int PyArg_VaParse Py_PROTO((PyObject *, char *, va_list));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000047
Guido van Rossum79f25d91997-04-29 20:08:16 +000048int PyArg_ParseTupleAndKeywords Py_PROTO((PyObject *, PyObject *,
Guido van Rossumaa354651996-08-19 19:32:04 +000049 char *, char **, ...));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000050
51/* Forward */
Guido van Rossum79f25d91997-04-29 20:08:16 +000052static int vgetargs1 Py_PROTO((PyObject *, char *, va_list *, int));
53static void seterror Py_PROTO((int, char *, int *, char *, char *));
54static char *convertitem Py_PROTO((PyObject *, char **, va_list *,
55 int *, char *));
56static char *converttuple Py_PROTO((PyObject *, char **, va_list *,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000057 int *, char *, int));
Guido van Rossum79f25d91997-04-29 20:08:16 +000058static char *convertsimple Py_PROTO((PyObject *, char **, va_list *, char *));
59static char *convertsimple1 Py_PROTO((PyObject *, char **, va_list *));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000060
Guido van Rossum79f25d91997-04-29 20:08:16 +000061static int vgetargskeywords Py_PROTO((PyObject *, PyObject *,
Guido van Rossumaa354651996-08-19 19:32:04 +000062 char *, char **, va_list *));
Guido van Rossum79f25d91997-04-29 20:08:16 +000063static char *skipitem Py_PROTO((char **, va_list *));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000064
65#ifdef HAVE_STDARG_PROTOTYPES
66/* VARARGS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +000067int PyArg_Parse(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000068#else
69/* VARARGS */
Guido van Rossum79f25d91997-04-29 20:08:16 +000070int PyArg_Parse(va_alist) va_dcl
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000071#endif
72{
73 int retval;
74 va_list va;
75#ifdef HAVE_STDARG_PROTOTYPES
76
77 va_start(va, format);
78#else
Guido van Rossum79f25d91997-04-29 20:08:16 +000079 PyObject *args;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000080 char *format;
81
82 va_start(va);
Guido van Rossum79f25d91997-04-29 20:08:16 +000083 args = va_arg(va, PyObject *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000084 format = va_arg(va, char *);
85#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +000086 retval = vgetargs1(args, format, &va, 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000087 va_end(va);
88 return retval;
89}
90
91
92#ifdef HAVE_STDARG_PROTOTYPES
93/* VARARGS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +000094int PyArg_ParseTuple(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000095#else
96/* VARARGS */
Guido van Rossum79f25d91997-04-29 20:08:16 +000097int PyArg_ParseTuple(va_alist) va_dcl
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000098#endif
99{
100 int retval;
101 va_list va;
102#ifdef HAVE_STDARG_PROTOTYPES
103
104 va_start(va, format);
105#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000106 PyObject *args;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000107 char *format;
108
109 va_start(va);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000110 args = va_arg(va, PyObject *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000111 format = va_arg(va, char *);
112#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000113 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000114 va_end(va);
115 return retval;
116}
117
118
119int
Guido van Rossum13454c31997-05-05 21:57:29 +0000120PyArg_VaParse(args, format, va)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000121 PyObject *args;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000122 char *format;
123 va_list va;
124{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000125 va_list lva;
126
127#ifdef VA_LIST_IS_ARRAY
128 memcpy(lva, va, sizeof(va_list));
129#else
130 lva = va;
131#endif
132
133 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000134}
135
136
137static int
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000138vgetargs1(args, format, p_va, compat)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000139 PyObject *args;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000140 char *format;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000141 va_list *p_va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000142 int compat;
143{
144 char msgbuf[256];
145 int levels[32];
146 char *fname = NULL;
147 char *message = NULL;
148 int min = -1;
149 int max = 0;
150 int level = 0;
151 char *formatsave = format;
152 int i, len;
153 char *msg;
154
155 for (;;) {
156 int c = *format++;
157 if (c == '(' /* ')' */) {
158 if (level == 0)
159 max++;
160 level++;
161 }
162 else if (/* '(' */ c == ')') {
163 if (level == 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 Py_FatalError(/* '(' */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000165 "excess ')' in getargs format");
166 else
167 level--;
168 }
169 else if (c == '\0')
170 break;
171 else if (c == ':') {
172 fname = format;
173 break;
174 }
175 else if (c == ';') {
176 message = format;
177 break;
178 }
179 else if (level != 0)
180 ; /* Pass */
181 else if (isalpha(c))
182 max++;
183 else if (c == '|')
184 min = max;
185 }
186
187 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000188 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000189
190 if (min < 0)
191 min = max;
192
193 format = formatsave;
194
195 if (compat) {
196 if (max == 0) {
197 if (args == NULL)
198 return 1;
199 sprintf(msgbuf, "%s requires no arguments",
200 fname==NULL ? "function" : fname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000201 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000202 return 0;
203 }
204 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000205 if (args == NULL) {
206 sprintf(msgbuf,
207 "%s requires at least one argument",
208 fname==NULL ? "function" : fname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000209 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000210 return 0;
211 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000212 msg = convertitem(args, &format, p_va, levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000213 if (msg == NULL)
214 return 1;
215 seterror(levels[0], msg, levels+1, fname, message);
216 return 0;
217 }
218 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000220 "old style getargs format uses new features");
221 return 0;
222 }
223 }
224
Guido van Rossum79f25d91997-04-29 20:08:16 +0000225 if (!PyTuple_Check(args)) {
226 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000227 "new style getargs format but argument is not a tuple");
228 return 0;
229 }
230
Guido van Rossum79f25d91997-04-29 20:08:16 +0000231 len = PyTuple_Size(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000232
233 if (len < min || max < len) {
234 if (message == NULL) {
235 sprintf(msgbuf,
236 "%s requires %s %d argument%s; %d given",
237 fname==NULL ? "function" : fname,
238 min==max ? "exactly"
239 : len < min ? "at least" : "at most",
240 len < min ? min : max,
241 (len < min ? min : max) == 1 ? "" : "s",
242 len);
243 message = msgbuf;
244 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000245 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000246 return 0;
247 }
248
249 for (i = 0; i < len; i++) {
250 if (*format == '|')
251 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000252 msg = convertitem(PyTuple_GetItem(args, i), &format, p_va,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000253 levels, msgbuf);
254 if (msg) {
255 seterror(i+1, msg, levels, fname, message);
256 return 0;
257 }
258 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000259
Guido van Rossum730806d1998-04-10 22:27:42 +0000260 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000261 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000262 *format != '|' && *format != ':' && *format != ';') {
263 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000264 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000265 return 0;
266 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000267
268 return 1;
269}
270
271
272
273static void
274seterror(iarg, msg, levels, fname, message)
275 int iarg;
276 char *msg;
277 int *levels;
278 char *fname;
279 char *message;
280{
281 char buf[256];
282 int i;
283 char *p = buf;
284
Guido van Rossum79f25d91997-04-29 20:08:16 +0000285 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000286 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000287 if (iarg == 0 && message == NULL)
288 message = msg;
289 else if (message == NULL) {
290 if (fname != NULL) {
291 sprintf(p, "%s, ", fname);
292 p += strlen(p);
293 }
294 sprintf(p, "argument %d", iarg);
295 i = 0;
296 p += strlen(p);
297 while (levels[i] > 0) {
298 sprintf(p, ", item %d", levels[i]-1);
299 p += strlen(p);
300 i++;
301 }
302 sprintf(p, ": expected %s found", msg);
303 message = buf;
304 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000305 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000306}
307
308
309/* Convert a tuple argument.
310 On entry, *p_format points to the character _after_ the opening '('.
311 On successful exit, *p_format points to the closing ')'.
312 If successful:
313 *p_format and *p_va are updated,
314 *levels and *msgbuf are untouched,
315 and NULL is returned.
316 If the argument is invalid:
317 *p_format is unchanged,
318 *p_va is undefined,
319 *levels is a 0-terminated list of item numbers,
320 *msgbuf contains an error message, whose format is:
321 "<typename1>, <typename2>", where:
322 <typename1> is the name of the expected type, and
323 <typename2> is the name of the actual type,
324 (so you can surround it by "expected ... found"),
325 and msgbuf is returned.
326*/
327
328static char *
329converttuple(arg, p_format, p_va, levels, msgbuf, toplevel)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000330 PyObject *arg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000331 char **p_format;
332 va_list *p_va;
333 int *levels;
334 char *msgbuf;
335 int toplevel;
336{
337 int level = 0;
338 int n = 0;
339 char *format = *p_format;
340 int i;
341
342 for (;;) {
343 int c = *format++;
344 if (c == '(') {
345 if (level == 0)
346 n++;
347 level++;
348 }
349 else if (c == ')') {
350 if (level == 0)
351 break;
352 level--;
353 }
354 else if (c == ':' || c == ';' || c == '\0')
355 break;
356 else if (level == 0 && isalpha(c))
357 n++;
358 }
359
Guido van Rossum79f25d91997-04-29 20:08:16 +0000360 if (!PyTuple_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000361 levels[0] = 0;
362 sprintf(msgbuf,
363 toplevel ? "%d arguments, %s" : "%d-tuple, %s",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000364 n, arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000365 return msgbuf;
366 }
367
Guido van Rossum79f25d91997-04-29 20:08:16 +0000368 if ((i = PyTuple_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000369 levels[0] = 0;
370 sprintf(msgbuf,
371 toplevel ? "%d arguments, %d" : "%d-tuple, %d-tuple",
372 n, i);
373 return msgbuf;
374 }
375
376 format = *p_format;
377 for (i = 0; i < n; i++) {
378 char *msg;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379 msg = convertitem(PyTuple_GetItem(arg, i), &format, p_va,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000380 levels+1, msgbuf);
381 if (msg != NULL) {
382 levels[0] = i+1;
383 return msg;
384 }
385 }
386
387 *p_format = format;
388 return NULL;
389}
390
391
392/* Convert a single item. */
393
394static char *
395convertitem(arg, p_format, p_va, levels, msgbuf)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000396 PyObject *arg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000397 char **p_format;
398 va_list *p_va;
399 int *levels;
400 char *msgbuf;
401{
402 char *msg;
403 char *format = *p_format;
404
405 if (*format == '(' /* ')' */) {
406 format++;
407 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
408 if (msg == NULL)
409 format++;
410 }
411 else {
412 msg = convertsimple(arg, &format, p_va, msgbuf);
413 if (msg != NULL)
414 levels[0] = 0;
415 }
416 if (msg == NULL)
417 *p_format = format;
418 return msg;
419}
420
421
422/* Convert a non-tuple argument. Adds to convertsimple1 functionality
423 by appending ", <actual argument type>" to error message. */
424
425static char *
426convertsimple(arg, p_format, p_va, msgbuf)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000427 PyObject *arg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000428 char **p_format;
429 va_list *p_va;
430 char *msgbuf;
431{
432 char *msg = convertsimple1(arg, p_format, p_va);
433 if (msg != NULL) {
434 sprintf(msgbuf, "%.50s, %.50s", msg,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000435 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000436 msg = msgbuf;
437 }
438 return msg;
439}
440
441
442/* Convert a non-tuple argument. Return NULL if conversion went OK,
443 or a string representing the expected type if the conversion failed.
444 When failing, an exception may or may not have been raised.
445 Don't call if a tuple is expected. */
446
447static char *
448convertsimple1(arg, p_format, p_va)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000449 PyObject *arg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000450 char **p_format;
451 va_list *p_va;
452{
453 char *format = *p_format;
454 char c = *format++;
455
456 switch (c) {
457
458 case 'b': /* byte -- very short int */
459 {
460 char *p = va_arg(*p_va, char *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000461 long ival = PyInt_AsLong(arg);
462 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000463 return "integer<b>";
464 else
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000465 *p = (char) ival;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000466 break;
467 }
468
469 case 'h': /* short int */
470 {
471 short *p = va_arg(*p_va, short *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000472 long ival = PyInt_AsLong(arg);
473 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000474 return "integer<h>";
475 else
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000476 *p = (short) ival;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000477 break;
478 }
479
480 case 'i': /* int */
481 {
482 int *p = va_arg(*p_va, int *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000483 long ival = PyInt_AsLong(arg);
484 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000485 return "integer<i>";
486 else
487 *p = ival;
488 break;
489 }
490
491 case 'l': /* long int */
492 {
493 long *p = va_arg(*p_va, long *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000494 long ival = PyInt_AsLong(arg);
495 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000496 return "integer<l>";
497 else
498 *p = ival;
499 break;
500 }
501
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000502#if HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +0000503 case 'L': /* LONG_LONG */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000504 {
Guido van Rossum3293b071998-08-25 16:07:15 +0000505 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
506 LONG_LONG ival = PyLong_AsLongLong( arg );
507 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000508 return "long<L>";
509 } else {
510 *p = ival;
511 }
512 break;
513 }
514#endif
515
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000516 case 'f': /* float */
517 {
518 float *p = va_arg(*p_va, float *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000519 double dval = PyFloat_AsDouble(arg);
520 if (PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000521 return "float<f>";
522 else
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000523 *p = (float) dval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000524 break;
525 }
526
527 case 'd': /* double */
528 {
529 double *p = va_arg(*p_va, double *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 double dval = PyFloat_AsDouble(arg);
531 if (PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000532 return "float<d>";
533 else
534 *p = dval;
535 break;
536 }
537
Guido van Rossum530956d1996-07-21 02:27:43 +0000538#ifndef WITHOUT_COMPLEX
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000539 case 'D': /* complex double */
540 {
Guido van Rossum530956d1996-07-21 02:27:43 +0000541 Py_complex *p = va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000542 Py_complex cval;
543 cval = PyComplex_AsCComplex(arg);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544 if (PyErr_Occurred())
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000545 return "complex<D>";
546 else
547 *p = cval;
548 break;
549 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000550#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000551
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000552 case 'c': /* char */
553 {
554 char *p = va_arg(*p_va, char *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 if (PyString_Check(arg) && PyString_Size(arg) == 1)
556 *p = PyString_AsString(arg)[0];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000557 else
558 return "char";
559 break;
560 }
561
562 case 's': /* string */
563 {
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000564 if (*format == '#') { /* any buffer-like object */
565 void **p = (void **)va_arg(*p_va, char **);
566 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000567 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000568 int count;
569
570 if ( pb == NULL ||
571 pb->bf_getreadbuffer == NULL ||
572 pb->bf_getsegcount == NULL )
573 return "read-only buffer";
574 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
575 return "single-segment read-only buffer";
576 if ( (count =
577 (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
578 return "(unspecified)";
579 *q = count;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000580 format++;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000581 } else {
582 char **p = va_arg(*p_va, char **);
583
584 if (PyString_Check(arg))
585 *p = PyString_AsString(arg);
586 else
587 return "string";
588 if ((int)strlen(*p) != PyString_Size(arg))
589 return "string without null bytes";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000590 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000591 break;
592 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000593
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000594 case 'z': /* string, may be NULL (None) */
595 {
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000596 if (*format == '#') { /* any buffer-like object */
597 void **p = (void **)va_arg(*p_va, char **);
598 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000599 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000600 int count;
601
602 if (arg == Py_None) {
603 *p = 0;
604 *q = 0;
605 } else {
606 if ( pb == NULL ||
607 pb->bf_getreadbuffer == NULL ||
608 pb->bf_getsegcount == NULL )
609 return "read-only buffer";
610 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
611 return "single-segment read-only buffer";
612 if ( (count = (*pb->bf_getreadbuffer)
613 (arg, 0, p)) < 0 )
614 return "(unspecified)";
615 *q = count;
616 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000617 format++;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000618 } else {
619 char **p = va_arg(*p_va, char **);
620
621 if (arg == Py_None)
622 *p = 0;
623 else if (PyString_Check(arg))
624 *p = PyString_AsString(arg);
625 else
626 return "None or string";
627 if (*format == '#') {
628 int *q = va_arg(*p_va, int *);
629 if (arg == Py_None)
630 *q = 0;
631 else
632 *q = PyString_Size(arg);
633 format++;
634 }
635 else if (*p != NULL &&
636 (int)strlen(*p) != PyString_Size(arg))
637 return "None or string without null bytes";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000638 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000639 break;
640 }
641
642 case 'S': /* string object */
643 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 PyObject **p = va_arg(*p_va, PyObject **);
645 if (PyString_Check(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000646 *p = arg;
647 else
648 return "string";
649 break;
650 }
651
652 case 'O': /* object */
653 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 PyTypeObject *type;
655 PyObject **p;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000656 if (*format == '!') {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 type = va_arg(*p_va, PyTypeObject*);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000658 p = va_arg(*p_va, PyObject **);
659 format++;
660 if (arg->ob_type == type)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000661 *p = arg;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000662 else
663 return type->tp_name;
664
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000665 }
666 else if (*format == '?') {
667 inquiry pred = va_arg(*p_va, inquiry);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000668 p = va_arg(*p_va, PyObject **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000669 format++;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000670 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000671 *p = arg;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000672 else
673 return "(unspecified)";
674
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000675 }
676 else if (*format == '&') {
Guido van Rossumaa354651996-08-19 19:32:04 +0000677 typedef int (*converter)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000678 Py_PROTO((PyObject *, void *));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000679 converter convert = va_arg(*p_va, converter);
680 void *addr = va_arg(*p_va, void *);
681 format++;
682 if (! (*convert)(arg, addr))
Guido van Rossum64fc6491995-01-21 14:09:37 +0000683 return "(unspecified)";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000684 }
685 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 p = va_arg(*p_va, PyObject **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000687 *p = arg;
688 }
689 break;
690 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000691
692
693 case 'w': /* memory buffer, read-write access */
694 {
695 void **p = va_arg(*p_va, void **);
696 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
697 int count;
698
699 if ( pb == NULL || pb->bf_getwritebuffer == NULL ||
700 pb->bf_getsegcount == NULL )
701 return "read-write buffer";
702 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
703 return "single-segment read-write buffer";
704 if ( (count = pb->bf_getwritebuffer(arg, 0, p)) < 0 )
705 return "(unspecified)";
706 if (*format == '#') {
707 int *q = va_arg(*p_va, int *);
708
709 *q = count;
710 format++;
711 }
712 break;
713 }
714
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000715
716 default:
717 return "impossible<bad format char>";
718
719 }
720
721 *p_format = format;
722 return NULL;
723}
Guido van Rossumaa354651996-08-19 19:32:04 +0000724
725
726/* Support for keyword arguments donated by
727 Geoff Philbrick <philbric@delphi.hks.com> */
728
729#ifdef HAVE_STDARG_PROTOTYPES
730/* VARARGS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000731int PyArg_ParseTupleAndKeywords(PyObject *args,
732 PyObject *keywords,
Guido van Rossumaa354651996-08-19 19:32:04 +0000733 char *format,
734 char **kwlist, ...)
735#else
736/* VARARGS */
737int PyArg_ParseTupleAndKeywords(va_alist) va_dcl
738#endif
739{
740 int retval;
741 va_list va;
742#ifdef HAVE_STDARG_PROTOTYPES
743
744 va_start(va, kwlist);
745#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000746 PyObject *args;
747 PyObject *keywords;
Guido van Rossumaa354651996-08-19 19:32:04 +0000748 char *format;
749 char **kwlist;
750
751 va_start(va);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 args = va_arg(va, PyObject *);
753 keywords = va_arg(va, PyObject *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000754 format = va_arg(va, char *);
755 kwlist = va_arg(va, char **);
756#endif
757 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
758 va_end(va);
759 return retval;
760}
761
762
763static int
764vgetargskeywords(args, keywords, format, kwlist, p_va)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000765 PyObject *args;
766 PyObject *keywords;
Guido van Rossumaa354651996-08-19 19:32:04 +0000767 char *format;
768 char **kwlist;
769 va_list *p_va;
770{
771 char msgbuf[256];
772 int levels[32];
773 char *fname = NULL;
774 char *message = NULL;
775 int min = -1;
776 int max = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +0000777 char *formatsave = format;
778 int i, len, tplen, kwlen;
779 char *msg, *ks, **p;
780 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000781 PyObject *key, *value;
Guido van Rossumaa354651996-08-19 19:32:04 +0000782
783 /* nested tuples cannot be parsed when using keyword arguments */
784
785 for (;;) {
786 int c = *format++;
787 if (c == '(') {
788 PyErr_SetString(PyExc_SystemError,
789 "tuple found in format when using keyword arguments");
790 return 0;
791 }
792 else if (c == '\0')
793 break;
794 else if (c == ':') {
795 fname = format;
796 break;
797 }
798 else if (c == ';') {
799 message = format;
800 break;
801 }
802 else if (isalpha(c))
803 max++;
804 else if (c == '|')
805 min = max;
806 }
807
808 if (min < 0)
809 min = max;
810
811 format = formatsave;
812
813 if (!PyTuple_Check(args)) {
814 PyErr_SetString(PyExc_SystemError,
815 "new style getargs format but argument is not a tuple");
816 return 0;
817 }
818
819 tplen = PyTuple_Size(args);
820
821 /* do a cursory check of the keywords just to see how many we got */
822
823 if (keywords) {
824 if (!PyDict_Check(keywords)) {
825 PyErr_SetString(PyExc_SystemError,
826 "non-dictionary object received when keyword dictionary expected");
827 return 0;
828 }
829 kwlen = PyDict_Size(keywords);
830 }
831 else {
832 kwlen = 0;
833 }
834
835 /* make sure there are no duplicate values for an argument;
836 its not clear when to use the term "keyword argument vs.
837 keyword parameter in messages */
838
839 if (keywords) {
840 for (i = 0; i < tplen; i++) {
841 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
842 sprintf(msgbuf,
843 "keyword parameter %s redefined",
844 kwlist[i]);
845 PyErr_SetString(PyExc_TypeError, msgbuf);
846 return 0;
847 }
848 }
849 }
850 PyErr_Clear(); /* I'm not which Py functions set the error string */
851
852 /* required arguments missing from args can be supplied by keyword
853 arguments */
854
855 len = tplen;
856 if (keywords && tplen < min) {
857 for (i = tplen; i < min; i++) {
858 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
859 len++;
860 }
861 }
862 }
863 PyErr_Clear();
864
865 /* make sure we got an acceptable number of arguments; the message
866 is a little confusing with keywords since keyword arguments
867 which are supplied, but don't match the required arguments
868 are not included in the "%d given" part of the message */
869
870 if (len < min || max < len) {
871 if (message == NULL) {
872 sprintf(msgbuf,
873 "%s requires %s %d argument%s; %d given",
874 fname==NULL ? "function" : fname,
875 min==max ? "exactly"
876 : len < min ? "at least" : "at most",
877 len < min ? min : max,
878 (len < min ? min : max) == 1 ? "" : "s",
879 len);
880 message = msgbuf;
881 }
882 PyErr_SetString(PyExc_TypeError, message);
883 return 0;
884 }
885
886 for (i = 0; i < tplen; i++) {
887 if (*format == '|')
888 format++;
889 msg = convertitem(PyTuple_GetItem(args, i), &format, p_va,
890 levels, msgbuf);
891 if (msg) {
892 seterror(i+1, msg, levels, fname, message);
893 return 0;
894 }
895 }
896
897 /* handle no keyword parameters in call */
898
899 if (!keywords) return 1;
900
901 /* make sure the number of keywords in the keyword list matches the
902 number of items in the format string */
903
904 nkwds = 0;
905 p = kwlist;
906 for (;;) {
907 if (!*(p++)) break;
908 nkwds++;
909 }
910
911 if (nkwds != max) {
912 PyErr_SetString(PyExc_SystemError,
913 "number of items in format string and keyword list do not match");
914 return 0;
915 }
916
917 /* convert the keyword arguments; this uses the format
918 string where it was left after processing args */
919
920 converted = 0;
921 for (i = tplen; i < nkwds; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000922 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +0000923 if (*format == '|')
924 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000925 item = PyMapping_GetItemString(keywords, kwlist[i]);
926 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +0000927 msg = convertitem(item, &format, p_va, levels, msgbuf);
928 if (msg) {
929 seterror(i+1, msg, levels, fname, message);
930 return 0;
931 }
932 converted++;
933 }
934 else {
935 PyErr_Clear();
936 msg = skipitem(&format, p_va);
937 if (msg) {
938 seterror(i+1, msg, levels, fname, message);
939 return 0;
940 }
941 }
942 }
943
944 /* make sure there are no extraneous keyword arguments */
945
946 pos = 0;
947 if (converted < kwlen) {
948 while (PyDict_Next(keywords, &pos, &key, &value)) {
949 match = 0;
950 ks = PyString_AsString(key);
951 for (i = 0; i < nkwds; i++) {
952 if (!strcmp(ks, kwlist[i])) {
953 match = 1;
954 break;
955 }
956 }
957 if (!match) {
958 sprintf(msgbuf,
959 "%s is an invalid keyword argument for this function",
960 ks);
961 PyErr_SetString(PyExc_TypeError, msgbuf);
962 return 0;
963 }
964 }
965 }
966
967 return 1;
968}
969
970
971static char *
972skipitem(p_format, p_va)
973 char **p_format;
974 va_list *p_va;
975{
976 char *format = *p_format;
977 char c = *format++;
978
979 switch (c) {
980
981 case 'b': /* byte -- very short int */
982 {
Guido van Rossum80bb9651996-12-05 23:27:02 +0000983 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000984 break;
985 }
986
987 case 'h': /* short int */
988 {
Guido van Rossum80bb9651996-12-05 23:27:02 +0000989 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000990 break;
991 }
992
993 case 'i': /* int */
994 {
Guido van Rossum80bb9651996-12-05 23:27:02 +0000995 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000996 break;
997 }
998
999 case 'l': /* long int */
1000 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001001 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001002 break;
1003 }
1004
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001005#if HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001006 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001007 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001008 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001009 break;
1010 }
1011#endif
1012
Guido van Rossumaa354651996-08-19 19:32:04 +00001013 case 'f': /* float */
1014 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001015 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001016 break;
1017 }
1018
1019 case 'd': /* double */
1020 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001021 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001022 break;
1023 }
1024
1025#ifndef WITHOUT_COMPLEX
1026 case 'D': /* complex double */
1027 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001028 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001029 break;
1030 }
1031#endif /* WITHOUT_COMPLEX */
1032
1033 case 'c': /* char */
1034 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001035 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001036 break;
1037 }
1038
1039 case 's': /* string */
1040 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001041 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001042 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001043 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001044 format++;
1045 }
1046 break;
1047 }
1048
1049 case 'z': /* string */
1050 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001051 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001052 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001053 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001054 format++;
1055 }
1056 break;
1057 }
1058
1059 case 'S': /* string object */
1060 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001062 break;
1063 }
1064
1065 case 'O': /* object */
1066 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001067 if (*format == '!') {
1068 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001069 (void) va_arg(*p_va, PyTypeObject*);
1070 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001071 }
1072#if 0
1073/* I don't know what this is for */
1074 else if (*format == '?') {
1075 inquiry pred = va_arg(*p_va, inquiry);
1076 format++;
1077 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001078 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001079 }
1080 }
1081#endif
1082 else if (*format == '&') {
1083 typedef int (*converter)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001084 Py_PROTO((PyObject *, void *));
Guido van Rossum80bb9651996-12-05 23:27:02 +00001085 (void) va_arg(*p_va, converter);
1086 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001087 format++;
1088 }
1089 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001090 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001091 }
1092 break;
1093 }
1094
1095 default:
1096 return "impossible<bad format char>";
1097
1098 }
1099
1100 *p_format = format;
1101 return NULL;
1102}