blob: 9f76ac0b2ff9e074c2d7a173d1357ea8d85b5e5a [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#define UNICODE_DEFAULT_ENCODING(arg) \
363 _PyUnicode_AsDefaultEncodedString(arg, NULL)
364
365/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000366
367static char *
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000368converterr(char *expected, PyObject *arg, char *msgbuf)
369{
370 assert (expected != NULL);
371 sprintf(msgbuf, "must be %.50s, not %.50s", expected,
372 arg == Py_None ? "None" : arg->ob_type->tp_name);
373 return msgbuf;
374}
375
376#define CONV_UNICODE "(unicode conversion error)"
377
378/* Convert a non-tuple argument. Return NULL if conversion went OK,
379 or a string with a message describing the failure. The message is
380 formatted as "must be <desired type>, not <actual type>".
381 When failing, an exception may or may not have been raised.
382 Don't call if a tuple is expected.
383*/
384
385static char *
386convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000387{
388 char *format = *p_format;
389 char c = *format++;
390
391 switch (c) {
392
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000393 case 'b': { /* unsigned byte -- very short int */
394 char *p = va_arg(*p_va, char *);
395 long ival = PyInt_AsLong(arg);
396 if (ival == -1 && PyErr_Occurred())
397 return converterr("integer<b>", arg, msgbuf);
398 else if (ival < 0) {
399 PyErr_SetString(PyExc_OverflowError,
400 "unsigned byte integer is less than minimum");
401 return converterr("integer<b>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000402 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000403 else if (ival > UCHAR_MAX) {
404 PyErr_SetString(PyExc_OverflowError,
405 "unsigned byte integer is greater than maximum");
406 return converterr("integer<b>", arg, msgbuf);
407 }
408 else
409 *p = (unsigned char) ival;
410 break;
411 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000412
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000413 case 'B': {/* byte sized bitfield - both signed and unsigned
414 values allowed */
415 char *p = va_arg(*p_va, char *);
416 long ival = PyInt_AsLong(arg);
417 if (ival == -1 && PyErr_Occurred())
418 return converterr("integer<b>", arg, msgbuf);
419 else if (ival < SCHAR_MIN) {
420 PyErr_SetString(PyExc_OverflowError,
421 "byte-sized integer bitfield is less than minimum");
422 return converterr("integer<B>", arg, msgbuf);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000423 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000424 else if (ival > (int)UCHAR_MAX) {
425 PyErr_SetString(PyExc_OverflowError,
426 "byte-sized integer bitfield is greater than maximum");
427 return converterr("integer<B>", arg, msgbuf);
428 }
429 else
430 *p = (unsigned char) ival;
431 break;
432 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000433
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000434 case 'h': {/* signed short int */
435 short *p = va_arg(*p_va, short *);
436 long ival = PyInt_AsLong(arg);
437 if (ival == -1 && PyErr_Occurred())
438 return converterr("integer<h>", arg, msgbuf);
439 else if (ival < SHRT_MIN) {
440 PyErr_SetString(PyExc_OverflowError,
441 "signed short integer is less than minimum");
442 return converterr("integer<h>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000443 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000444 else if (ival > SHRT_MAX) {
445 PyErr_SetString(PyExc_OverflowError,
446 "signed short integer is greater than maximum");
447 return converterr("integer<h>", arg, msgbuf);
448 }
449 else
450 *p = (short) ival;
451 break;
452 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000453
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000454 case 'H': { /* short int sized bitfield, both signed and
455 unsigned allowed */
456 unsigned short *p = va_arg(*p_va, unsigned short *);
457 long ival = PyInt_AsLong(arg);
458 if (ival == -1 && PyErr_Occurred())
459 return converterr("integer<H>", arg, msgbuf);
460 else if (ival < SHRT_MIN) {
461 PyErr_SetString(PyExc_OverflowError,
462 "short integer bitfield is less than minimum");
463 return converterr("integer<H>", arg, msgbuf);
Jack Jansend50338f2000-07-06 12:22:00 +0000464 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000465 else if (ival > USHRT_MAX) {
466 PyErr_SetString(PyExc_OverflowError,
467 "short integer bitfield is greater than maximum");
468 return converterr("integer<H>", arg, msgbuf);
469 }
470 else
471 *p = (unsigned short) ival;
472 break;
473 }
Jack Jansend50338f2000-07-06 12:22:00 +0000474
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000475 case 'i': {/* signed int */
476 int *p = va_arg(*p_va, int *);
477 long ival = PyInt_AsLong(arg);
478 if (ival == -1 && PyErr_Occurred())
479 return converterr("integer<i>", arg, msgbuf);
480 else if (ival > INT_MAX) {
481 PyErr_SetString(PyExc_OverflowError,
482 "signed integer is greater than maximum");
483 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000484 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000485 else if (ival < INT_MIN) {
486 PyErr_SetString(PyExc_OverflowError,
487 "signed integer is less than minimum");
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
491 *p = ival;
492 break;
493 }
494
495 case 'l': {/* long int */
496 long *p = va_arg(*p_va, long *);
497 long ival = PyInt_AsLong(arg);
498 if (ival == -1 && PyErr_Occurred())
499 return converterr("integer<l>", arg, msgbuf);
500 else
501 *p = ival;
502 break;
503 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000504
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000505#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000506 case 'L': {/* LONG_LONG */
507 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
508 LONG_LONG ival = PyLong_AsLongLong( arg );
509 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
510 return converterr("long<L>", arg, msgbuf);
511 } else {
512 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000513 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000514 break;
515 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000516#endif
517
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000518 case 'f': {/* float */
519 float *p = va_arg(*p_va, float *);
520 double dval = PyFloat_AsDouble(arg);
521 if (PyErr_Occurred())
522 return converterr("float<f>", arg, msgbuf);
523 else
524 *p = (float) dval;
525 break;
526 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000527
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000528 case 'd': {/* double */
529 double *p = va_arg(*p_va, double *);
530 double dval = PyFloat_AsDouble(arg);
531 if (PyErr_Occurred())
532 return converterr("float<d>", arg, msgbuf);
533 else
534 *p = dval;
535 break;
536 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000537
Guido van Rossum530956d1996-07-21 02:27:43 +0000538#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000539 case 'D': {/* complex double */
540 Py_complex *p = va_arg(*p_va, Py_complex *);
541 Py_complex cval;
542 cval = PyComplex_AsCComplex(arg);
543 if (PyErr_Occurred())
544 return converterr("complex<D>", arg, msgbuf);
545 else
546 *p = cval;
547 break;
548 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000549#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000550
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000551 case 'c': {/* char */
552 char *p = va_arg(*p_va, char *);
553 if (PyString_Check(arg) && PyString_Size(arg) == 1)
554 *p = PyString_AsString(arg)[0];
555 else
556 return converterr("char", arg, msgbuf);
557 break;
558 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000559
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000560 case 's': {/* string */
561 if (*format == '#') {
562 void **p = (void **)va_arg(*p_va, char **);
563 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000564
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000565 if (PyString_Check(arg)) {
566 *p = PyString_AS_STRING(arg);
567 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000568 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000569#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000570 else if (PyUnicode_Check(arg)) {
571 arg = UNICODE_DEFAULT_ENCODING(arg);
572 if (arg == NULL)
573 return converterr(CONV_UNICODE,
574 arg, msgbuf);
575 *p = PyString_AS_STRING(arg);
576 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000577 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000578#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000579 else { /* any buffer-like object */
580 char *buf;
581 int count = convertbuffer(arg, p, &buf);
582 if (count < 0)
583 return converterr(buf, arg, msgbuf);
584 *q = count;
585 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000586 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000587 } else {
588 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000589
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000590 if (PyString_Check(arg))
591 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000592#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000593 else if (PyUnicode_Check(arg)) {
594 arg = UNICODE_DEFAULT_ENCODING(arg);
595 if (arg == NULL)
596 return converterr(CONV_UNICODE,
597 arg, msgbuf);
598 *p = PyString_AS_STRING(arg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000599 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000600#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000601 else
602 return converterr("string", arg, msgbuf);
603 if ((int)strlen(*p) != PyString_Size(arg))
604 return converterr("string without null bytes",
605 arg, msgbuf);
606 }
607 break;
608 }
609
610 case 'z': {/* string, may be NULL (None) */
611 if (*format == '#') { /* any buffer-like object */
612 void **p = (void **)va_arg(*p_va, char **);
613 int *q = va_arg(*p_va, int *);
614
615 if (arg == Py_None) {
616 *p = 0;
617 *q = 0;
618 }
619 else if (PyString_Check(arg)) {
620 *p = PyString_AS_STRING(arg);
621 *q = PyString_GET_SIZE(arg);
622 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000623#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000624 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 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000632#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000633 else { /* any buffer-like object */
634 char *buf;
635 int count = convertbuffer(arg, p, &buf);
636
637 if (count < 0)
638 return converterr(buf, arg, msgbuf);
639 *q = count;
640 }
641 format++;
642 } else {
643 char **p = va_arg(*p_va, char **);
644
645 if (arg == Py_None)
646 *p = 0;
647 else if (PyString_Check(arg))
648 *p = PyString_AsString(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000649#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000650 else if (PyUnicode_Check(arg)) {
651 arg = UNICODE_DEFAULT_ENCODING(arg);
652 if (arg == NULL)
653 return converterr(CONV_UNICODE,
654 arg, msgbuf);
655 *p = PyString_AS_STRING(arg);
656 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000657#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000658 else
659 return converterr("string or None",
660 arg, msgbuf);
661 if (*format == '#') {
662 int *q = va_arg(*p_va, int *);
663 if (arg == Py_None)
664 *q = 0;
665 else
666 *q = PyString_Size(arg);
667 format++;
668 }
669 else if (*p != NULL &&
670 (int)strlen(*p) != PyString_Size(arg))
671 return converterr(
672 "string without null bytes or None",
673 arg, msgbuf);
674 }
675 break;
676 }
677
678 case 'e': {/* encoded string */
679 char **buffer;
680 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000681 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000682 int size, recode_strings;
683
684 /* Get 'e' parameter: the encoding name */
685 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000686#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000687 if (encoding == NULL)
688 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000689#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000690
691 /* Get output buffer parameter:
692 's' (recode all objects via Unicode) or
693 't' (only recode non-string objects)
694 */
695 if (*format == 's')
696 recode_strings = 1;
697 else if (*format == 't')
698 recode_strings = 0;
699 else
700 return converterr(
701 "(unknown parser marker combination)",
702 arg, msgbuf);
703 buffer = (char **)va_arg(*p_va, char **);
704 format++;
705 if (buffer == NULL)
706 return converterr("(buffer is NULL)",
707 arg, msgbuf);
708
709 /* Encode object */
710 if (!recode_strings && PyString_Check(arg)) {
711 s = arg;
712 Py_INCREF(s);
713 }
714 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000715#ifdef Py_USING_UNICODE
716 PyObject *u;
717
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000718 /* Convert object to Unicode */
719 u = PyUnicode_FromObject(arg);
720 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000721 return converterr(
722 "string or unicode or text buffer",
723 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000724
725 /* Encode object; use default error handling */
726 s = PyUnicode_AsEncodedString(u,
727 encoding,
728 NULL);
729 Py_DECREF(u);
730 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000731 return converterr("(encoding failed)",
732 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000733 if (!PyString_Check(s)) {
734 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000735 return converterr(
736 "(encoder failed to return a string)",
737 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000738 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000739#else
740 return converterr("string<e>", arg, msgbuf);
741#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000742 }
743 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000744
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000745 /* Write output; output is guaranteed to be 0-terminated */
746 if (*format == '#') {
747 /* Using buffer length parameter '#':
748
749 - if *buffer is NULL, a new buffer of the
750 needed size is allocated and the data
751 copied into it; *buffer is updated to point
752 to the new buffer; the caller is
753 responsible for PyMem_Free()ing it after
754 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000755
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000756 - if *buffer is not NULL, the data is
757 copied to *buffer; *buffer_len has to be
758 set to the size of the buffer on input;
759 buffer overflow is signalled with an error;
760 buffer has to provide enough room for the
761 encoded string plus the trailing 0-byte
762
763 - in both cases, *buffer_len is updated to
764 the size of the buffer /excluding/ the
765 trailing 0-byte
766
767 */
768 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000769
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000770 format++;
771 if (buffer_len == NULL)
772 return converterr(
773 "(buffer_len is NULL)",
774 arg, msgbuf);
775 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000776 *buffer = PyMem_NEW(char, size + 1);
777 if (*buffer == NULL) {
778 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000779 return converterr(
780 "(memory error)",
781 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000782 }
Fred Drake25871c02000-05-03 15:17:02 +0000783 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000784 if (size + 1 > *buffer_len) {
785 Py_DECREF(s);
786 return converterr(
787 "(buffer overflow)",
788 arg, msgbuf);
789 }
Fred Drake25871c02000-05-03 15:17:02 +0000790 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000791 memcpy(*buffer,
792 PyString_AS_STRING(s),
793 size + 1);
794 *buffer_len = size;
795 } else {
796 /* Using a 0-terminated buffer:
797
798 - the encoded string has to be 0-terminated
799 for this variant to work; if it is not, an
800 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000801
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000802 - a new buffer of the needed size is
803 allocated and the data copied into it;
804 *buffer is updated to point to the new
805 buffer; the caller is responsible for
806 PyMem_Free()ing it after usage
807
808 */
809 if ((int)strlen(PyString_AS_STRING(s)) != size)
810 return converterr(
811 "(encoded string without NULL bytes)",
812 arg, msgbuf);
813 *buffer = PyMem_NEW(char, size + 1);
814 if (*buffer == NULL) {
815 Py_DECREF(s);
816 return converterr("(memory error)",
817 arg, msgbuf);
818 }
819 memcpy(*buffer,
820 PyString_AS_STRING(s),
821 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000822 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000823 Py_DECREF(s);
824 break;
825 }
826
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000827#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000828 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
829 if (*format == '#') { /* any buffer-like object */
830 void **p = (void **)va_arg(*p_va, char **);
831 int *q = va_arg(*p_va, int *);
832 char *buf;
833 int count = convertbuffer(arg, p, &buf);
834
835 if (count < 0)
836 return converterr(buf, arg, msgbuf);
837 *q = count/(sizeof(Py_UNICODE));
838 format++;
839 } else {
840 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
841
Guido van Rossume826ef02000-03-10 23:02:17 +0000842 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000843 *p = PyUnicode_AS_UNICODE(arg);
844 else
845 return converterr("unicode", arg, msgbuf);
846 }
847 break;
848 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000849#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000850
851 case 'S': { /* string object */
852 PyObject **p = va_arg(*p_va, PyObject **);
853 if (PyString_Check(arg))
854 *p = arg;
855 else
856 return converterr("string", arg, msgbuf);
857 break;
858 }
859
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000860#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000861 case 'U': { /* Unicode object */
862 PyObject **p = va_arg(*p_va, PyObject **);
863 if (PyUnicode_Check(arg))
864 *p = arg;
865 else
866 return converterr("unicode", arg, msgbuf);
867 break;
868 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000869#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000870
871 case 'O': { /* object */
872 PyTypeObject *type;
873 PyObject **p;
874 if (*format == '!') {
875 type = va_arg(*p_va, PyTypeObject*);
876 p = va_arg(*p_va, PyObject **);
877 format++;
878 if (arg->ob_type == type)
Guido van Rossume826ef02000-03-10 23:02:17 +0000879 *p = arg;
880 else
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000881 return converterr(type->tp_name, arg, msgbuf);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000882
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000883 }
884 else if (*format == '?') {
885 inquiry pred = va_arg(*p_va, inquiry);
886 p = va_arg(*p_va, PyObject **);
887 format++;
888 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000889 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000890 else
891 return converterr("(unspecified)",
892 arg, msgbuf);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000893
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000894 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000895 else if (*format == '&') {
896 typedef int (*converter)(PyObject *, void *);
897 converter convert = va_arg(*p_va, converter);
898 void *addr = va_arg(*p_va, void *);
899 format++;
900 if (! (*convert)(arg, addr))
901 return converterr("(unspecified)",
902 arg, msgbuf);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000903 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000904 else {
905 p = va_arg(*p_va, PyObject **);
906 *p = arg;
907 }
908 break;
909 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000910
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000911
912 case 'w': { /* memory buffer, read-write access */
913 void **p = va_arg(*p_va, void **);
914 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
915 int count;
916
917 if (pb == NULL ||
918 pb->bf_getwritebuffer == NULL ||
919 pb->bf_getsegcount == NULL)
920 return converterr("read-write buffer", arg, msgbuf);
921 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
922 return converterr("single-segment read-write buffer",
923 arg, msgbuf);
924 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
925 return converterr("(unspecified)", arg, msgbuf);
926 if (*format == '#') {
927 int *q = va_arg(*p_va, int *);
928
929 *q = count;
930 format++;
931 }
932 break;
933 }
934
935 case 't': { /* 8-bit character buffer, read-only access */
936 const char **p = va_arg(*p_va, const char **);
937 char *buf;
938 int count;
939
940 if (*format++ != '#')
941 return converterr(
942 "invalid use of 't' format character",
943 arg, msgbuf);
944 if (!PyType_HasFeature(arg->ob_type,
945 Py_TPFLAGS_HAVE_GETCHARBUFFER))
946 return converterr(
947 "string or read-only character buffer",
948 arg, msgbuf);
949
950 count = convertbuffer(arg, (void **)p, &buf);
951 if (count < 0)
952 return converterr(buf, arg, msgbuf);
953 *va_arg(*p_va, int *) = count;
954 break;
955 }
956
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000957 default:
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000958 return converterr("impossible<bad format char>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000959
960 }
961
962 *p_format = format;
963 return NULL;
964}
Guido van Rossumaa354651996-08-19 19:32:04 +0000965
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000966int convertbuffer(PyObject *arg, void **p, char **errmsg)
967{
968 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
969 int count;
970 if (pb == NULL ||
971 pb->bf_getreadbuffer == NULL ||
972 pb->bf_getsegcount == NULL) {
973 *errmsg = "string or read-only buffer";
974 return -1;
975 }
976 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
977 *errmsg = "string or single-segment read-only buffer";
978 return -1;
979 }
980 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
981 *errmsg = "(unspecified)";
982 }
983 return count;
984}
Guido van Rossumaa354651996-08-19 19:32:04 +0000985
986/* Support for keyword arguments donated by
987 Geoff Philbrick <philbric@delphi.hks.com> */
988
Guido van Rossum79f25d91997-04-29 20:08:16 +0000989int PyArg_ParseTupleAndKeywords(PyObject *args,
990 PyObject *keywords,
Guido van Rossumaa354651996-08-19 19:32:04 +0000991 char *format,
992 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +0000993{
994 int retval;
995 va_list va;
Guido van Rossumaa354651996-08-19 19:32:04 +0000996
997 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +0000998 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
999 va_end(va);
1000 return retval;
1001}
1002
1003
1004static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001005vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
1006 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001007{
1008 char msgbuf[256];
1009 int levels[32];
1010 char *fname = NULL;
1011 char *message = NULL;
1012 int min = -1;
1013 int max = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001014 char *formatsave = format;
1015 int i, len, tplen, kwlen;
1016 char *msg, *ks, **p;
1017 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001018 PyObject *key, *value;
Guido van Rossumaa354651996-08-19 19:32:04 +00001019
1020 /* nested tuples cannot be parsed when using keyword arguments */
1021
1022 for (;;) {
1023 int c = *format++;
1024 if (c == '(') {
1025 PyErr_SetString(PyExc_SystemError,
1026 "tuple found in format when using keyword arguments");
1027 return 0;
1028 }
1029 else if (c == '\0')
1030 break;
1031 else if (c == ':') {
1032 fname = format;
1033 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001034 } else if (c == ';') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001035 message = format;
1036 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001037 } else if (c == 'e')
Marc-André Lemburgbbcf2a72000-09-08 11:49:37 +00001038 ; /* Pass */
Guido van Rossumaa354651996-08-19 19:32:04 +00001039 else if (isalpha(c))
1040 max++;
1041 else if (c == '|')
1042 min = max;
1043 }
1044
1045 if (min < 0)
1046 min = max;
1047
1048 format = formatsave;
1049
1050 if (!PyTuple_Check(args)) {
1051 PyErr_SetString(PyExc_SystemError,
1052 "new style getargs format but argument is not a tuple");
1053 return 0;
1054 }
1055
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001056 tplen = PyTuple_GET_SIZE(args);
Guido van Rossumaa354651996-08-19 19:32:04 +00001057
1058 /* do a cursory check of the keywords just to see how many we got */
1059
1060 if (keywords) {
1061 if (!PyDict_Check(keywords)) {
Jeremy Hyltona0ac40c2001-01-25 20:13:10 +00001062 if (keywords == NULL)
1063 PyErr_SetString(PyExc_SystemError,
1064 "NULL received when keyword dictionary expected");
1065 else
1066 PyErr_Format(PyExc_SystemError,
1067 "%s received when keyword dictionary expected",
1068 keywords->ob_type->tp_name);
Guido van Rossumaa354651996-08-19 19:32:04 +00001069 return 0;
1070 }
1071 kwlen = PyDict_Size(keywords);
1072 }
1073 else {
1074 kwlen = 0;
1075 }
1076
1077 /* make sure there are no duplicate values for an argument;
1078 its not clear when to use the term "keyword argument vs.
1079 keyword parameter in messages */
1080
1081 if (keywords) {
1082 for (i = 0; i < tplen; i++) {
1083 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1084 sprintf(msgbuf,
1085 "keyword parameter %s redefined",
1086 kwlist[i]);
1087 PyErr_SetString(PyExc_TypeError, msgbuf);
1088 return 0;
1089 }
1090 }
1091 }
1092 PyErr_Clear(); /* I'm not which Py functions set the error string */
1093
1094 /* required arguments missing from args can be supplied by keyword
1095 arguments */
1096
1097 len = tplen;
1098 if (keywords && tplen < min) {
1099 for (i = tplen; i < min; i++) {
1100 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1101 len++;
1102 }
1103 }
1104 }
1105 PyErr_Clear();
1106
1107 /* make sure we got an acceptable number of arguments; the message
1108 is a little confusing with keywords since keyword arguments
1109 which are supplied, but don't match the required arguments
1110 are not included in the "%d given" part of the message */
1111
1112 if (len < min || max < len) {
1113 if (message == NULL) {
1114 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001115 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001116 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001117 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001118 min==max ? "exactly"
1119 : len < min ? "at least" : "at most",
1120 len < min ? min : max,
1121 (len < min ? min : max) == 1 ? "" : "s",
1122 len);
1123 message = msgbuf;
1124 }
1125 PyErr_SetString(PyExc_TypeError, message);
1126 return 0;
1127 }
1128
1129 for (i = 0; i < tplen; i++) {
1130 if (*format == '|')
1131 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001132 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001133 levels, msgbuf);
1134 if (msg) {
1135 seterror(i+1, msg, levels, fname, message);
1136 return 0;
1137 }
1138 }
1139
1140 /* handle no keyword parameters in call */
1141
1142 if (!keywords) return 1;
1143
1144 /* make sure the number of keywords in the keyword list matches the
1145 number of items in the format string */
1146
1147 nkwds = 0;
1148 p = kwlist;
1149 for (;;) {
1150 if (!*(p++)) break;
1151 nkwds++;
1152 }
1153
1154 if (nkwds != max) {
1155 PyErr_SetString(PyExc_SystemError,
1156 "number of items in format string and keyword list do not match");
1157 return 0;
1158 }
1159
1160 /* convert the keyword arguments; this uses the format
1161 string where it was left after processing args */
1162
1163 converted = 0;
1164 for (i = tplen; i < nkwds; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001165 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001166 if (*format == '|')
1167 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001168 item = PyMapping_GetItemString(keywords, kwlist[i]);
1169 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001170 msg = convertitem(item, &format, p_va, levels, msgbuf);
1171 if (msg) {
1172 seterror(i+1, msg, levels, fname, message);
1173 return 0;
1174 }
1175 converted++;
Barry Warsaw07050282000-12-11 20:01:55 +00001176 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001177 }
1178 else {
1179 PyErr_Clear();
1180 msg = skipitem(&format, p_va);
1181 if (msg) {
1182 seterror(i+1, msg, levels, fname, message);
1183 return 0;
1184 }
1185 }
1186 }
1187
1188 /* make sure there are no extraneous keyword arguments */
1189
1190 pos = 0;
1191 if (converted < kwlen) {
1192 while (PyDict_Next(keywords, &pos, &key, &value)) {
1193 match = 0;
1194 ks = PyString_AsString(key);
1195 for (i = 0; i < nkwds; i++) {
1196 if (!strcmp(ks, kwlist[i])) {
1197 match = 1;
1198 break;
1199 }
1200 }
1201 if (!match) {
1202 sprintf(msgbuf,
Guido van Rossum80dc16b2000-05-08 14:02:41 +00001203 "%s is an invalid keyword argument for this function",
Guido van Rossumaa354651996-08-19 19:32:04 +00001204 ks);
1205 PyErr_SetString(PyExc_TypeError, msgbuf);
1206 return 0;
1207 }
1208 }
1209 }
1210
1211 return 1;
1212}
1213
1214
1215static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001216skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001217{
1218 char *format = *p_format;
1219 char c = *format++;
1220
1221 switch (c) {
1222
1223 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001224 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001225 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001226 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001227 break;
1228 }
1229
1230 case 'h': /* short int */
1231 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001232 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001233 break;
1234 }
1235
Jack Jansencc22fbe2000-08-05 21:29:58 +00001236 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001237 {
1238 (void) va_arg(*p_va, unsigned short *);
1239 break;
1240 }
1241
Guido van Rossumaa354651996-08-19 19:32:04 +00001242 case 'i': /* int */
1243 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001244 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001245 break;
1246 }
1247
1248 case 'l': /* long int */
1249 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001250 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001251 break;
1252 }
1253
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001254#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001255 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001256 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001257 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001258 break;
1259 }
1260#endif
1261
Guido van Rossumaa354651996-08-19 19:32:04 +00001262 case 'f': /* float */
1263 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001264 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001265 break;
1266 }
1267
1268 case 'd': /* double */
1269 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001270 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001271 break;
1272 }
1273
1274#ifndef WITHOUT_COMPLEX
1275 case 'D': /* complex double */
1276 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001277 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001278 break;
1279 }
1280#endif /* WITHOUT_COMPLEX */
1281
1282 case 'c': /* char */
1283 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001284 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001285 break;
1286 }
1287
1288 case 's': /* string */
1289 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001290 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001291 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001292 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001293 format++;
1294 }
1295 break;
1296 }
1297
1298 case 'z': /* string */
1299 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001300 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001301 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001302 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001303 format++;
1304 }
1305 break;
1306 }
1307
1308 case 'S': /* string object */
1309 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001310 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001311 break;
1312 }
1313
1314 case 'O': /* object */
1315 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001316 if (*format == '!') {
1317 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001318 (void) va_arg(*p_va, PyTypeObject*);
1319 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001320 }
1321#if 0
1322/* I don't know what this is for */
1323 else if (*format == '?') {
1324 inquiry pred = va_arg(*p_va, inquiry);
1325 format++;
1326 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001327 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001328 }
1329 }
1330#endif
1331 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001332 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001333 (void) va_arg(*p_va, converter);
1334 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001335 format++;
1336 }
1337 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001339 }
1340 break;
1341 }
1342
1343 default:
1344 return "impossible<bad format char>";
1345
1346 }
1347
1348 *p_format = format;
1349 return NULL;
1350}