blob: 8c00b0ee1a10f2a4b38fe574810891559f855254 [file] [log] [blame]
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001
2/* New getargs implementation */
3
4/* XXX There are several unchecked sprintf or strcat calls in this file.
5 XXX The only way these can become a danger is if some C code in the
6 XXX Python source (or in an extension) uses ridiculously long names
Thomas Wouters7e474022000-07-16 12:04:32 +00007 XXX or ridiculously deep nesting in format strings. */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00008
Guido van Rossum79f25d91997-04-29 20:08:16 +00009#include "Python.h"
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000010
Guido van Rossumc1d50531996-08-21 23:38:24 +000011#include <ctype.h>
12
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000013
Tim Petersdbd9ba62000-07-09 03:09:57 +000014int PyArg_Parse(PyObject *, char *, ...);
15int PyArg_ParseTuple(PyObject *, char *, ...);
16int PyArg_VaParse(PyObject *, char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000017
Tim Petersdbd9ba62000-07-09 03:09:57 +000018int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
19 char *, char **, ...);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000020
21/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000022static int vgetargs1(PyObject *, char *, va_list *, int);
23static void seterror(int, char *, int *, char *, char *);
24static char *convertitem(PyObject *, char **, va_list *, int *, char *);
25static char *converttuple(PyObject *, char **, va_list *,
26 int *, char *, int);
27static char *convertsimple(PyObject *, char **, va_list *, char *);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +000028static int convertbuffer(PyObject *, void **p, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000029
Tim Petersdbd9ba62000-07-09 03:09:57 +000030static int vgetargskeywords(PyObject *, PyObject *,
31 char *, char **, va_list *);
32static char *skipitem(char **, va_list *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000033
Guido van Rossum79f25d91997-04-29 20:08:16 +000034int PyArg_Parse(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000035{
36 int retval;
37 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000038
39 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000040 retval = vgetargs1(args, format, &va, 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000041 va_end(va);
42 return retval;
43}
44
45
Guido van Rossum79f25d91997-04-29 20:08:16 +000046int PyArg_ParseTuple(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000047{
48 int retval;
49 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000050
51 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000052 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000053 va_end(va);
54 return retval;
55}
56
57
58int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000059PyArg_VaParse(PyObject *args, char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000060{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061 va_list lva;
62
63#ifdef VA_LIST_IS_ARRAY
64 memcpy(lva, va, sizeof(va_list));
65#else
66 lva = va;
67#endif
68
69 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000070}
71
72
73static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000074vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000075{
76 char msgbuf[256];
77 int levels[32];
78 char *fname = NULL;
79 char *message = NULL;
80 int min = -1;
81 int max = 0;
82 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000083 int endfmt = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000084 char *formatsave = format;
85 int i, len;
86 char *msg;
87
Tim Peters5c4d5bf2001-02-12 22:13:26 +000088 assert(compat || (args != (PyObject*)NULL));
89
Jeremy Hylton25916bd2001-05-29 17:46:19 +000090 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000091 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000092 switch (c) {
93 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000094 if (level == 0)
95 max++;
96 level++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000097 break;
98 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000099 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000100 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000101 else
102 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000103 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000104 case '\0':
105 endfmt = 1;
106 break;
107 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000108 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000109 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000110 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000111 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000112 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000113 endfmt = 1;
114 break;
115 default:
116 if (level == 0) {
117 if (c == 'O')
118 max++;
119 else if (isalpha(c)) {
120 if (c != 'e') /* skip encoded */
121 max++;
122 } else if (c == '|')
123 min = max;
124 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000125 break;
126 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000127 }
128
129 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000130 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000131
132 if (min < 0)
133 min = max;
134
135 format = formatsave;
136
137 if (compat) {
138 if (max == 0) {
139 if (args == NULL)
140 return 1;
Ka-Ping Yee20579702001-01-15 22:14:16 +0000141 sprintf(msgbuf, "%s%s takes no arguments",
142 fname==NULL ? "function" : fname,
143 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000145 return 0;
146 }
147 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000148 if (args == NULL) {
149 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000150 "%s%s takes at least one argument",
151 fname==NULL ? "function" : fname,
152 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000153 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000154 return 0;
155 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000156 msg = convertitem(args, &format, p_va, levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000157 if (msg == NULL)
158 return 1;
159 seterror(levels[0], msg, levels+1, fname, message);
160 return 0;
161 }
162 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000164 "old style getargs format uses new features");
165 return 0;
166 }
167 }
168
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 if (!PyTuple_Check(args)) {
170 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000171 "new style getargs format but argument is not a tuple");
172 return 0;
173 }
174
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000175 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000176
177 if (len < min || max < len) {
178 if (message == NULL) {
179 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000180 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000181 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000182 fname==NULL ? "" : "()",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000183 min==max ? "exactly"
184 : len < min ? "at least" : "at most",
185 len < min ? min : max,
186 (len < min ? min : max) == 1 ? "" : "s",
187 len);
188 message = msgbuf;
189 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000190 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000191 return 0;
192 }
193
194 for (i = 0; i < len; i++) {
195 if (*format == '|')
196 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000197 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
198 levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000199 if (msg) {
200 seterror(i+1, msg, levels, fname, message);
201 return 0;
202 }
203 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000204
Guido van Rossum730806d1998-04-10 22:27:42 +0000205 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000206 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000207 *format != '|' && *format != ':' && *format != ';') {
208 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000209 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000210 return 0;
211 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000212
213 return 1;
214}
215
216
217
218static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000220{
221 char buf[256];
222 int i;
223 char *p = buf;
224
Guido van Rossum79f25d91997-04-29 20:08:16 +0000225 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000226 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000227 else if (message == NULL) {
228 if (fname != NULL) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000229 sprintf(p, "%s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000230 p += strlen(p);
231 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000232 if (iarg != 0) {
233 sprintf(p, "argument %d", iarg);
234 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000235 p += strlen(p);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000236 while (levels[i] > 0) {
237 sprintf(p, ", item %d", levels[i]-1);
238 p += strlen(p);
239 i++;
240 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000241 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000242 else {
243 sprintf(p, "argument");
244 p += strlen(p);
245 }
246 sprintf(p, " %s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000247 message = buf;
248 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000249 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000250}
251
252
253/* Convert a tuple argument.
254 On entry, *p_format points to the character _after_ the opening '('.
255 On successful exit, *p_format points to the closing ')'.
256 If successful:
257 *p_format and *p_va are updated,
258 *levels and *msgbuf are untouched,
259 and NULL is returned.
260 If the argument is invalid:
261 *p_format is unchanged,
262 *p_va is undefined,
263 *levels is a 0-terminated list of item numbers,
264 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000265 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000266 <typename1> is the name of the expected type, and
267 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000268 and msgbuf is returned.
269*/
270
271static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000272converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
273 char *msgbuf, int toplevel)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000274{
275 int level = 0;
276 int n = 0;
277 char *format = *p_format;
278 int i;
279
280 for (;;) {
281 int c = *format++;
282 if (c == '(') {
283 if (level == 0)
284 n++;
285 level++;
286 }
287 else if (c == ')') {
288 if (level == 0)
289 break;
290 level--;
291 }
292 else if (c == ':' || c == ';' || c == '\0')
293 break;
294 else if (level == 0 && isalpha(c))
295 n++;
296 }
297
Ka-Ping Yee20579702001-01-15 22:14:16 +0000298 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000299 levels[0] = 0;
300 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000301 toplevel ? "expected %d arguments, not %s" :
302 "must be %d-item sequence, not %s",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000303 n, arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000304 return msgbuf;
305 }
306
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000307 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000308 levels[0] = 0;
309 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000310 toplevel ? "expected %d arguments, not %d" :
311 "must be sequence of length %d, not %d",
312 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000313 return msgbuf;
314 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000315
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000316 format = *p_format;
317 for (i = 0; i < n; i++) {
318 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000319 PyObject *item;
320 item = PySequence_GetItem(arg, i);
321 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
322 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
323 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000324 if (msg != NULL) {
325 levels[0] = i+1;
326 return msg;
327 }
328 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000329
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000330 *p_format = format;
331 return NULL;
332}
333
334
335/* Convert a single item. */
336
337static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000338convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
339 char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000340{
341 char *msg;
342 char *format = *p_format;
343
344 if (*format == '(' /* ')' */) {
345 format++;
346 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
347 if (msg == NULL)
348 format++;
349 }
350 else {
351 msg = convertsimple(arg, &format, p_va, msgbuf);
352 if (msg != NULL)
353 levels[0] = 0;
354 }
355 if (msg == NULL)
356 *p_format = format;
357 return msg;
358}
359
360
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000361
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000362/* Internal API needed by convertsimple() and a helper macro. */
Guido van Rossum700c6ff2000-04-27 20:13:18 +0000363extern
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000364PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
Guido van Rossum700c6ff2000-04-27 20:13:18 +0000365 const char *errors);
366
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000367#define UNICODE_DEFAULT_ENCODING(arg) \
368 _PyUnicode_AsDefaultEncodedString(arg, NULL)
369
370/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000371
372static char *
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000373converterr(char *expected, PyObject *arg, char *msgbuf)
374{
375 assert (expected != NULL);
376 sprintf(msgbuf, "must be %.50s, not %.50s", expected,
377 arg == Py_None ? "None" : arg->ob_type->tp_name);
378 return msgbuf;
379}
380
381#define CONV_UNICODE "(unicode conversion error)"
382
383/* Convert a non-tuple argument. Return NULL if conversion went OK,
384 or a string with a message describing the failure. The message is
385 formatted as "must be <desired type>, not <actual type>".
386 When failing, an exception may or may not have been raised.
387 Don't call if a tuple is expected.
388*/
389
390static char *
391convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000392{
393 char *format = *p_format;
394 char c = *format++;
395
396 switch (c) {
397
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000398 case 'b': { /* unsigned byte -- very short int */
399 char *p = va_arg(*p_va, char *);
400 long ival = PyInt_AsLong(arg);
401 if (ival == -1 && PyErr_Occurred())
402 return converterr("integer<b>", arg, msgbuf);
403 else if (ival < 0) {
404 PyErr_SetString(PyExc_OverflowError,
405 "unsigned byte integer is less than minimum");
406 return converterr("integer<b>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000407 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000408 else if (ival > UCHAR_MAX) {
409 PyErr_SetString(PyExc_OverflowError,
410 "unsigned byte integer is greater than maximum");
411 return converterr("integer<b>", arg, msgbuf);
412 }
413 else
414 *p = (unsigned char) ival;
415 break;
416 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000417
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000418 case 'B': {/* byte sized bitfield - both signed and unsigned
419 values allowed */
420 char *p = va_arg(*p_va, char *);
421 long ival = PyInt_AsLong(arg);
422 if (ival == -1 && PyErr_Occurred())
423 return converterr("integer<b>", arg, msgbuf);
424 else if (ival < SCHAR_MIN) {
425 PyErr_SetString(PyExc_OverflowError,
426 "byte-sized integer bitfield is less than minimum");
427 return converterr("integer<B>", arg, msgbuf);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000428 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000429 else if (ival > (int)UCHAR_MAX) {
430 PyErr_SetString(PyExc_OverflowError,
431 "byte-sized integer bitfield is greater than maximum");
432 return converterr("integer<B>", arg, msgbuf);
433 }
434 else
435 *p = (unsigned char) ival;
436 break;
437 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000438
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000439 case 'h': {/* signed short int */
440 short *p = va_arg(*p_va, short *);
441 long ival = PyInt_AsLong(arg);
442 if (ival == -1 && PyErr_Occurred())
443 return converterr("integer<h>", arg, msgbuf);
444 else if (ival < SHRT_MIN) {
445 PyErr_SetString(PyExc_OverflowError,
446 "signed short integer is less than minimum");
447 return converterr("integer<h>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000448 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000449 else if (ival > SHRT_MAX) {
450 PyErr_SetString(PyExc_OverflowError,
451 "signed short integer is greater than maximum");
452 return converterr("integer<h>", arg, msgbuf);
453 }
454 else
455 *p = (short) ival;
456 break;
457 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000458
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000459 case 'H': { /* short int sized bitfield, both signed and
460 unsigned allowed */
461 unsigned short *p = va_arg(*p_va, unsigned short *);
462 long ival = PyInt_AsLong(arg);
463 if (ival == -1 && PyErr_Occurred())
464 return converterr("integer<H>", arg, msgbuf);
465 else if (ival < SHRT_MIN) {
466 PyErr_SetString(PyExc_OverflowError,
467 "short integer bitfield is less than minimum");
468 return converterr("integer<H>", arg, msgbuf);
Jack Jansend50338f2000-07-06 12:22:00 +0000469 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000470 else if (ival > USHRT_MAX) {
471 PyErr_SetString(PyExc_OverflowError,
472 "short integer bitfield is greater than maximum");
473 return converterr("integer<H>", arg, msgbuf);
474 }
475 else
476 *p = (unsigned short) ival;
477 break;
478 }
Jack Jansend50338f2000-07-06 12:22:00 +0000479
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000480 case 'i': {/* signed int */
481 int *p = va_arg(*p_va, int *);
482 long ival = PyInt_AsLong(arg);
483 if (ival == -1 && PyErr_Occurred())
484 return converterr("integer<i>", arg, msgbuf);
485 else if (ival > INT_MAX) {
486 PyErr_SetString(PyExc_OverflowError,
487 "signed integer is greater than maximum");
488 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000489 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000490 else if (ival < INT_MIN) {
491 PyErr_SetString(PyExc_OverflowError,
492 "signed integer is less than minimum");
493 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000494 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000495 else
496 *p = ival;
497 break;
498 }
499
500 case 'l': {/* long int */
501 long *p = va_arg(*p_va, long *);
502 long ival = PyInt_AsLong(arg);
503 if (ival == -1 && PyErr_Occurred())
504 return converterr("integer<l>", arg, msgbuf);
505 else
506 *p = ival;
507 break;
508 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000509
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000510#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000511 case 'L': {/* LONG_LONG */
512 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
513 LONG_LONG ival = PyLong_AsLongLong( arg );
514 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
515 return converterr("long<L>", arg, msgbuf);
516 } else {
517 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000518 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000519 break;
520 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000521#endif
522
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000523 case 'f': {/* float */
524 float *p = va_arg(*p_va, float *);
525 double dval = PyFloat_AsDouble(arg);
526 if (PyErr_Occurred())
527 return converterr("float<f>", arg, msgbuf);
528 else
529 *p = (float) dval;
530 break;
531 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000532
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000533 case 'd': {/* double */
534 double *p = va_arg(*p_va, double *);
535 double dval = PyFloat_AsDouble(arg);
536 if (PyErr_Occurred())
537 return converterr("float<d>", arg, msgbuf);
538 else
539 *p = dval;
540 break;
541 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000542
Guido van Rossum530956d1996-07-21 02:27:43 +0000543#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000544 case 'D': {/* complex double */
545 Py_complex *p = va_arg(*p_va, Py_complex *);
546 Py_complex cval;
547 cval = PyComplex_AsCComplex(arg);
548 if (PyErr_Occurred())
549 return converterr("complex<D>", arg, msgbuf);
550 else
551 *p = cval;
552 break;
553 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000554#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000555
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000556 case 'c': {/* char */
557 char *p = va_arg(*p_va, char *);
558 if (PyString_Check(arg) && PyString_Size(arg) == 1)
559 *p = PyString_AsString(arg)[0];
560 else
561 return converterr("char", arg, msgbuf);
562 break;
563 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000564
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000565 case 's': {/* string */
566 if (*format == '#') {
567 void **p = (void **)va_arg(*p_va, char **);
568 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000569
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000570 if (PyString_Check(arg)) {
571 *p = PyString_AS_STRING(arg);
572 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000573 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000574 else if (PyUnicode_Check(arg)) {
575 arg = UNICODE_DEFAULT_ENCODING(arg);
576 if (arg == NULL)
577 return converterr(CONV_UNICODE,
578 arg, msgbuf);
579 *p = PyString_AS_STRING(arg);
580 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000581 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000582 else { /* any buffer-like object */
583 char *buf;
584 int count = convertbuffer(arg, p, &buf);
585 if (count < 0)
586 return converterr(buf, arg, msgbuf);
587 *q = count;
588 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000589 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000590 } else {
591 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000592
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000593 if (PyString_Check(arg))
594 *p = PyString_AS_STRING(arg);
595 else if (PyUnicode_Check(arg)) {
596 arg = UNICODE_DEFAULT_ENCODING(arg);
597 if (arg == NULL)
598 return converterr(CONV_UNICODE,
599 arg, msgbuf);
600 *p = PyString_AS_STRING(arg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000601 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000602 else
603 return converterr("string", arg, msgbuf);
604 if ((int)strlen(*p) != PyString_Size(arg))
605 return converterr("string without null bytes",
606 arg, msgbuf);
607 }
608 break;
609 }
610
611 case 'z': {/* string, may be NULL (None) */
612 if (*format == '#') { /* any buffer-like object */
613 void **p = (void **)va_arg(*p_va, char **);
614 int *q = va_arg(*p_va, int *);
615
616 if (arg == Py_None) {
617 *p = 0;
618 *q = 0;
619 }
620 else if (PyString_Check(arg)) {
621 *p = PyString_AS_STRING(arg);
622 *q = PyString_GET_SIZE(arg);
623 }
624 else if (PyUnicode_Check(arg)) {
625 arg = UNICODE_DEFAULT_ENCODING(arg);
626 if (arg == NULL)
627 return converterr(CONV_UNICODE,
628 arg, msgbuf);
629 *p = PyString_AS_STRING(arg);
630 *q = PyString_GET_SIZE(arg);
631 }
632 else { /* any buffer-like object */
633 char *buf;
634 int count = convertbuffer(arg, p, &buf);
635
636 if (count < 0)
637 return converterr(buf, arg, msgbuf);
638 *q = count;
639 }
640 format++;
641 } else {
642 char **p = va_arg(*p_va, char **);
643
644 if (arg == Py_None)
645 *p = 0;
646 else if (PyString_Check(arg))
647 *p = PyString_AsString(arg);
648 else if (PyUnicode_Check(arg)) {
649 arg = UNICODE_DEFAULT_ENCODING(arg);
650 if (arg == NULL)
651 return converterr(CONV_UNICODE,
652 arg, msgbuf);
653 *p = PyString_AS_STRING(arg);
654 }
655 else
656 return converterr("string or None",
657 arg, msgbuf);
658 if (*format == '#') {
659 int *q = va_arg(*p_va, int *);
660 if (arg == Py_None)
661 *q = 0;
662 else
663 *q = PyString_Size(arg);
664 format++;
665 }
666 else if (*p != NULL &&
667 (int)strlen(*p) != PyString_Size(arg))
668 return converterr(
669 "string without null bytes or None",
670 arg, msgbuf);
671 }
672 break;
673 }
674
675 case 'e': {/* encoded string */
676 char **buffer;
677 const char *encoding;
678 PyObject *u, *s;
679 int size, recode_strings;
680
681 /* Get 'e' parameter: the encoding name */
682 encoding = (const char *)va_arg(*p_va, const char *);
683 if (encoding == NULL)
684 encoding = PyUnicode_GetDefaultEncoding();
685
686 /* Get output buffer parameter:
687 's' (recode all objects via Unicode) or
688 't' (only recode non-string objects)
689 */
690 if (*format == 's')
691 recode_strings = 1;
692 else if (*format == 't')
693 recode_strings = 0;
694 else
695 return converterr(
696 "(unknown parser marker combination)",
697 arg, msgbuf);
698 buffer = (char **)va_arg(*p_va, char **);
699 format++;
700 if (buffer == NULL)
701 return converterr("(buffer is NULL)",
702 arg, msgbuf);
703
704 /* Encode object */
705 if (!recode_strings && PyString_Check(arg)) {
706 s = arg;
707 Py_INCREF(s);
708 }
709 else {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000710 /* Convert object to Unicode */
711 u = PyUnicode_FromObject(arg);
712 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000713 return converterr(
714 "string or unicode or text buffer",
715 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000716
717 /* Encode object; use default error handling */
718 s = PyUnicode_AsEncodedString(u,
719 encoding,
720 NULL);
721 Py_DECREF(u);
722 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000723 return converterr("(encoding failed)",
724 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000725 if (!PyString_Check(s)) {
726 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000727 return converterr(
728 "(encoder failed to return a string)",
729 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000730 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000731 }
732 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000733
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000734 /* Write output; output is guaranteed to be 0-terminated */
735 if (*format == '#') {
736 /* Using buffer length parameter '#':
737
738 - if *buffer is NULL, a new buffer of the
739 needed size is allocated and the data
740 copied into it; *buffer is updated to point
741 to the new buffer; the caller is
742 responsible for PyMem_Free()ing it after
743 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000744
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000745 - if *buffer is not NULL, the data is
746 copied to *buffer; *buffer_len has to be
747 set to the size of the buffer on input;
748 buffer overflow is signalled with an error;
749 buffer has to provide enough room for the
750 encoded string plus the trailing 0-byte
751
752 - in both cases, *buffer_len is updated to
753 the size of the buffer /excluding/ the
754 trailing 0-byte
755
756 */
757 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000758
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000759 format++;
760 if (buffer_len == NULL)
761 return converterr(
762 "(buffer_len is NULL)",
763 arg, msgbuf);
764 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000765 *buffer = PyMem_NEW(char, size + 1);
766 if (*buffer == NULL) {
767 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000768 return converterr(
769 "(memory error)",
770 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000771 }
Fred Drake25871c02000-05-03 15:17:02 +0000772 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000773 if (size + 1 > *buffer_len) {
774 Py_DECREF(s);
775 return converterr(
776 "(buffer overflow)",
777 arg, msgbuf);
778 }
Fred Drake25871c02000-05-03 15:17:02 +0000779 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000780 memcpy(*buffer,
781 PyString_AS_STRING(s),
782 size + 1);
783 *buffer_len = size;
784 } else {
785 /* Using a 0-terminated buffer:
786
787 - the encoded string has to be 0-terminated
788 for this variant to work; if it is not, an
789 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000790
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000791 - a new buffer of the needed size is
792 allocated and the data copied into it;
793 *buffer is updated to point to the new
794 buffer; the caller is responsible for
795 PyMem_Free()ing it after usage
796
797 */
798 if ((int)strlen(PyString_AS_STRING(s)) != size)
799 return converterr(
800 "(encoded string without NULL bytes)",
801 arg, msgbuf);
802 *buffer = PyMem_NEW(char, size + 1);
803 if (*buffer == NULL) {
804 Py_DECREF(s);
805 return converterr("(memory error)",
806 arg, msgbuf);
807 }
808 memcpy(*buffer,
809 PyString_AS_STRING(s),
810 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000811 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000812 Py_DECREF(s);
813 break;
814 }
815
816 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
817 if (*format == '#') { /* any buffer-like object */
818 void **p = (void **)va_arg(*p_va, char **);
819 int *q = va_arg(*p_va, int *);
820 char *buf;
821 int count = convertbuffer(arg, p, &buf);
822
823 if (count < 0)
824 return converterr(buf, arg, msgbuf);
825 *q = count/(sizeof(Py_UNICODE));
826 format++;
827 } else {
828 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
829
Guido van Rossume826ef02000-03-10 23:02:17 +0000830 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000831 *p = PyUnicode_AS_UNICODE(arg);
832 else
833 return converterr("unicode", arg, msgbuf);
834 }
835 break;
836 }
837
838 case 'S': { /* string object */
839 PyObject **p = va_arg(*p_va, PyObject **);
840 if (PyString_Check(arg))
841 *p = arg;
842 else
843 return converterr("string", arg, msgbuf);
844 break;
845 }
846
847 case 'U': { /* Unicode object */
848 PyObject **p = va_arg(*p_va, PyObject **);
849 if (PyUnicode_Check(arg))
850 *p = arg;
851 else
852 return converterr("unicode", arg, msgbuf);
853 break;
854 }
855
856 case 'O': { /* object */
857 PyTypeObject *type;
858 PyObject **p;
859 if (*format == '!') {
860 type = va_arg(*p_va, PyTypeObject*);
861 p = va_arg(*p_va, PyObject **);
862 format++;
863 if (arg->ob_type == type)
Guido van Rossume826ef02000-03-10 23:02:17 +0000864 *p = arg;
865 else
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000866 return converterr(type->tp_name, arg, msgbuf);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000867
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000868 }
869 else if (*format == '?') {
870 inquiry pred = va_arg(*p_va, inquiry);
871 p = va_arg(*p_va, PyObject **);
872 format++;
873 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000874 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000875 else
876 return converterr("(unspecified)",
877 arg, msgbuf);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000878
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000879 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000880 else if (*format == '&') {
881 typedef int (*converter)(PyObject *, void *);
882 converter convert = va_arg(*p_va, converter);
883 void *addr = va_arg(*p_va, void *);
884 format++;
885 if (! (*convert)(arg, addr))
886 return converterr("(unspecified)",
887 arg, msgbuf);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000888 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000889 else {
890 p = va_arg(*p_va, PyObject **);
891 *p = arg;
892 }
893 break;
894 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000895
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000896
897 case 'w': { /* memory buffer, read-write access */
898 void **p = va_arg(*p_va, void **);
899 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
900 int count;
901
902 if (pb == NULL ||
903 pb->bf_getwritebuffer == NULL ||
904 pb->bf_getsegcount == NULL)
905 return converterr("read-write buffer", arg, msgbuf);
906 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
907 return converterr("single-segment read-write buffer",
908 arg, msgbuf);
909 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
910 return converterr("(unspecified)", arg, msgbuf);
911 if (*format == '#') {
912 int *q = va_arg(*p_va, int *);
913
914 *q = count;
915 format++;
916 }
917 break;
918 }
919
920 case 't': { /* 8-bit character buffer, read-only access */
921 const char **p = va_arg(*p_va, const char **);
922 char *buf;
923 int count;
924
925 if (*format++ != '#')
926 return converterr(
927 "invalid use of 't' format character",
928 arg, msgbuf);
929 if (!PyType_HasFeature(arg->ob_type,
930 Py_TPFLAGS_HAVE_GETCHARBUFFER))
931 return converterr(
932 "string or read-only character buffer",
933 arg, msgbuf);
934
935 count = convertbuffer(arg, (void **)p, &buf);
936 if (count < 0)
937 return converterr(buf, arg, msgbuf);
938 *va_arg(*p_va, int *) = count;
939 break;
940 }
941
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000942 default:
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000943 return converterr("impossible<bad format char>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000944
945 }
946
947 *p_format = format;
948 return NULL;
949}
Guido van Rossumaa354651996-08-19 19:32:04 +0000950
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000951int convertbuffer(PyObject *arg, void **p, char **errmsg)
952{
953 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
954 int count;
955 if (pb == NULL ||
956 pb->bf_getreadbuffer == NULL ||
957 pb->bf_getsegcount == NULL) {
958 *errmsg = "string or read-only buffer";
959 return -1;
960 }
961 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
962 *errmsg = "string or single-segment read-only buffer";
963 return -1;
964 }
965 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
966 *errmsg = "(unspecified)";
967 }
968 return count;
969}
Guido van Rossumaa354651996-08-19 19:32:04 +0000970
971/* Support for keyword arguments donated by
972 Geoff Philbrick <philbric@delphi.hks.com> */
973
Guido van Rossum79f25d91997-04-29 20:08:16 +0000974int PyArg_ParseTupleAndKeywords(PyObject *args,
975 PyObject *keywords,
Guido van Rossumaa354651996-08-19 19:32:04 +0000976 char *format,
977 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +0000978{
979 int retval;
980 va_list va;
Guido van Rossumaa354651996-08-19 19:32:04 +0000981
982 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +0000983 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
984 va_end(va);
985 return retval;
986}
987
988
989static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000990vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
991 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +0000992{
993 char msgbuf[256];
994 int levels[32];
995 char *fname = NULL;
996 char *message = NULL;
997 int min = -1;
998 int max = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +0000999 char *formatsave = format;
1000 int i, len, tplen, kwlen;
1001 char *msg, *ks, **p;
1002 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001003 PyObject *key, *value;
Guido van Rossumaa354651996-08-19 19:32:04 +00001004
1005 /* nested tuples cannot be parsed when using keyword arguments */
1006
1007 for (;;) {
1008 int c = *format++;
1009 if (c == '(') {
1010 PyErr_SetString(PyExc_SystemError,
1011 "tuple found in format when using keyword arguments");
1012 return 0;
1013 }
1014 else if (c == '\0')
1015 break;
1016 else if (c == ':') {
1017 fname = format;
1018 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001019 } else if (c == ';') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001020 message = format;
1021 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001022 } else if (c == 'e')
Marc-André Lemburgbbcf2a72000-09-08 11:49:37 +00001023 ; /* Pass */
Guido van Rossumaa354651996-08-19 19:32:04 +00001024 else if (isalpha(c))
1025 max++;
1026 else if (c == '|')
1027 min = max;
1028 }
1029
1030 if (min < 0)
1031 min = max;
1032
1033 format = formatsave;
1034
1035 if (!PyTuple_Check(args)) {
1036 PyErr_SetString(PyExc_SystemError,
1037 "new style getargs format but argument is not a tuple");
1038 return 0;
1039 }
1040
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001041 tplen = PyTuple_GET_SIZE(args);
Guido van Rossumaa354651996-08-19 19:32:04 +00001042
1043 /* do a cursory check of the keywords just to see how many we got */
1044
1045 if (keywords) {
1046 if (!PyDict_Check(keywords)) {
Jeremy Hyltona0ac40c2001-01-25 20:13:10 +00001047 if (keywords == NULL)
1048 PyErr_SetString(PyExc_SystemError,
1049 "NULL received when keyword dictionary expected");
1050 else
1051 PyErr_Format(PyExc_SystemError,
1052 "%s received when keyword dictionary expected",
1053 keywords->ob_type->tp_name);
Guido van Rossumaa354651996-08-19 19:32:04 +00001054 return 0;
1055 }
1056 kwlen = PyDict_Size(keywords);
1057 }
1058 else {
1059 kwlen = 0;
1060 }
1061
1062 /* make sure there are no duplicate values for an argument;
1063 its not clear when to use the term "keyword argument vs.
1064 keyword parameter in messages */
1065
1066 if (keywords) {
1067 for (i = 0; i < tplen; i++) {
1068 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1069 sprintf(msgbuf,
1070 "keyword parameter %s redefined",
1071 kwlist[i]);
1072 PyErr_SetString(PyExc_TypeError, msgbuf);
1073 return 0;
1074 }
1075 }
1076 }
1077 PyErr_Clear(); /* I'm not which Py functions set the error string */
1078
1079 /* required arguments missing from args can be supplied by keyword
1080 arguments */
1081
1082 len = tplen;
1083 if (keywords && tplen < min) {
1084 for (i = tplen; i < min; i++) {
1085 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1086 len++;
1087 }
1088 }
1089 }
1090 PyErr_Clear();
1091
1092 /* make sure we got an acceptable number of arguments; the message
1093 is a little confusing with keywords since keyword arguments
1094 which are supplied, but don't match the required arguments
1095 are not included in the "%d given" part of the message */
1096
1097 if (len < min || max < len) {
1098 if (message == NULL) {
1099 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001100 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001101 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001102 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001103 min==max ? "exactly"
1104 : len < min ? "at least" : "at most",
1105 len < min ? min : max,
1106 (len < min ? min : max) == 1 ? "" : "s",
1107 len);
1108 message = msgbuf;
1109 }
1110 PyErr_SetString(PyExc_TypeError, message);
1111 return 0;
1112 }
1113
1114 for (i = 0; i < tplen; i++) {
1115 if (*format == '|')
1116 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001117 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001118 levels, msgbuf);
1119 if (msg) {
1120 seterror(i+1, msg, levels, fname, message);
1121 return 0;
1122 }
1123 }
1124
1125 /* handle no keyword parameters in call */
1126
1127 if (!keywords) return 1;
1128
1129 /* make sure the number of keywords in the keyword list matches the
1130 number of items in the format string */
1131
1132 nkwds = 0;
1133 p = kwlist;
1134 for (;;) {
1135 if (!*(p++)) break;
1136 nkwds++;
1137 }
1138
1139 if (nkwds != max) {
1140 PyErr_SetString(PyExc_SystemError,
1141 "number of items in format string and keyword list do not match");
1142 return 0;
1143 }
1144
1145 /* convert the keyword arguments; this uses the format
1146 string where it was left after processing args */
1147
1148 converted = 0;
1149 for (i = tplen; i < nkwds; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001151 if (*format == '|')
1152 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001153 item = PyMapping_GetItemString(keywords, kwlist[i]);
1154 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001155 msg = convertitem(item, &format, p_va, levels, msgbuf);
1156 if (msg) {
1157 seterror(i+1, msg, levels, fname, message);
1158 return 0;
1159 }
1160 converted++;
Barry Warsaw07050282000-12-11 20:01:55 +00001161 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001162 }
1163 else {
1164 PyErr_Clear();
1165 msg = skipitem(&format, p_va);
1166 if (msg) {
1167 seterror(i+1, msg, levels, fname, message);
1168 return 0;
1169 }
1170 }
1171 }
1172
1173 /* make sure there are no extraneous keyword arguments */
1174
1175 pos = 0;
1176 if (converted < kwlen) {
1177 while (PyDict_Next(keywords, &pos, &key, &value)) {
1178 match = 0;
1179 ks = PyString_AsString(key);
1180 for (i = 0; i < nkwds; i++) {
1181 if (!strcmp(ks, kwlist[i])) {
1182 match = 1;
1183 break;
1184 }
1185 }
1186 if (!match) {
1187 sprintf(msgbuf,
Guido van Rossum80dc16b2000-05-08 14:02:41 +00001188 "%s is an invalid keyword argument for this function",
Guido van Rossumaa354651996-08-19 19:32:04 +00001189 ks);
1190 PyErr_SetString(PyExc_TypeError, msgbuf);
1191 return 0;
1192 }
1193 }
1194 }
1195
1196 return 1;
1197}
1198
1199
1200static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001201skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001202{
1203 char *format = *p_format;
1204 char c = *format++;
1205
1206 switch (c) {
1207
1208 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001209 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001210 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001211 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001212 break;
1213 }
1214
1215 case 'h': /* short int */
1216 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001217 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001218 break;
1219 }
1220
Jack Jansencc22fbe2000-08-05 21:29:58 +00001221 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001222 {
1223 (void) va_arg(*p_va, unsigned short *);
1224 break;
1225 }
1226
Guido van Rossumaa354651996-08-19 19:32:04 +00001227 case 'i': /* int */
1228 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001229 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001230 break;
1231 }
1232
1233 case 'l': /* long int */
1234 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001235 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001236 break;
1237 }
1238
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001239#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001240 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001241 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001242 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001243 break;
1244 }
1245#endif
1246
Guido van Rossumaa354651996-08-19 19:32:04 +00001247 case 'f': /* float */
1248 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001249 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001250 break;
1251 }
1252
1253 case 'd': /* double */
1254 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001255 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001256 break;
1257 }
1258
1259#ifndef WITHOUT_COMPLEX
1260 case 'D': /* complex double */
1261 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001262 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001263 break;
1264 }
1265#endif /* WITHOUT_COMPLEX */
1266
1267 case 'c': /* char */
1268 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001269 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001270 break;
1271 }
1272
1273 case 's': /* string */
1274 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001275 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001276 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001277 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001278 format++;
1279 }
1280 break;
1281 }
1282
1283 case 'z': /* string */
1284 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001285 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001286 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001287 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001288 format++;
1289 }
1290 break;
1291 }
1292
1293 case 'S': /* string object */
1294 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001295 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001296 break;
1297 }
1298
1299 case 'O': /* object */
1300 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001301 if (*format == '!') {
1302 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001303 (void) va_arg(*p_va, PyTypeObject*);
1304 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001305 }
1306#if 0
1307/* I don't know what this is for */
1308 else if (*format == '?') {
1309 inquiry pred = va_arg(*p_va, inquiry);
1310 format++;
1311 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001312 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001313 }
1314 }
1315#endif
1316 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001317 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001318 (void) va_arg(*p_va, converter);
1319 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001320 format++;
1321 }
1322 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001323 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001324 }
1325 break;
1326 }
1327
1328 default:
1329 return "impossible<bad format char>";
1330
1331 }
1332
1333 *p_format = format;
1334 return NULL;
1335}