blob: e4c2634796d10d80ae5628f783cba1c5dd1395e9 [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
502 case 'f': /* float */
503 {
504 float *p = va_arg(*p_va, float *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000505 double dval = PyFloat_AsDouble(arg);
506 if (PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000507 return "float<f>";
508 else
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000509 *p = (float) dval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000510 break;
511 }
512
513 case 'd': /* double */
514 {
515 double *p = va_arg(*p_va, double *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000516 double dval = PyFloat_AsDouble(arg);
517 if (PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000518 return "float<d>";
519 else
520 *p = dval;
521 break;
522 }
523
Guido van Rossum530956d1996-07-21 02:27:43 +0000524#ifndef WITHOUT_COMPLEX
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000525 case 'D': /* complex double */
526 {
Guido van Rossum530956d1996-07-21 02:27:43 +0000527 Py_complex *p = va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000528 Py_complex cval;
529 cval = PyComplex_AsCComplex(arg);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 if (PyErr_Occurred())
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000531 return "complex<D>";
532 else
533 *p = cval;
534 break;
535 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000536#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000537
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000538 case 'c': /* char */
539 {
540 char *p = va_arg(*p_va, char *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000541 if (PyString_Check(arg) && PyString_Size(arg) == 1)
542 *p = PyString_AsString(arg)[0];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000543 else
544 return "char";
545 break;
546 }
547
548 case 's': /* string */
549 {
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000550 if (*format == '#') { /* any buffer-like object */
551 void **p = (void **)va_arg(*p_va, char **);
552 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000553 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000554 int count;
555
556 if ( pb == NULL ||
557 pb->bf_getreadbuffer == NULL ||
558 pb->bf_getsegcount == NULL )
559 return "read-only buffer";
560 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
561 return "single-segment read-only buffer";
562 if ( (count =
563 (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
564 return "(unspecified)";
565 *q = count;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000566 format++;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000567 } else {
568 char **p = va_arg(*p_va, char **);
569
570 if (PyString_Check(arg))
571 *p = PyString_AsString(arg);
572 else
573 return "string";
574 if ((int)strlen(*p) != PyString_Size(arg))
575 return "string without null bytes";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000576 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000577 break;
578 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000579
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000580 case 'z': /* string, may be NULL (None) */
581 {
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000582 if (*format == '#') { /* any buffer-like object */
583 void **p = (void **)va_arg(*p_va, char **);
584 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000585 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000586 int count;
587
588 if (arg == Py_None) {
589 *p = 0;
590 *q = 0;
591 } else {
592 if ( pb == NULL ||
593 pb->bf_getreadbuffer == NULL ||
594 pb->bf_getsegcount == NULL )
595 return "read-only buffer";
596 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
597 return "single-segment read-only buffer";
598 if ( (count = (*pb->bf_getreadbuffer)
599 (arg, 0, p)) < 0 )
600 return "(unspecified)";
601 *q = count;
602 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000603 format++;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000604 } else {
605 char **p = va_arg(*p_va, char **);
606
607 if (arg == Py_None)
608 *p = 0;
609 else if (PyString_Check(arg))
610 *p = PyString_AsString(arg);
611 else
612 return "None or string";
613 if (*format == '#') {
614 int *q = va_arg(*p_va, int *);
615 if (arg == Py_None)
616 *q = 0;
617 else
618 *q = PyString_Size(arg);
619 format++;
620 }
621 else if (*p != NULL &&
622 (int)strlen(*p) != PyString_Size(arg))
623 return "None or string without null bytes";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000624 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000625 break;
626 }
627
628 case 'S': /* string object */
629 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000630 PyObject **p = va_arg(*p_va, PyObject **);
631 if (PyString_Check(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000632 *p = arg;
633 else
634 return "string";
635 break;
636 }
637
638 case 'O': /* object */
639 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000640 PyTypeObject *type;
641 PyObject **p;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000642 if (*format == '!') {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000643 type = va_arg(*p_va, PyTypeObject*);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000644 p = va_arg(*p_va, PyObject **);
645 format++;
646 if (arg->ob_type == type)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000647 *p = arg;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000648 else
649 return type->tp_name;
650
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000651 }
652 else if (*format == '?') {
653 inquiry pred = va_arg(*p_va, inquiry);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000654 p = va_arg(*p_va, PyObject **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000655 format++;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000656 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000657 *p = arg;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000658 else
659 return "(unspecified)";
660
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000661 }
662 else if (*format == '&') {
Guido van Rossumaa354651996-08-19 19:32:04 +0000663 typedef int (*converter)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 Py_PROTO((PyObject *, void *));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000665 converter convert = va_arg(*p_va, converter);
666 void *addr = va_arg(*p_va, void *);
667 format++;
668 if (! (*convert)(arg, addr))
Guido van Rossum64fc6491995-01-21 14:09:37 +0000669 return "(unspecified)";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000670 }
671 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000672 p = va_arg(*p_va, PyObject **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000673 *p = arg;
674 }
675 break;
676 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000677
678
679 case 'w': /* memory buffer, read-write access */
680 {
681 void **p = va_arg(*p_va, void **);
682 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
683 int count;
684
685 if ( pb == NULL || pb->bf_getwritebuffer == NULL ||
686 pb->bf_getsegcount == NULL )
687 return "read-write buffer";
688 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
689 return "single-segment read-write buffer";
690 if ( (count = pb->bf_getwritebuffer(arg, 0, p)) < 0 )
691 return "(unspecified)";
692 if (*format == '#') {
693 int *q = va_arg(*p_va, int *);
694
695 *q = count;
696 format++;
697 }
698 break;
699 }
700
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000701
702 default:
703 return "impossible<bad format char>";
704
705 }
706
707 *p_format = format;
708 return NULL;
709}
Guido van Rossumaa354651996-08-19 19:32:04 +0000710
711
712/* Support for keyword arguments donated by
713 Geoff Philbrick <philbric@delphi.hks.com> */
714
715#ifdef HAVE_STDARG_PROTOTYPES
716/* VARARGS2 */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000717int PyArg_ParseTupleAndKeywords(PyObject *args,
718 PyObject *keywords,
Guido van Rossumaa354651996-08-19 19:32:04 +0000719 char *format,
720 char **kwlist, ...)
721#else
722/* VARARGS */
723int PyArg_ParseTupleAndKeywords(va_alist) va_dcl
724#endif
725{
726 int retval;
727 va_list va;
728#ifdef HAVE_STDARG_PROTOTYPES
729
730 va_start(va, kwlist);
731#else
Guido van Rossum79f25d91997-04-29 20:08:16 +0000732 PyObject *args;
733 PyObject *keywords;
Guido van Rossumaa354651996-08-19 19:32:04 +0000734 char *format;
735 char **kwlist;
736
737 va_start(va);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000738 args = va_arg(va, PyObject *);
739 keywords = va_arg(va, PyObject *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000740 format = va_arg(va, char *);
741 kwlist = va_arg(va, char **);
742#endif
743 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
744 va_end(va);
745 return retval;
746}
747
748
749static int
750vgetargskeywords(args, keywords, format, kwlist, p_va)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000751 PyObject *args;
752 PyObject *keywords;
Guido van Rossumaa354651996-08-19 19:32:04 +0000753 char *format;
754 char **kwlist;
755 va_list *p_va;
756{
757 char msgbuf[256];
758 int levels[32];
759 char *fname = NULL;
760 char *message = NULL;
761 int min = -1;
762 int max = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +0000763 char *formatsave = format;
764 int i, len, tplen, kwlen;
765 char *msg, *ks, **p;
766 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000767 PyObject *key, *value;
Guido van Rossumaa354651996-08-19 19:32:04 +0000768
769 /* nested tuples cannot be parsed when using keyword arguments */
770
771 for (;;) {
772 int c = *format++;
773 if (c == '(') {
774 PyErr_SetString(PyExc_SystemError,
775 "tuple found in format when using keyword arguments");
776 return 0;
777 }
778 else if (c == '\0')
779 break;
780 else if (c == ':') {
781 fname = format;
782 break;
783 }
784 else if (c == ';') {
785 message = format;
786 break;
787 }
788 else if (isalpha(c))
789 max++;
790 else if (c == '|')
791 min = max;
792 }
793
794 if (min < 0)
795 min = max;
796
797 format = formatsave;
798
799 if (!PyTuple_Check(args)) {
800 PyErr_SetString(PyExc_SystemError,
801 "new style getargs format but argument is not a tuple");
802 return 0;
803 }
804
805 tplen = PyTuple_Size(args);
806
807 /* do a cursory check of the keywords just to see how many we got */
808
809 if (keywords) {
810 if (!PyDict_Check(keywords)) {
811 PyErr_SetString(PyExc_SystemError,
812 "non-dictionary object received when keyword dictionary expected");
813 return 0;
814 }
815 kwlen = PyDict_Size(keywords);
816 }
817 else {
818 kwlen = 0;
819 }
820
821 /* make sure there are no duplicate values for an argument;
822 its not clear when to use the term "keyword argument vs.
823 keyword parameter in messages */
824
825 if (keywords) {
826 for (i = 0; i < tplen; i++) {
827 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
828 sprintf(msgbuf,
829 "keyword parameter %s redefined",
830 kwlist[i]);
831 PyErr_SetString(PyExc_TypeError, msgbuf);
832 return 0;
833 }
834 }
835 }
836 PyErr_Clear(); /* I'm not which Py functions set the error string */
837
838 /* required arguments missing from args can be supplied by keyword
839 arguments */
840
841 len = tplen;
842 if (keywords && tplen < min) {
843 for (i = tplen; i < min; i++) {
844 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
845 len++;
846 }
847 }
848 }
849 PyErr_Clear();
850
851 /* make sure we got an acceptable number of arguments; the message
852 is a little confusing with keywords since keyword arguments
853 which are supplied, but don't match the required arguments
854 are not included in the "%d given" part of the message */
855
856 if (len < min || max < len) {
857 if (message == NULL) {
858 sprintf(msgbuf,
859 "%s requires %s %d argument%s; %d given",
860 fname==NULL ? "function" : fname,
861 min==max ? "exactly"
862 : len < min ? "at least" : "at most",
863 len < min ? min : max,
864 (len < min ? min : max) == 1 ? "" : "s",
865 len);
866 message = msgbuf;
867 }
868 PyErr_SetString(PyExc_TypeError, message);
869 return 0;
870 }
871
872 for (i = 0; i < tplen; i++) {
873 if (*format == '|')
874 format++;
875 msg = convertitem(PyTuple_GetItem(args, i), &format, p_va,
876 levels, msgbuf);
877 if (msg) {
878 seterror(i+1, msg, levels, fname, message);
879 return 0;
880 }
881 }
882
883 /* handle no keyword parameters in call */
884
885 if (!keywords) return 1;
886
887 /* make sure the number of keywords in the keyword list matches the
888 number of items in the format string */
889
890 nkwds = 0;
891 p = kwlist;
892 for (;;) {
893 if (!*(p++)) break;
894 nkwds++;
895 }
896
897 if (nkwds != max) {
898 PyErr_SetString(PyExc_SystemError,
899 "number of items in format string and keyword list do not match");
900 return 0;
901 }
902
903 /* convert the keyword arguments; this uses the format
904 string where it was left after processing args */
905
906 converted = 0;
907 for (i = tplen; i < nkwds; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +0000909 if (*format == '|')
910 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +0000911 item = PyMapping_GetItemString(keywords, kwlist[i]);
912 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +0000913 msg = convertitem(item, &format, p_va, levels, msgbuf);
914 if (msg) {
915 seterror(i+1, msg, levels, fname, message);
916 return 0;
917 }
918 converted++;
919 }
920 else {
921 PyErr_Clear();
922 msg = skipitem(&format, p_va);
923 if (msg) {
924 seterror(i+1, msg, levels, fname, message);
925 return 0;
926 }
927 }
928 }
929
930 /* make sure there are no extraneous keyword arguments */
931
932 pos = 0;
933 if (converted < kwlen) {
934 while (PyDict_Next(keywords, &pos, &key, &value)) {
935 match = 0;
936 ks = PyString_AsString(key);
937 for (i = 0; i < nkwds; i++) {
938 if (!strcmp(ks, kwlist[i])) {
939 match = 1;
940 break;
941 }
942 }
943 if (!match) {
944 sprintf(msgbuf,
945 "%s is an invalid keyword argument for this function",
946 ks);
947 PyErr_SetString(PyExc_TypeError, msgbuf);
948 return 0;
949 }
950 }
951 }
952
953 return 1;
954}
955
956
957static char *
958skipitem(p_format, p_va)
959 char **p_format;
960 va_list *p_va;
961{
962 char *format = *p_format;
963 char c = *format++;
964
965 switch (c) {
966
967 case 'b': /* byte -- very short int */
968 {
Guido van Rossum80bb9651996-12-05 23:27:02 +0000969 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000970 break;
971 }
972
973 case 'h': /* short int */
974 {
Guido van Rossum80bb9651996-12-05 23:27:02 +0000975 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000976 break;
977 }
978
979 case 'i': /* int */
980 {
Guido van Rossum80bb9651996-12-05 23:27:02 +0000981 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000982 break;
983 }
984
985 case 'l': /* long int */
986 {
Guido van Rossum80bb9651996-12-05 23:27:02 +0000987 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000988 break;
989 }
990
991 case 'f': /* float */
992 {
Guido van Rossum80bb9651996-12-05 23:27:02 +0000993 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000994 break;
995 }
996
997 case 'd': /* double */
998 {
Guido van Rossum80bb9651996-12-05 23:27:02 +0000999 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001000 break;
1001 }
1002
1003#ifndef WITHOUT_COMPLEX
1004 case 'D': /* complex double */
1005 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001006 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001007 break;
1008 }
1009#endif /* WITHOUT_COMPLEX */
1010
1011 case 'c': /* char */
1012 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001013 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001014 break;
1015 }
1016
1017 case 's': /* string */
1018 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001019 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001020 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001021 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001022 format++;
1023 }
1024 break;
1025 }
1026
1027 case 'z': /* string */
1028 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001029 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001030 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001031 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001032 format++;
1033 }
1034 break;
1035 }
1036
1037 case 'S': /* string object */
1038 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001039 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001040 break;
1041 }
1042
1043 case 'O': /* object */
1044 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001045 if (*format == '!') {
1046 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001047 (void) va_arg(*p_va, PyTypeObject*);
1048 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001049 }
1050#if 0
1051/* I don't know what this is for */
1052 else if (*format == '?') {
1053 inquiry pred = va_arg(*p_va, inquiry);
1054 format++;
1055 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001056 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001057 }
1058 }
1059#endif
1060 else if (*format == '&') {
1061 typedef int (*converter)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001062 Py_PROTO((PyObject *, void *));
Guido van Rossum80bb9651996-12-05 23:27:02 +00001063 (void) va_arg(*p_va, converter);
1064 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001065 format++;
1066 }
1067 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001068 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001069 }
1070 break;
1071 }
1072
1073 default:
1074 return "impossible<bad format char>";
1075
1076 }
1077
1078 *p_format = format;
1079 return NULL;
1080}