blob: 16373d9b756c632af0d8c9c8f9a9fbe524c8f600 [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{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000370 assert(expected != NULL);
371 assert(arg != NULL);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000372 sprintf(msgbuf, "must be %.50s, not %.50s", expected,
373 arg == Py_None ? "None" : arg->ob_type->tp_name);
374 return msgbuf;
375}
376
377#define CONV_UNICODE "(unicode conversion error)"
378
379/* Convert a non-tuple argument. Return NULL if conversion went OK,
380 or a string with a message describing the failure. The message is
381 formatted as "must be <desired type>, not <actual type>".
382 When failing, an exception may or may not have been raised.
383 Don't call if a tuple is expected.
384*/
385
386static char *
387convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000388{
389 char *format = *p_format;
390 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000391 PyObject *uarg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000392
393 switch (c) {
394
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000395 case 'b': { /* unsigned byte -- very short int */
396 char *p = va_arg(*p_va, char *);
397 long ival = PyInt_AsLong(arg);
398 if (ival == -1 && PyErr_Occurred())
399 return converterr("integer<b>", arg, msgbuf);
400 else if (ival < 0) {
401 PyErr_SetString(PyExc_OverflowError,
402 "unsigned byte integer is less than minimum");
403 return converterr("integer<b>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000404 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000405 else if (ival > UCHAR_MAX) {
406 PyErr_SetString(PyExc_OverflowError,
407 "unsigned byte integer is greater than maximum");
408 return converterr("integer<b>", arg, msgbuf);
409 }
410 else
411 *p = (unsigned char) ival;
412 break;
413 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000414
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000415 case 'B': {/* byte sized bitfield - both signed and unsigned
416 values allowed */
417 char *p = va_arg(*p_va, char *);
418 long ival = PyInt_AsLong(arg);
419 if (ival == -1 && PyErr_Occurred())
420 return converterr("integer<b>", arg, msgbuf);
421 else if (ival < SCHAR_MIN) {
422 PyErr_SetString(PyExc_OverflowError,
423 "byte-sized integer bitfield is less than minimum");
424 return converterr("integer<B>", arg, msgbuf);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000425 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000426 else if (ival > (int)UCHAR_MAX) {
427 PyErr_SetString(PyExc_OverflowError,
428 "byte-sized integer bitfield is greater than maximum");
429 return converterr("integer<B>", arg, msgbuf);
430 }
431 else
432 *p = (unsigned char) ival;
433 break;
434 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000435
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000436 case 'h': {/* signed short int */
437 short *p = va_arg(*p_va, short *);
438 long ival = PyInt_AsLong(arg);
439 if (ival == -1 && PyErr_Occurred())
440 return converterr("integer<h>", arg, msgbuf);
441 else if (ival < SHRT_MIN) {
442 PyErr_SetString(PyExc_OverflowError,
443 "signed short integer is less than minimum");
444 return converterr("integer<h>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000445 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000446 else if (ival > SHRT_MAX) {
447 PyErr_SetString(PyExc_OverflowError,
448 "signed short integer is greater than maximum");
449 return converterr("integer<h>", arg, msgbuf);
450 }
451 else
452 *p = (short) ival;
453 break;
454 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000455
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000456 case 'H': { /* short int sized bitfield, both signed and
457 unsigned allowed */
458 unsigned short *p = va_arg(*p_va, unsigned short *);
459 long ival = PyInt_AsLong(arg);
460 if (ival == -1 && PyErr_Occurred())
461 return converterr("integer<H>", arg, msgbuf);
462 else if (ival < SHRT_MIN) {
463 PyErr_SetString(PyExc_OverflowError,
464 "short integer bitfield is less than minimum");
465 return converterr("integer<H>", arg, msgbuf);
Jack Jansend50338f2000-07-06 12:22:00 +0000466 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000467 else if (ival > USHRT_MAX) {
468 PyErr_SetString(PyExc_OverflowError,
469 "short integer bitfield is greater than maximum");
470 return converterr("integer<H>", arg, msgbuf);
471 }
472 else
473 *p = (unsigned short) ival;
474 break;
475 }
Jack Jansend50338f2000-07-06 12:22:00 +0000476
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000477 case 'i': {/* signed int */
478 int *p = va_arg(*p_va, int *);
479 long ival = PyInt_AsLong(arg);
480 if (ival == -1 && PyErr_Occurred())
481 return converterr("integer<i>", arg, msgbuf);
482 else if (ival > INT_MAX) {
483 PyErr_SetString(PyExc_OverflowError,
484 "signed integer is greater than maximum");
485 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000486 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000487 else if (ival < INT_MIN) {
488 PyErr_SetString(PyExc_OverflowError,
489 "signed integer is less than minimum");
490 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000491 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000492 else
493 *p = ival;
494 break;
495 }
496
497 case 'l': {/* long int */
498 long *p = va_arg(*p_va, long *);
499 long ival = PyInt_AsLong(arg);
500 if (ival == -1 && PyErr_Occurred())
501 return converterr("integer<l>", arg, msgbuf);
502 else
503 *p = ival;
504 break;
505 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000506
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000507#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000508 case 'L': {/* LONG_LONG */
509 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
510 LONG_LONG ival = PyLong_AsLongLong( arg );
511 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
512 return converterr("long<L>", arg, msgbuf);
513 } else {
514 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000515 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000516 break;
517 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000518#endif
519
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000520 case 'f': {/* float */
521 float *p = va_arg(*p_va, float *);
522 double dval = PyFloat_AsDouble(arg);
523 if (PyErr_Occurred())
524 return converterr("float<f>", arg, msgbuf);
525 else
526 *p = (float) dval;
527 break;
528 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000529
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000530 case 'd': {/* double */
531 double *p = va_arg(*p_va, double *);
532 double dval = PyFloat_AsDouble(arg);
533 if (PyErr_Occurred())
534 return converterr("float<d>", arg, msgbuf);
535 else
536 *p = dval;
537 break;
538 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000539
Guido van Rossum530956d1996-07-21 02:27:43 +0000540#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000541 case 'D': {/* complex double */
542 Py_complex *p = va_arg(*p_va, Py_complex *);
543 Py_complex cval;
544 cval = PyComplex_AsCComplex(arg);
545 if (PyErr_Occurred())
546 return converterr("complex<D>", arg, msgbuf);
547 else
548 *p = cval;
549 break;
550 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000551#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000552
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000553 case 'c': {/* char */
554 char *p = va_arg(*p_va, char *);
555 if (PyString_Check(arg) && PyString_Size(arg) == 1)
556 *p = PyString_AsString(arg)[0];
557 else
558 return converterr("char", arg, msgbuf);
559 break;
560 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000561
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000562 case 's': {/* string */
563 if (*format == '#') {
564 void **p = (void **)va_arg(*p_va, char **);
565 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000566
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000567 if (PyString_Check(arg)) {
568 *p = PyString_AS_STRING(arg);
569 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000570 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000571#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000572 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000573 uarg = UNICODE_DEFAULT_ENCODING(arg);
574 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000575 return converterr(CONV_UNICODE,
576 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000577 *p = PyString_AS_STRING(uarg);
578 *q = PyString_GET_SIZE(uarg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000579 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000580#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000581 else { /* any buffer-like object */
582 char *buf;
583 int count = convertbuffer(arg, p, &buf);
584 if (count < 0)
585 return converterr(buf, arg, msgbuf);
586 *q = count;
587 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000588 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000589 } else {
590 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000591
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000592 if (PyString_Check(arg))
593 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000594#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000595 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000596 uarg = UNICODE_DEFAULT_ENCODING(arg);
597 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000598 return converterr(CONV_UNICODE,
599 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000600 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000601 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000602#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000603 else
604 return converterr("string", arg, msgbuf);
605 if ((int)strlen(*p) != PyString_Size(arg))
606 return converterr("string without null bytes",
607 arg, msgbuf);
608 }
609 break;
610 }
611
612 case 'z': {/* string, may be NULL (None) */
613 if (*format == '#') { /* any buffer-like object */
614 void **p = (void **)va_arg(*p_va, char **);
615 int *q = va_arg(*p_va, int *);
616
617 if (arg == Py_None) {
618 *p = 0;
619 *q = 0;
620 }
621 else if (PyString_Check(arg)) {
622 *p = PyString_AS_STRING(arg);
623 *q = PyString_GET_SIZE(arg);
624 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000625#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000626 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000627 uarg = UNICODE_DEFAULT_ENCODING(arg);
628 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000629 return converterr(CONV_UNICODE,
630 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000631 *p = PyString_AS_STRING(uarg);
632 *q = PyString_GET_SIZE(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000633 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000634#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000635 else { /* any buffer-like object */
636 char *buf;
637 int count = convertbuffer(arg, p, &buf);
638
639 if (count < 0)
640 return converterr(buf, arg, msgbuf);
641 *q = count;
642 }
643 format++;
644 } else {
645 char **p = va_arg(*p_va, char **);
646
647 if (arg == Py_None)
648 *p = 0;
649 else if (PyString_Check(arg))
650 *p = PyString_AsString(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000651#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000652 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000653 uarg = UNICODE_DEFAULT_ENCODING(arg);
654 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000655 return converterr(CONV_UNICODE,
656 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000657 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000658 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000659#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000660 else
661 return converterr("string or None",
662 arg, msgbuf);
663 if (*format == '#') {
664 int *q = va_arg(*p_va, int *);
665 if (arg == Py_None)
666 *q = 0;
667 else
668 *q = PyString_Size(arg);
669 format++;
670 }
671 else if (*p != NULL &&
672 (int)strlen(*p) != PyString_Size(arg))
673 return converterr(
674 "string without null bytes or None",
675 arg, msgbuf);
676 }
677 break;
678 }
679
680 case 'e': {/* encoded string */
681 char **buffer;
682 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000683 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000684 int size, recode_strings;
685
686 /* Get 'e' parameter: the encoding name */
687 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000688#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000689 if (encoding == NULL)
690 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000691#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000692
693 /* Get output buffer parameter:
694 's' (recode all objects via Unicode) or
695 't' (only recode non-string objects)
696 */
697 if (*format == 's')
698 recode_strings = 1;
699 else if (*format == 't')
700 recode_strings = 0;
701 else
702 return converterr(
703 "(unknown parser marker combination)",
704 arg, msgbuf);
705 buffer = (char **)va_arg(*p_va, char **);
706 format++;
707 if (buffer == NULL)
708 return converterr("(buffer is NULL)",
709 arg, msgbuf);
710
711 /* Encode object */
712 if (!recode_strings && PyString_Check(arg)) {
713 s = arg;
714 Py_INCREF(s);
715 }
716 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000717#ifdef Py_USING_UNICODE
718 PyObject *u;
719
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000720 /* Convert object to Unicode */
721 u = PyUnicode_FromObject(arg);
722 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000723 return converterr(
724 "string or unicode or text buffer",
725 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000726
727 /* Encode object; use default error handling */
728 s = PyUnicode_AsEncodedString(u,
729 encoding,
730 NULL);
731 Py_DECREF(u);
732 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000733 return converterr("(encoding failed)",
734 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000735 if (!PyString_Check(s)) {
736 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000737 return converterr(
738 "(encoder failed to return a string)",
739 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000740 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000741#else
742 return converterr("string<e>", arg, msgbuf);
743#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000744 }
745 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000746
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000747 /* Write output; output is guaranteed to be 0-terminated */
748 if (*format == '#') {
749 /* Using buffer length parameter '#':
750
751 - if *buffer is NULL, a new buffer of the
752 needed size is allocated and the data
753 copied into it; *buffer is updated to point
754 to the new buffer; the caller is
755 responsible for PyMem_Free()ing it after
756 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000757
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000758 - if *buffer is not NULL, the data is
759 copied to *buffer; *buffer_len has to be
760 set to the size of the buffer on input;
761 buffer overflow is signalled with an error;
762 buffer has to provide enough room for the
763 encoded string plus the trailing 0-byte
764
765 - in both cases, *buffer_len is updated to
766 the size of the buffer /excluding/ the
767 trailing 0-byte
768
769 */
770 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000771
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000772 format++;
773 if (buffer_len == NULL)
774 return converterr(
775 "(buffer_len is NULL)",
776 arg, msgbuf);
777 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000778 *buffer = PyMem_NEW(char, size + 1);
779 if (*buffer == NULL) {
780 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000781 return converterr(
782 "(memory error)",
783 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000784 }
Fred Drake25871c02000-05-03 15:17:02 +0000785 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000786 if (size + 1 > *buffer_len) {
787 Py_DECREF(s);
788 return converterr(
789 "(buffer overflow)",
790 arg, msgbuf);
791 }
Fred Drake25871c02000-05-03 15:17:02 +0000792 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000793 memcpy(*buffer,
794 PyString_AS_STRING(s),
795 size + 1);
796 *buffer_len = size;
797 } else {
798 /* Using a 0-terminated buffer:
799
800 - the encoded string has to be 0-terminated
801 for this variant to work; if it is not, an
802 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000803
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000804 - a new buffer of the needed size is
805 allocated and the data copied into it;
806 *buffer is updated to point to the new
807 buffer; the caller is responsible for
808 PyMem_Free()ing it after usage
809
810 */
811 if ((int)strlen(PyString_AS_STRING(s)) != size)
812 return converterr(
813 "(encoded string without NULL bytes)",
814 arg, msgbuf);
815 *buffer = PyMem_NEW(char, size + 1);
816 if (*buffer == NULL) {
817 Py_DECREF(s);
818 return converterr("(memory error)",
819 arg, msgbuf);
820 }
821 memcpy(*buffer,
822 PyString_AS_STRING(s),
823 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000824 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000825 Py_DECREF(s);
826 break;
827 }
828
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000829#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000830 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
831 if (*format == '#') { /* any buffer-like object */
832 void **p = (void **)va_arg(*p_va, char **);
833 int *q = va_arg(*p_va, int *);
834 char *buf;
835 int count = convertbuffer(arg, p, &buf);
836
837 if (count < 0)
838 return converterr(buf, arg, msgbuf);
839 *q = count/(sizeof(Py_UNICODE));
840 format++;
841 } else {
842 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
843
Guido van Rossume826ef02000-03-10 23:02:17 +0000844 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000845 *p = PyUnicode_AS_UNICODE(arg);
846 else
847 return converterr("unicode", arg, msgbuf);
848 }
849 break;
850 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000851#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000852
853 case 'S': { /* string object */
854 PyObject **p = va_arg(*p_va, PyObject **);
855 if (PyString_Check(arg))
856 *p = arg;
857 else
858 return converterr("string", arg, msgbuf);
859 break;
860 }
861
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000862#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000863 case 'U': { /* Unicode object */
864 PyObject **p = va_arg(*p_va, PyObject **);
865 if (PyUnicode_Check(arg))
866 *p = arg;
867 else
868 return converterr("unicode", arg, msgbuf);
869 break;
870 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000871#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000872
873 case 'O': { /* object */
874 PyTypeObject *type;
875 PyObject **p;
876 if (*format == '!') {
877 type = va_arg(*p_va, PyTypeObject*);
878 p = va_arg(*p_va, PyObject **);
879 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +0000880 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +0000881 *p = arg;
882 else
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000883 return converterr(type->tp_name, arg, msgbuf);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000884
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000885 }
886 else if (*format == '?') {
887 inquiry pred = va_arg(*p_va, inquiry);
888 p = va_arg(*p_va, PyObject **);
889 format++;
890 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000891 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000892 else
893 return converterr("(unspecified)",
894 arg, msgbuf);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000895
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000896 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000897 else if (*format == '&') {
898 typedef int (*converter)(PyObject *, void *);
899 converter convert = va_arg(*p_va, converter);
900 void *addr = va_arg(*p_va, void *);
901 format++;
902 if (! (*convert)(arg, addr))
903 return converterr("(unspecified)",
904 arg, msgbuf);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000905 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000906 else {
907 p = va_arg(*p_va, PyObject **);
908 *p = arg;
909 }
910 break;
911 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000912
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000913
914 case 'w': { /* memory buffer, read-write access */
915 void **p = va_arg(*p_va, void **);
916 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
917 int count;
918
919 if (pb == NULL ||
920 pb->bf_getwritebuffer == NULL ||
921 pb->bf_getsegcount == NULL)
922 return converterr("read-write buffer", arg, msgbuf);
923 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
924 return converterr("single-segment read-write buffer",
925 arg, msgbuf);
926 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
927 return converterr("(unspecified)", arg, msgbuf);
928 if (*format == '#') {
929 int *q = va_arg(*p_va, int *);
930
931 *q = count;
932 format++;
933 }
934 break;
935 }
936
937 case 't': { /* 8-bit character buffer, read-only access */
938 const char **p = va_arg(*p_va, const char **);
939 char *buf;
940 int count;
941
942 if (*format++ != '#')
943 return converterr(
944 "invalid use of 't' format character",
945 arg, msgbuf);
946 if (!PyType_HasFeature(arg->ob_type,
947 Py_TPFLAGS_HAVE_GETCHARBUFFER))
948 return converterr(
949 "string or read-only character buffer",
950 arg, msgbuf);
951
952 count = convertbuffer(arg, (void **)p, &buf);
953 if (count < 0)
954 return converterr(buf, arg, msgbuf);
955 *va_arg(*p_va, int *) = count;
956 break;
957 }
958
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000959 default:
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000960 return converterr("impossible<bad format char>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000961
962 }
963
964 *p_format = format;
965 return NULL;
966}
Guido van Rossumaa354651996-08-19 19:32:04 +0000967
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000968int convertbuffer(PyObject *arg, void **p, char **errmsg)
969{
970 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
971 int count;
972 if (pb == NULL ||
973 pb->bf_getreadbuffer == NULL ||
974 pb->bf_getsegcount == NULL) {
975 *errmsg = "string or read-only buffer";
976 return -1;
977 }
978 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
979 *errmsg = "string or single-segment read-only buffer";
980 return -1;
981 }
982 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
983 *errmsg = "(unspecified)";
984 }
985 return count;
986}
Guido van Rossumaa354651996-08-19 19:32:04 +0000987
988/* Support for keyword arguments donated by
989 Geoff Philbrick <philbric@delphi.hks.com> */
990
Guido van Rossum79f25d91997-04-29 20:08:16 +0000991int PyArg_ParseTupleAndKeywords(PyObject *args,
992 PyObject *keywords,
Guido van Rossumaa354651996-08-19 19:32:04 +0000993 char *format,
994 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +0000995{
996 int retval;
997 va_list va;
Guido van Rossumaa354651996-08-19 19:32:04 +0000998
999 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001000 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
1001 va_end(va);
1002 return retval;
1003}
1004
1005
1006static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001007vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
1008 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001009{
1010 char msgbuf[256];
1011 int levels[32];
1012 char *fname = NULL;
1013 char *message = NULL;
1014 int min = -1;
1015 int max = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001016 char *formatsave = format;
1017 int i, len, tplen, kwlen;
1018 char *msg, *ks, **p;
1019 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001020 PyObject *key, *value;
Guido van Rossumaa354651996-08-19 19:32:04 +00001021
1022 /* nested tuples cannot be parsed when using keyword arguments */
1023
1024 for (;;) {
1025 int c = *format++;
1026 if (c == '(') {
1027 PyErr_SetString(PyExc_SystemError,
1028 "tuple found in format when using keyword arguments");
1029 return 0;
1030 }
1031 else if (c == '\0')
1032 break;
1033 else if (c == ':') {
1034 fname = format;
1035 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001036 } else if (c == ';') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001037 message = format;
1038 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001039 } else if (c == 'e')
Marc-André Lemburgbbcf2a72000-09-08 11:49:37 +00001040 ; /* Pass */
Guido van Rossumaa354651996-08-19 19:32:04 +00001041 else if (isalpha(c))
1042 max++;
1043 else if (c == '|')
1044 min = max;
1045 }
1046
1047 if (min < 0)
1048 min = max;
1049
1050 format = formatsave;
1051
1052 if (!PyTuple_Check(args)) {
1053 PyErr_SetString(PyExc_SystemError,
1054 "new style getargs format but argument is not a tuple");
1055 return 0;
1056 }
1057
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001058 tplen = PyTuple_GET_SIZE(args);
Guido van Rossumaa354651996-08-19 19:32:04 +00001059
1060 /* do a cursory check of the keywords just to see how many we got */
1061
1062 if (keywords) {
1063 if (!PyDict_Check(keywords)) {
Jeremy Hyltona0ac40c2001-01-25 20:13:10 +00001064 if (keywords == NULL)
1065 PyErr_SetString(PyExc_SystemError,
1066 "NULL received when keyword dictionary expected");
1067 else
1068 PyErr_Format(PyExc_SystemError,
1069 "%s received when keyword dictionary expected",
1070 keywords->ob_type->tp_name);
Guido van Rossumaa354651996-08-19 19:32:04 +00001071 return 0;
1072 }
1073 kwlen = PyDict_Size(keywords);
1074 }
1075 else {
1076 kwlen = 0;
1077 }
1078
1079 /* make sure there are no duplicate values for an argument;
1080 its not clear when to use the term "keyword argument vs.
1081 keyword parameter in messages */
1082
1083 if (keywords) {
1084 for (i = 0; i < tplen; i++) {
1085 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1086 sprintf(msgbuf,
1087 "keyword parameter %s redefined",
1088 kwlist[i]);
1089 PyErr_SetString(PyExc_TypeError, msgbuf);
1090 return 0;
1091 }
1092 }
1093 }
1094 PyErr_Clear(); /* I'm not which Py functions set the error string */
1095
1096 /* required arguments missing from args can be supplied by keyword
1097 arguments */
1098
1099 len = tplen;
1100 if (keywords && tplen < min) {
1101 for (i = tplen; i < min; i++) {
1102 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1103 len++;
1104 }
1105 }
1106 }
1107 PyErr_Clear();
1108
1109 /* make sure we got an acceptable number of arguments; the message
1110 is a little confusing with keywords since keyword arguments
1111 which are supplied, but don't match the required arguments
1112 are not included in the "%d given" part of the message */
1113
1114 if (len < min || max < len) {
1115 if (message == NULL) {
1116 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001117 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001118 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001119 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001120 min==max ? "exactly"
1121 : len < min ? "at least" : "at most",
1122 len < min ? min : max,
1123 (len < min ? min : max) == 1 ? "" : "s",
1124 len);
1125 message = msgbuf;
1126 }
1127 PyErr_SetString(PyExc_TypeError, message);
1128 return 0;
1129 }
1130
1131 for (i = 0; i < tplen; i++) {
1132 if (*format == '|')
1133 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001134 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001135 levels, msgbuf);
1136 if (msg) {
1137 seterror(i+1, msg, levels, fname, message);
1138 return 0;
1139 }
1140 }
1141
1142 /* handle no keyword parameters in call */
1143
1144 if (!keywords) return 1;
1145
1146 /* make sure the number of keywords in the keyword list matches the
1147 number of items in the format string */
1148
1149 nkwds = 0;
1150 p = kwlist;
1151 for (;;) {
1152 if (!*(p++)) break;
1153 nkwds++;
1154 }
1155
1156 if (nkwds != max) {
1157 PyErr_SetString(PyExc_SystemError,
1158 "number of items in format string and keyword list do not match");
1159 return 0;
1160 }
1161
1162 /* convert the keyword arguments; this uses the format
1163 string where it was left after processing args */
1164
1165 converted = 0;
1166 for (i = tplen; i < nkwds; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001168 if (*format == '|')
1169 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001170 item = PyMapping_GetItemString(keywords, kwlist[i]);
1171 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001172 msg = convertitem(item, &format, p_va, levels, msgbuf);
1173 if (msg) {
1174 seterror(i+1, msg, levels, fname, message);
1175 return 0;
1176 }
1177 converted++;
Barry Warsaw07050282000-12-11 20:01:55 +00001178 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001179 }
1180 else {
1181 PyErr_Clear();
1182 msg = skipitem(&format, p_va);
1183 if (msg) {
1184 seterror(i+1, msg, levels, fname, message);
1185 return 0;
1186 }
1187 }
1188 }
1189
1190 /* make sure there are no extraneous keyword arguments */
1191
1192 pos = 0;
1193 if (converted < kwlen) {
1194 while (PyDict_Next(keywords, &pos, &key, &value)) {
1195 match = 0;
1196 ks = PyString_AsString(key);
1197 for (i = 0; i < nkwds; i++) {
1198 if (!strcmp(ks, kwlist[i])) {
1199 match = 1;
1200 break;
1201 }
1202 }
1203 if (!match) {
1204 sprintf(msgbuf,
Guido van Rossum80dc16b2000-05-08 14:02:41 +00001205 "%s is an invalid keyword argument for this function",
Guido van Rossumaa354651996-08-19 19:32:04 +00001206 ks);
1207 PyErr_SetString(PyExc_TypeError, msgbuf);
1208 return 0;
1209 }
1210 }
1211 }
1212
1213 return 1;
1214}
1215
1216
1217static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001218skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001219{
1220 char *format = *p_format;
1221 char c = *format++;
1222
1223 switch (c) {
1224
1225 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001226 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001227 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001228 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001229 break;
1230 }
1231
1232 case 'h': /* short int */
1233 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001234 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001235 break;
1236 }
1237
Jack Jansencc22fbe2000-08-05 21:29:58 +00001238 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001239 {
1240 (void) va_arg(*p_va, unsigned short *);
1241 break;
1242 }
1243
Guido van Rossumaa354651996-08-19 19:32:04 +00001244 case 'i': /* int */
1245 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001246 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001247 break;
1248 }
1249
1250 case 'l': /* long int */
1251 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001252 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001253 break;
1254 }
1255
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001256#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001257 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001258 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001259 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001260 break;
1261 }
1262#endif
1263
Guido van Rossumaa354651996-08-19 19:32:04 +00001264 case 'f': /* float */
1265 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001266 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001267 break;
1268 }
1269
1270 case 'd': /* double */
1271 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001272 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001273 break;
1274 }
1275
1276#ifndef WITHOUT_COMPLEX
1277 case 'D': /* complex double */
1278 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001279 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001280 break;
1281 }
1282#endif /* WITHOUT_COMPLEX */
1283
1284 case 'c': /* char */
1285 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001286 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001287 break;
1288 }
1289
1290 case 's': /* string */
1291 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001292 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001293 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001294 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001295 format++;
1296 }
1297 break;
1298 }
1299
1300 case 'z': /* string */
1301 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001302 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001303 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001304 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001305 format++;
1306 }
1307 break;
1308 }
1309
1310 case 'S': /* string object */
1311 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001312 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001313 break;
1314 }
1315
1316 case 'O': /* object */
1317 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001318 if (*format == '!') {
1319 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001320 (void) va_arg(*p_va, PyTypeObject*);
1321 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001322 }
1323#if 0
1324/* I don't know what this is for */
1325 else if (*format == '?') {
1326 inquiry pred = va_arg(*p_va, inquiry);
1327 format++;
1328 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001329 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001330 }
1331 }
1332#endif
1333 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001334 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001335 (void) va_arg(*p_va, converter);
1336 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001337 format++;
1338 }
1339 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001340 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001341 }
1342 break;
1343 }
1344
1345 default:
1346 return "impossible<bad format char>";
1347
1348 }
1349
1350 *p_format = format;
1351 return NULL;
1352}