blob: c112386a41188009acd7617079abab27b226ad19 [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;
83 char *formatsave = format;
84 int i, len;
85 char *msg;
86
Tim Peters5c4d5bf2001-02-12 22:13:26 +000087 assert(compat || (args != (PyObject*)NULL));
88
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000089 for (;;) {
90 int c = *format++;
91 if (c == '(' /* ')' */) {
92 if (level == 0)
93 max++;
94 level++;
95 }
96 else if (/* '(' */ c == ')') {
97 if (level == 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +000098 Py_FatalError(/* '(' */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000099 "excess ')' in getargs format");
100 else
101 level--;
102 }
103 else if (c == '\0')
104 break;
105 else if (c == ':') {
106 fname = format;
107 break;
108 }
109 else if (c == ';') {
110 message = format;
111 break;
112 }
113 else if (level != 0)
114 ; /* Pass */
Fred Draked6573032001-05-18 21:03:40 +0000115 else if (c == 'e')
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000116 ; /* Pass */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000117 else if (isalpha(c))
118 max++;
119 else if (c == '|')
120 min = max;
121 }
122
123 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000124 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000125
126 if (min < 0)
127 min = max;
128
129 format = formatsave;
130
131 if (compat) {
132 if (max == 0) {
133 if (args == NULL)
134 return 1;
Ka-Ping Yee20579702001-01-15 22:14:16 +0000135 sprintf(msgbuf, "%s%s takes no arguments",
136 fname==NULL ? "function" : fname,
137 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000138 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000139 return 0;
140 }
141 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000142 if (args == NULL) {
143 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000144 "%s%s takes at least one argument",
145 fname==NULL ? "function" : fname,
146 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000147 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000148 return 0;
149 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000150 msg = convertitem(args, &format, p_va, levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000151 if (msg == NULL)
152 return 1;
153 seterror(levels[0], msg, levels+1, fname, message);
154 return 0;
155 }
156 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000157 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000158 "old style getargs format uses new features");
159 return 0;
160 }
161 }
162
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 if (!PyTuple_Check(args)) {
164 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000165 "new style getargs format but argument is not a tuple");
166 return 0;
167 }
168
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000169 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000170
171 if (len < min || max < len) {
172 if (message == NULL) {
173 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000174 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000175 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000176 fname==NULL ? "" : "()",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000177 min==max ? "exactly"
178 : len < min ? "at least" : "at most",
179 len < min ? min : max,
180 (len < min ? min : max) == 1 ? "" : "s",
181 len);
182 message = msgbuf;
183 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000185 return 0;
186 }
187
188 for (i = 0; i < len; i++) {
189 if (*format == '|')
190 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000191 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
192 levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000193 if (msg) {
194 seterror(i+1, msg, levels, fname, message);
195 return 0;
196 }
197 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000198
Guido van Rossum730806d1998-04-10 22:27:42 +0000199 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000200 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000201 *format != '|' && *format != ':' && *format != ';') {
202 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000203 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000204 return 0;
205 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000206
207 return 1;
208}
209
210
211
212static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000213seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000214{
215 char buf[256];
216 int i;
217 char *p = buf;
218
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000220 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000221 else if (message == NULL) {
222 if (fname != NULL) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000223 sprintf(p, "%s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000224 p += strlen(p);
225 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000226 if (iarg != 0) {
227 sprintf(p, "argument %d", iarg);
228 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000229 p += strlen(p);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000230 while (levels[i] > 0) {
231 sprintf(p, ", item %d", levels[i]-1);
232 p += strlen(p);
233 i++;
234 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000235 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000236 else {
237 sprintf(p, "argument");
238 p += strlen(p);
239 }
240 sprintf(p, " %s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000241 message = buf;
242 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000244}
245
246
247/* Convert a tuple argument.
248 On entry, *p_format points to the character _after_ the opening '('.
249 On successful exit, *p_format points to the closing ')'.
250 If successful:
251 *p_format and *p_va are updated,
252 *levels and *msgbuf are untouched,
253 and NULL is returned.
254 If the argument is invalid:
255 *p_format is unchanged,
256 *p_va is undefined,
257 *levels is a 0-terminated list of item numbers,
258 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000259 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000260 <typename1> is the name of the expected type, and
261 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000262 and msgbuf is returned.
263*/
264
265static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000266converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
267 char *msgbuf, int toplevel)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000268{
269 int level = 0;
270 int n = 0;
271 char *format = *p_format;
272 int i;
273
274 for (;;) {
275 int c = *format++;
276 if (c == '(') {
277 if (level == 0)
278 n++;
279 level++;
280 }
281 else if (c == ')') {
282 if (level == 0)
283 break;
284 level--;
285 }
286 else if (c == ':' || c == ';' || c == '\0')
287 break;
288 else if (level == 0 && isalpha(c))
289 n++;
290 }
291
Ka-Ping Yee20579702001-01-15 22:14:16 +0000292 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000293 levels[0] = 0;
294 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000295 toplevel ? "expected %d arguments, not %s" :
296 "must be %d-item sequence, not %s",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000297 n, arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000298 return msgbuf;
299 }
300
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000301 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000302 levels[0] = 0;
303 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000304 toplevel ? "expected %d arguments, not %d" :
305 "must be sequence of length %d, not %d",
306 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000307 return msgbuf;
308 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000309
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000310 format = *p_format;
311 for (i = 0; i < n; i++) {
312 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000313 PyObject *item;
314 item = PySequence_GetItem(arg, i);
315 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
316 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
317 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000318 if (msg != NULL) {
319 levels[0] = i+1;
320 return msg;
321 }
322 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000323
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000324 *p_format = format;
325 return NULL;
326}
327
328
329/* Convert a single item. */
330
331static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
333 char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000334{
335 char *msg;
336 char *format = *p_format;
337
338 if (*format == '(' /* ')' */) {
339 format++;
340 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
341 if (msg == NULL)
342 format++;
343 }
344 else {
345 msg = convertsimple(arg, &format, p_va, msgbuf);
346 if (msg != NULL)
347 levels[0] = 0;
348 }
349 if (msg == NULL)
350 *p_format = format;
351 return msg;
352}
353
354
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000355
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000356/* Internal API needed by convertsimple() and a helper macro. */
Guido van Rossum700c6ff2000-04-27 20:13:18 +0000357extern
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000358PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
Guido van Rossum700c6ff2000-04-27 20:13:18 +0000359 const char *errors);
360
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000361#define UNICODE_DEFAULT_ENCODING(arg) \
362 _PyUnicode_AsDefaultEncodedString(arg, NULL)
363
364/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000365
366static char *
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000367converterr(char *expected, PyObject *arg, char *msgbuf)
368{
369 assert (expected != NULL);
370 sprintf(msgbuf, "must be %.50s, not %.50s", expected,
371 arg == Py_None ? "None" : arg->ob_type->tp_name);
372 return msgbuf;
373}
374
375#define CONV_UNICODE "(unicode conversion error)"
376
377/* Convert a non-tuple argument. Return NULL if conversion went OK,
378 or a string with a message describing the failure. The message is
379 formatted as "must be <desired type>, not <actual type>".
380 When failing, an exception may or may not have been raised.
381 Don't call if a tuple is expected.
382*/
383
384static char *
385convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000386{
387 char *format = *p_format;
388 char c = *format++;
389
390 switch (c) {
391
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000392 case 'b': { /* unsigned byte -- very short int */
393 char *p = va_arg(*p_va, char *);
394 long ival = PyInt_AsLong(arg);
395 if (ival == -1 && PyErr_Occurred())
396 return converterr("integer<b>", arg, msgbuf);
397 else if (ival < 0) {
398 PyErr_SetString(PyExc_OverflowError,
399 "unsigned byte integer is less than minimum");
400 return converterr("integer<b>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000401 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000402 else if (ival > UCHAR_MAX) {
403 PyErr_SetString(PyExc_OverflowError,
404 "unsigned byte integer is greater than maximum");
405 return converterr("integer<b>", arg, msgbuf);
406 }
407 else
408 *p = (unsigned char) ival;
409 break;
410 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000411
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000412 case 'B': {/* byte sized bitfield - both signed and unsigned
413 values allowed */
414 char *p = va_arg(*p_va, char *);
415 long ival = PyInt_AsLong(arg);
416 if (ival == -1 && PyErr_Occurred())
417 return converterr("integer<b>", arg, msgbuf);
418 else if (ival < SCHAR_MIN) {
419 PyErr_SetString(PyExc_OverflowError,
420 "byte-sized integer bitfield is less than minimum");
421 return converterr("integer<B>", arg, msgbuf);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000422 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000423 else if (ival > (int)UCHAR_MAX) {
424 PyErr_SetString(PyExc_OverflowError,
425 "byte-sized integer bitfield is greater than maximum");
426 return converterr("integer<B>", arg, msgbuf);
427 }
428 else
429 *p = (unsigned char) ival;
430 break;
431 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000432
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000433 case 'h': {/* signed short int */
434 short *p = va_arg(*p_va, short *);
435 long ival = PyInt_AsLong(arg);
436 if (ival == -1 && PyErr_Occurred())
437 return converterr("integer<h>", arg, msgbuf);
438 else if (ival < SHRT_MIN) {
439 PyErr_SetString(PyExc_OverflowError,
440 "signed short integer is less than minimum");
441 return converterr("integer<h>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000442 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000443 else if (ival > SHRT_MAX) {
444 PyErr_SetString(PyExc_OverflowError,
445 "signed short integer is greater than maximum");
446 return converterr("integer<h>", arg, msgbuf);
447 }
448 else
449 *p = (short) ival;
450 break;
451 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000452
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000453 case 'H': { /* short int sized bitfield, both signed and
454 unsigned allowed */
455 unsigned short *p = va_arg(*p_va, unsigned short *);
456 long ival = PyInt_AsLong(arg);
457 if (ival == -1 && PyErr_Occurred())
458 return converterr("integer<H>", arg, msgbuf);
459 else if (ival < SHRT_MIN) {
460 PyErr_SetString(PyExc_OverflowError,
461 "short integer bitfield is less than minimum");
462 return converterr("integer<H>", arg, msgbuf);
Jack Jansend50338f2000-07-06 12:22:00 +0000463 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000464 else if (ival > USHRT_MAX) {
465 PyErr_SetString(PyExc_OverflowError,
466 "short integer bitfield is greater than maximum");
467 return converterr("integer<H>", arg, msgbuf);
468 }
469 else
470 *p = (unsigned short) ival;
471 break;
472 }
Jack Jansend50338f2000-07-06 12:22:00 +0000473
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000474 case 'i': {/* signed int */
475 int *p = va_arg(*p_va, int *);
476 long ival = PyInt_AsLong(arg);
477 if (ival == -1 && PyErr_Occurred())
478 return converterr("integer<i>", arg, msgbuf);
479 else if (ival > INT_MAX) {
480 PyErr_SetString(PyExc_OverflowError,
481 "signed integer is greater than maximum");
482 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000483 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000484 else if (ival < INT_MIN) {
485 PyErr_SetString(PyExc_OverflowError,
486 "signed integer is less than minimum");
487 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000488 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000489 else
490 *p = ival;
491 break;
492 }
493
494 case 'l': {/* long int */
495 long *p = va_arg(*p_va, long *);
496 long ival = PyInt_AsLong(arg);
497 if (ival == -1 && PyErr_Occurred())
498 return converterr("integer<l>", arg, msgbuf);
499 else
500 *p = ival;
501 break;
502 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000503
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000504#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000505 case 'L': {/* LONG_LONG */
506 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
507 LONG_LONG ival = PyLong_AsLongLong( arg );
508 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
509 return converterr("long<L>", arg, msgbuf);
510 } else {
511 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000512 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000513 break;
514 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000515#endif
516
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000517 case 'f': {/* float */
518 float *p = va_arg(*p_va, float *);
519 double dval = PyFloat_AsDouble(arg);
520 if (PyErr_Occurred())
521 return converterr("float<f>", arg, msgbuf);
522 else
523 *p = (float) dval;
524 break;
525 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000526
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000527 case 'd': {/* double */
528 double *p = va_arg(*p_va, double *);
529 double dval = PyFloat_AsDouble(arg);
530 if (PyErr_Occurred())
531 return converterr("float<d>", arg, msgbuf);
532 else
533 *p = dval;
534 break;
535 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000536
Guido van Rossum530956d1996-07-21 02:27:43 +0000537#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000538 case 'D': {/* complex double */
539 Py_complex *p = va_arg(*p_va, Py_complex *);
540 Py_complex cval;
541 cval = PyComplex_AsCComplex(arg);
542 if (PyErr_Occurred())
543 return converterr("complex<D>", arg, msgbuf);
544 else
545 *p = cval;
546 break;
547 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000548#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000549
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000550 case 'c': {/* char */
551 char *p = va_arg(*p_va, char *);
552 if (PyString_Check(arg) && PyString_Size(arg) == 1)
553 *p = PyString_AsString(arg)[0];
554 else
555 return converterr("char", arg, msgbuf);
556 break;
557 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000558
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000559 case 's': {/* string */
560 if (*format == '#') {
561 void **p = (void **)va_arg(*p_va, char **);
562 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000563
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000564 if (PyString_Check(arg)) {
565 *p = PyString_AS_STRING(arg);
566 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000567 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000568 else if (PyUnicode_Check(arg)) {
569 arg = UNICODE_DEFAULT_ENCODING(arg);
570 if (arg == NULL)
571 return converterr(CONV_UNICODE,
572 arg, msgbuf);
573 *p = PyString_AS_STRING(arg);
574 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000575 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000576 else { /* any buffer-like object */
577 char *buf;
578 int count = convertbuffer(arg, p, &buf);
579 if (count < 0)
580 return converterr(buf, arg, msgbuf);
581 *q = count;
582 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000583 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000584 } else {
585 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000586
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000587 if (PyString_Check(arg))
588 *p = PyString_AS_STRING(arg);
589 else if (PyUnicode_Check(arg)) {
590 arg = UNICODE_DEFAULT_ENCODING(arg);
591 if (arg == NULL)
592 return converterr(CONV_UNICODE,
593 arg, msgbuf);
594 *p = PyString_AS_STRING(arg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000595 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000596 else
597 return converterr("string", arg, msgbuf);
598 if ((int)strlen(*p) != PyString_Size(arg))
599 return converterr("string without null bytes",
600 arg, msgbuf);
601 }
602 break;
603 }
604
605 case 'z': {/* string, may be NULL (None) */
606 if (*format == '#') { /* any buffer-like object */
607 void **p = (void **)va_arg(*p_va, char **);
608 int *q = va_arg(*p_va, int *);
609
610 if (arg == Py_None) {
611 *p = 0;
612 *q = 0;
613 }
614 else if (PyString_Check(arg)) {
615 *p = PyString_AS_STRING(arg);
616 *q = PyString_GET_SIZE(arg);
617 }
618 else if (PyUnicode_Check(arg)) {
619 arg = UNICODE_DEFAULT_ENCODING(arg);
620 if (arg == NULL)
621 return converterr(CONV_UNICODE,
622 arg, msgbuf);
623 *p = PyString_AS_STRING(arg);
624 *q = PyString_GET_SIZE(arg);
625 }
626 else { /* any buffer-like object */
627 char *buf;
628 int count = convertbuffer(arg, p, &buf);
629
630 if (count < 0)
631 return converterr(buf, arg, msgbuf);
632 *q = count;
633 }
634 format++;
635 } else {
636 char **p = va_arg(*p_va, char **);
637
638 if (arg == Py_None)
639 *p = 0;
640 else if (PyString_Check(arg))
641 *p = PyString_AsString(arg);
642 else if (PyUnicode_Check(arg)) {
643 arg = UNICODE_DEFAULT_ENCODING(arg);
644 if (arg == NULL)
645 return converterr(CONV_UNICODE,
646 arg, msgbuf);
647 *p = PyString_AS_STRING(arg);
648 }
649 else
650 return converterr("string or None",
651 arg, msgbuf);
652 if (*format == '#') {
653 int *q = va_arg(*p_va, int *);
654 if (arg == Py_None)
655 *q = 0;
656 else
657 *q = PyString_Size(arg);
658 format++;
659 }
660 else if (*p != NULL &&
661 (int)strlen(*p) != PyString_Size(arg))
662 return converterr(
663 "string without null bytes or None",
664 arg, msgbuf);
665 }
666 break;
667 }
668
669 case 'e': {/* encoded string */
670 char **buffer;
671 const char *encoding;
672 PyObject *u, *s;
673 int size, recode_strings;
674
675 /* Get 'e' parameter: the encoding name */
676 encoding = (const char *)va_arg(*p_va, const char *);
677 if (encoding == NULL)
678 encoding = PyUnicode_GetDefaultEncoding();
679
680 /* Get output buffer parameter:
681 's' (recode all objects via Unicode) or
682 't' (only recode non-string objects)
683 */
684 if (*format == 's')
685 recode_strings = 1;
686 else if (*format == 't')
687 recode_strings = 0;
688 else
689 return converterr(
690 "(unknown parser marker combination)",
691 arg, msgbuf);
692 buffer = (char **)va_arg(*p_va, char **);
693 format++;
694 if (buffer == NULL)
695 return converterr("(buffer is NULL)",
696 arg, msgbuf);
697
698 /* Encode object */
699 if (!recode_strings && PyString_Check(arg)) {
700 s = arg;
701 Py_INCREF(s);
702 }
703 else {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000704 /* Convert object to Unicode */
705 u = PyUnicode_FromObject(arg);
706 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000707 return converterr(
708 "string or unicode or text buffer",
709 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000710
711 /* Encode object; use default error handling */
712 s = PyUnicode_AsEncodedString(u,
713 encoding,
714 NULL);
715 Py_DECREF(u);
716 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000717 return converterr("(encoding failed)",
718 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000719 if (!PyString_Check(s)) {
720 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000721 return converterr(
722 "(encoder failed to return a string)",
723 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000724 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000725 }
726 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000727
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000728 /* Write output; output is guaranteed to be 0-terminated */
729 if (*format == '#') {
730 /* Using buffer length parameter '#':
731
732 - if *buffer is NULL, a new buffer of the
733 needed size is allocated and the data
734 copied into it; *buffer is updated to point
735 to the new buffer; the caller is
736 responsible for PyMem_Free()ing it after
737 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000738
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000739 - if *buffer is not NULL, the data is
740 copied to *buffer; *buffer_len has to be
741 set to the size of the buffer on input;
742 buffer overflow is signalled with an error;
743 buffer has to provide enough room for the
744 encoded string plus the trailing 0-byte
745
746 - in both cases, *buffer_len is updated to
747 the size of the buffer /excluding/ the
748 trailing 0-byte
749
750 */
751 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000752
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000753 format++;
754 if (buffer_len == NULL)
755 return converterr(
756 "(buffer_len is NULL)",
757 arg, msgbuf);
758 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000759 *buffer = PyMem_NEW(char, size + 1);
760 if (*buffer == NULL) {
761 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000762 return converterr(
763 "(memory error)",
764 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000765 }
Fred Drake25871c02000-05-03 15:17:02 +0000766 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000767 if (size + 1 > *buffer_len) {
768 Py_DECREF(s);
769 return converterr(
770 "(buffer overflow)",
771 arg, msgbuf);
772 }
Fred Drake25871c02000-05-03 15:17:02 +0000773 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000774 memcpy(*buffer,
775 PyString_AS_STRING(s),
776 size + 1);
777 *buffer_len = size;
778 } else {
779 /* Using a 0-terminated buffer:
780
781 - the encoded string has to be 0-terminated
782 for this variant to work; if it is not, an
783 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000784
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000785 - a new buffer of the needed size is
786 allocated and the data copied into it;
787 *buffer is updated to point to the new
788 buffer; the caller is responsible for
789 PyMem_Free()ing it after usage
790
791 */
792 if ((int)strlen(PyString_AS_STRING(s)) != size)
793 return converterr(
794 "(encoded string without NULL bytes)",
795 arg, msgbuf);
796 *buffer = PyMem_NEW(char, size + 1);
797 if (*buffer == NULL) {
798 Py_DECREF(s);
799 return converterr("(memory error)",
800 arg, msgbuf);
801 }
802 memcpy(*buffer,
803 PyString_AS_STRING(s),
804 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000805 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000806 Py_DECREF(s);
807 break;
808 }
809
810 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
811 if (*format == '#') { /* any buffer-like object */
812 void **p = (void **)va_arg(*p_va, char **);
813 int *q = va_arg(*p_va, int *);
814 char *buf;
815 int count = convertbuffer(arg, p, &buf);
816
817 if (count < 0)
818 return converterr(buf, arg, msgbuf);
819 *q = count/(sizeof(Py_UNICODE));
820 format++;
821 } else {
822 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
823
Guido van Rossume826ef02000-03-10 23:02:17 +0000824 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000825 *p = PyUnicode_AS_UNICODE(arg);
826 else
827 return converterr("unicode", arg, msgbuf);
828 }
829 break;
830 }
831
832 case 'S': { /* string object */
833 PyObject **p = va_arg(*p_va, PyObject **);
834 if (PyString_Check(arg))
835 *p = arg;
836 else
837 return converterr("string", arg, msgbuf);
838 break;
839 }
840
841 case 'U': { /* Unicode object */
842 PyObject **p = va_arg(*p_va, PyObject **);
843 if (PyUnicode_Check(arg))
844 *p = arg;
845 else
846 return converterr("unicode", arg, msgbuf);
847 break;
848 }
849
850 case 'O': { /* object */
851 PyTypeObject *type;
852 PyObject **p;
853 if (*format == '!') {
854 type = va_arg(*p_va, PyTypeObject*);
855 p = va_arg(*p_va, PyObject **);
856 format++;
857 if (arg->ob_type == type)
Guido van Rossume826ef02000-03-10 23:02:17 +0000858 *p = arg;
859 else
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000860 return converterr(type->tp_name, arg, msgbuf);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000861
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000862 }
863 else if (*format == '?') {
864 inquiry pred = va_arg(*p_va, inquiry);
865 p = va_arg(*p_va, PyObject **);
866 format++;
867 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000868 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000869 else
870 return converterr("(unspecified)",
871 arg, msgbuf);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000872
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000873 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000874 else if (*format == '&') {
875 typedef int (*converter)(PyObject *, void *);
876 converter convert = va_arg(*p_va, converter);
877 void *addr = va_arg(*p_va, void *);
878 format++;
879 if (! (*convert)(arg, addr))
880 return converterr("(unspecified)",
881 arg, msgbuf);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000882 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000883 else {
884 p = va_arg(*p_va, PyObject **);
885 *p = arg;
886 }
887 break;
888 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000889
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000890
891 case 'w': { /* memory buffer, read-write access */
892 void **p = va_arg(*p_va, void **);
893 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
894 int count;
895
896 if (pb == NULL ||
897 pb->bf_getwritebuffer == NULL ||
898 pb->bf_getsegcount == NULL)
899 return converterr("read-write buffer", arg, msgbuf);
900 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
901 return converterr("single-segment read-write buffer",
902 arg, msgbuf);
903 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
904 return converterr("(unspecified)", arg, msgbuf);
905 if (*format == '#') {
906 int *q = va_arg(*p_va, int *);
907
908 *q = count;
909 format++;
910 }
911 break;
912 }
913
914 case 't': { /* 8-bit character buffer, read-only access */
915 const char **p = va_arg(*p_va, const char **);
916 char *buf;
917 int count;
918
919 if (*format++ != '#')
920 return converterr(
921 "invalid use of 't' format character",
922 arg, msgbuf);
923 if (!PyType_HasFeature(arg->ob_type,
924 Py_TPFLAGS_HAVE_GETCHARBUFFER))
925 return converterr(
926 "string or read-only character buffer",
927 arg, msgbuf);
928
929 count = convertbuffer(arg, (void **)p, &buf);
930 if (count < 0)
931 return converterr(buf, arg, msgbuf);
932 *va_arg(*p_va, int *) = count;
933 break;
934 }
935
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000936 default:
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000937 return converterr("impossible<bad format char>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000938
939 }
940
941 *p_format = format;
942 return NULL;
943}
Guido van Rossumaa354651996-08-19 19:32:04 +0000944
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000945int convertbuffer(PyObject *arg, void **p, char **errmsg)
946{
947 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
948 int count;
949 if (pb == NULL ||
950 pb->bf_getreadbuffer == NULL ||
951 pb->bf_getsegcount == NULL) {
952 *errmsg = "string or read-only buffer";
953 return -1;
954 }
955 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
956 *errmsg = "string or single-segment read-only buffer";
957 return -1;
958 }
959 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
960 *errmsg = "(unspecified)";
961 }
962 return count;
963}
Guido van Rossumaa354651996-08-19 19:32:04 +0000964
965/* Support for keyword arguments donated by
966 Geoff Philbrick <philbric@delphi.hks.com> */
967
Guido van Rossum79f25d91997-04-29 20:08:16 +0000968int PyArg_ParseTupleAndKeywords(PyObject *args,
969 PyObject *keywords,
Guido van Rossumaa354651996-08-19 19:32:04 +0000970 char *format,
971 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +0000972{
973 int retval;
974 va_list va;
Guido van Rossumaa354651996-08-19 19:32:04 +0000975
976 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +0000977 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
978 va_end(va);
979 return retval;
980}
981
982
983static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000984vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
985 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +0000986{
987 char msgbuf[256];
988 int levels[32];
989 char *fname = NULL;
990 char *message = NULL;
991 int min = -1;
992 int max = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +0000993 char *formatsave = format;
994 int i, len, tplen, kwlen;
995 char *msg, *ks, **p;
996 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000997 PyObject *key, *value;
Guido van Rossumaa354651996-08-19 19:32:04 +0000998
999 /* nested tuples cannot be parsed when using keyword arguments */
1000
1001 for (;;) {
1002 int c = *format++;
1003 if (c == '(') {
1004 PyErr_SetString(PyExc_SystemError,
1005 "tuple found in format when using keyword arguments");
1006 return 0;
1007 }
1008 else if (c == '\0')
1009 break;
1010 else if (c == ':') {
1011 fname = format;
1012 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001013 } else if (c == ';') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001014 message = format;
1015 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001016 } else if (c == 'e')
Marc-André Lemburgbbcf2a72000-09-08 11:49:37 +00001017 ; /* Pass */
Guido van Rossumaa354651996-08-19 19:32:04 +00001018 else if (isalpha(c))
1019 max++;
1020 else if (c == '|')
1021 min = max;
1022 }
1023
1024 if (min < 0)
1025 min = max;
1026
1027 format = formatsave;
1028
1029 if (!PyTuple_Check(args)) {
1030 PyErr_SetString(PyExc_SystemError,
1031 "new style getargs format but argument is not a tuple");
1032 return 0;
1033 }
1034
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001035 tplen = PyTuple_GET_SIZE(args);
Guido van Rossumaa354651996-08-19 19:32:04 +00001036
1037 /* do a cursory check of the keywords just to see how many we got */
1038
1039 if (keywords) {
1040 if (!PyDict_Check(keywords)) {
Jeremy Hyltona0ac40c2001-01-25 20:13:10 +00001041 if (keywords == NULL)
1042 PyErr_SetString(PyExc_SystemError,
1043 "NULL received when keyword dictionary expected");
1044 else
1045 PyErr_Format(PyExc_SystemError,
1046 "%s received when keyword dictionary expected",
1047 keywords->ob_type->tp_name);
Guido van Rossumaa354651996-08-19 19:32:04 +00001048 return 0;
1049 }
1050 kwlen = PyDict_Size(keywords);
1051 }
1052 else {
1053 kwlen = 0;
1054 }
1055
1056 /* make sure there are no duplicate values for an argument;
1057 its not clear when to use the term "keyword argument vs.
1058 keyword parameter in messages */
1059
1060 if (keywords) {
1061 for (i = 0; i < tplen; i++) {
1062 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1063 sprintf(msgbuf,
1064 "keyword parameter %s redefined",
1065 kwlist[i]);
1066 PyErr_SetString(PyExc_TypeError, msgbuf);
1067 return 0;
1068 }
1069 }
1070 }
1071 PyErr_Clear(); /* I'm not which Py functions set the error string */
1072
1073 /* required arguments missing from args can be supplied by keyword
1074 arguments */
1075
1076 len = tplen;
1077 if (keywords && tplen < min) {
1078 for (i = tplen; i < min; i++) {
1079 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1080 len++;
1081 }
1082 }
1083 }
1084 PyErr_Clear();
1085
1086 /* make sure we got an acceptable number of arguments; the message
1087 is a little confusing with keywords since keyword arguments
1088 which are supplied, but don't match the required arguments
1089 are not included in the "%d given" part of the message */
1090
1091 if (len < min || max < len) {
1092 if (message == NULL) {
1093 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001094 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001095 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001096 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001097 min==max ? "exactly"
1098 : len < min ? "at least" : "at most",
1099 len < min ? min : max,
1100 (len < min ? min : max) == 1 ? "" : "s",
1101 len);
1102 message = msgbuf;
1103 }
1104 PyErr_SetString(PyExc_TypeError, message);
1105 return 0;
1106 }
1107
1108 for (i = 0; i < tplen; i++) {
1109 if (*format == '|')
1110 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001111 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001112 levels, msgbuf);
1113 if (msg) {
1114 seterror(i+1, msg, levels, fname, message);
1115 return 0;
1116 }
1117 }
1118
1119 /* handle no keyword parameters in call */
1120
1121 if (!keywords) return 1;
1122
1123 /* make sure the number of keywords in the keyword list matches the
1124 number of items in the format string */
1125
1126 nkwds = 0;
1127 p = kwlist;
1128 for (;;) {
1129 if (!*(p++)) break;
1130 nkwds++;
1131 }
1132
1133 if (nkwds != max) {
1134 PyErr_SetString(PyExc_SystemError,
1135 "number of items in format string and keyword list do not match");
1136 return 0;
1137 }
1138
1139 /* convert the keyword arguments; this uses the format
1140 string where it was left after processing args */
1141
1142 converted = 0;
1143 for (i = tplen; i < nkwds; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001144 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001145 if (*format == '|')
1146 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001147 item = PyMapping_GetItemString(keywords, kwlist[i]);
1148 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001149 msg = convertitem(item, &format, p_va, levels, msgbuf);
1150 if (msg) {
1151 seterror(i+1, msg, levels, fname, message);
1152 return 0;
1153 }
1154 converted++;
Barry Warsaw07050282000-12-11 20:01:55 +00001155 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001156 }
1157 else {
1158 PyErr_Clear();
1159 msg = skipitem(&format, p_va);
1160 if (msg) {
1161 seterror(i+1, msg, levels, fname, message);
1162 return 0;
1163 }
1164 }
1165 }
1166
1167 /* make sure there are no extraneous keyword arguments */
1168
1169 pos = 0;
1170 if (converted < kwlen) {
1171 while (PyDict_Next(keywords, &pos, &key, &value)) {
1172 match = 0;
1173 ks = PyString_AsString(key);
1174 for (i = 0; i < nkwds; i++) {
1175 if (!strcmp(ks, kwlist[i])) {
1176 match = 1;
1177 break;
1178 }
1179 }
1180 if (!match) {
1181 sprintf(msgbuf,
Guido van Rossum80dc16b2000-05-08 14:02:41 +00001182 "%s is an invalid keyword argument for this function",
Guido van Rossumaa354651996-08-19 19:32:04 +00001183 ks);
1184 PyErr_SetString(PyExc_TypeError, msgbuf);
1185 return 0;
1186 }
1187 }
1188 }
1189
1190 return 1;
1191}
1192
1193
1194static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001195skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001196{
1197 char *format = *p_format;
1198 char c = *format++;
1199
1200 switch (c) {
1201
1202 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001203 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001204 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001205 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001206 break;
1207 }
1208
1209 case 'h': /* short int */
1210 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001211 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001212 break;
1213 }
1214
Jack Jansencc22fbe2000-08-05 21:29:58 +00001215 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001216 {
1217 (void) va_arg(*p_va, unsigned short *);
1218 break;
1219 }
1220
Guido van Rossumaa354651996-08-19 19:32:04 +00001221 case 'i': /* int */
1222 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001223 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001224 break;
1225 }
1226
1227 case 'l': /* long int */
1228 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001229 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001230 break;
1231 }
1232
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001233#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001234 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001235 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001236 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001237 break;
1238 }
1239#endif
1240
Guido van Rossumaa354651996-08-19 19:32:04 +00001241 case 'f': /* float */
1242 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001243 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001244 break;
1245 }
1246
1247 case 'd': /* double */
1248 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001249 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001250 break;
1251 }
1252
1253#ifndef WITHOUT_COMPLEX
1254 case 'D': /* complex double */
1255 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001256 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001257 break;
1258 }
1259#endif /* WITHOUT_COMPLEX */
1260
1261 case 'c': /* char */
1262 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001263 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001264 break;
1265 }
1266
1267 case 's': /* string */
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 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001271 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001272 format++;
1273 }
1274 break;
1275 }
1276
1277 case 'z': /* string */
1278 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001279 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001280 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001281 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001282 format++;
1283 }
1284 break;
1285 }
1286
1287 case 'S': /* string object */
1288 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001289 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001290 break;
1291 }
1292
1293 case 'O': /* object */
1294 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001295 if (*format == '!') {
1296 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001297 (void) va_arg(*p_va, PyTypeObject*);
1298 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001299 }
1300#if 0
1301/* I don't know what this is for */
1302 else if (*format == '?') {
1303 inquiry pred = va_arg(*p_va, inquiry);
1304 format++;
1305 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001306 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001307 }
1308 }
1309#endif
1310 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001311 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001312 (void) va_arg(*p_va, converter);
1313 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001314 format++;
1315 }
1316 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001317 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001318 }
1319 break;
1320 }
1321
1322 default:
1323 return "impossible<bad format char>";
1324
1325 }
1326
1327 *p_format = format;
1328 return NULL;
1329}