blob: aa4a22828d26d947d7ea3ae4e2d472898ebd5ae9 [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 *);
28static char *convertsimple1(PyObject *, char **, va_list *);
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
87 for (;;) {
88 int c = *format++;
89 if (c == '(' /* ')' */) {
90 if (level == 0)
91 max++;
92 level++;
93 }
94 else if (/* '(' */ c == ')') {
95 if (level == 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +000096 Py_FatalError(/* '(' */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000097 "excess ')' in getargs format");
98 else
99 level--;
100 }
101 else if (c == '\0')
102 break;
103 else if (c == ':') {
104 fname = format;
105 break;
106 }
107 else if (c == ';') {
108 message = format;
109 break;
110 }
111 else if (level != 0)
112 ; /* Pass */
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000113 else if (c == 'e')
114 ; /* Pass */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000115 else if (isalpha(c))
116 max++;
117 else if (c == '|')
118 min = max;
119 }
120
121 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000122 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000123
124 if (min < 0)
125 min = max;
126
127 format = formatsave;
128
129 if (compat) {
130 if (max == 0) {
131 if (args == NULL)
132 return 1;
Ka-Ping Yee20579702001-01-15 22:14:16 +0000133 sprintf(msgbuf, "%s%s takes no arguments",
134 fname==NULL ? "function" : fname,
135 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000136 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000137 return 0;
138 }
139 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000140 if (args == NULL) {
141 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000142 "%s%s takes at least one argument",
143 fname==NULL ? "function" : fname,
144 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000145 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000146 return 0;
147 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000148 msg = convertitem(args, &format, p_va, levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000149 if (msg == NULL)
150 return 1;
151 seterror(levels[0], msg, levels+1, fname, message);
152 return 0;
153 }
154 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000155 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000156 "old style getargs format uses new features");
157 return 0;
158 }
159 }
160
Guido van Rossum79f25d91997-04-29 20:08:16 +0000161 if (!PyTuple_Check(args)) {
162 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000163 "new style getargs format but argument is not a tuple");
164 return 0;
165 }
166
Guido van Rossum79f25d91997-04-29 20:08:16 +0000167 len = PyTuple_Size(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000168
169 if (len < min || max < len) {
170 if (message == NULL) {
171 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000172 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000173 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000174 fname==NULL ? "" : "()",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000175 min==max ? "exactly"
176 : len < min ? "at least" : "at most",
177 len < min ? min : max,
178 (len < min ? min : max) == 1 ? "" : "s",
179 len);
180 message = msgbuf;
181 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000182 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000183 return 0;
184 }
185
186 for (i = 0; i < len; i++) {
187 if (*format == '|')
188 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000189 msg = convertitem(PyTuple_GetItem(args, i), &format, p_va,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000190 levels, msgbuf);
191 if (msg) {
192 seterror(i+1, msg, levels, fname, message);
193 return 0;
194 }
195 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000196
Guido van Rossum730806d1998-04-10 22:27:42 +0000197 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000198 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000199 *format != '|' && *format != ':' && *format != ';') {
200 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000201 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000202 return 0;
203 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000204
205 return 1;
206}
207
208
209
210static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000211seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000212{
213 char buf[256];
214 int i;
215 char *p = buf;
216
Guido van Rossum79f25d91997-04-29 20:08:16 +0000217 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000218 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000219 else if (message == NULL) {
220 if (fname != NULL) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000221 sprintf(p, "%s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000222 p += strlen(p);
223 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000224 if (iarg != 0) {
225 sprintf(p, "argument %d", iarg);
226 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000227 p += strlen(p);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000228 while (levels[i] > 0) {
229 sprintf(p, ", item %d", levels[i]-1);
230 p += strlen(p);
231 i++;
232 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000233 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000234 else {
235 sprintf(p, "argument");
236 p += strlen(p);
237 }
238 sprintf(p, " %s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000239 message = buf;
240 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000241 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000242}
243
244
245/* Convert a tuple argument.
246 On entry, *p_format points to the character _after_ the opening '('.
247 On successful exit, *p_format points to the closing ')'.
248 If successful:
249 *p_format and *p_va are updated,
250 *levels and *msgbuf are untouched,
251 and NULL is returned.
252 If the argument is invalid:
253 *p_format is unchanged,
254 *p_va is undefined,
255 *levels is a 0-terminated list of item numbers,
256 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000257 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000258 <typename1> is the name of the expected type, and
259 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000260 and msgbuf is returned.
261*/
262
263static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
265 char *msgbuf, int toplevel)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000266{
267 int level = 0;
268 int n = 0;
269 char *format = *p_format;
270 int i;
271
272 for (;;) {
273 int c = *format++;
274 if (c == '(') {
275 if (level == 0)
276 n++;
277 level++;
278 }
279 else if (c == ')') {
280 if (level == 0)
281 break;
282 level--;
283 }
284 else if (c == ':' || c == ';' || c == '\0')
285 break;
286 else if (level == 0 && isalpha(c))
287 n++;
288 }
289
Ka-Ping Yee20579702001-01-15 22:14:16 +0000290 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000291 levels[0] = 0;
292 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000293 toplevel ? "expected %d arguments, not %s" :
294 "must be %d-item sequence, not %s",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000295 n, arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000296 return msgbuf;
297 }
298
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000299 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000300 levels[0] = 0;
301 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000302 toplevel ? "expected %d arguments, not %d" :
303 "must be sequence of length %d, not %d",
304 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000305 return msgbuf;
306 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000307
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000308 format = *p_format;
309 for (i = 0; i < n; i++) {
310 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000311 PyObject *item;
312 item = PySequence_GetItem(arg, i);
313 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
314 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
315 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000316 if (msg != NULL) {
317 levels[0] = i+1;
318 return msg;
319 }
320 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000321
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000322 *p_format = format;
323 return NULL;
324}
325
326
327/* Convert a single item. */
328
329static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000330convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
331 char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000332{
333 char *msg;
334 char *format = *p_format;
335
336 if (*format == '(' /* ')' */) {
337 format++;
338 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
339 if (msg == NULL)
340 format++;
341 }
342 else {
343 msg = convertsimple(arg, &format, p_va, msgbuf);
344 if (msg != NULL)
345 levels[0] = 0;
346 }
347 if (msg == NULL)
348 *p_format = format;
349 return msg;
350}
351
352
353/* Convert a non-tuple argument. Adds to convertsimple1 functionality
Ka-Ping Yee20579702001-01-15 22:14:16 +0000354 by formatting messages as "must be <desired type>, not <actual type>". */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000355
356static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000358{
359 char *msg = convertsimple1(arg, p_format, p_va);
360 if (msg != NULL) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000361 sprintf(msgbuf, "must be %.50s, not %.50s", msg,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000362 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000363 msg = msgbuf;
364 }
365 return msg;
366}
367
368
Guido van Rossum700c6ff2000-04-27 20:13:18 +0000369/* Internal API needed by convertsimple1(): */
370extern
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000371PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
Guido van Rossum700c6ff2000-04-27 20:13:18 +0000372 const char *errors);
373
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000374/* Convert a non-tuple argument. Return NULL if conversion went OK,
375 or a string representing the expected type if the conversion failed.
376 When failing, an exception may or may not have been raised.
377 Don't call if a tuple is expected. */
378
379static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000380convertsimple1(PyObject *arg, char **p_format, va_list *p_va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000381{
382 char *format = *p_format;
383 char c = *format++;
384
385 switch (c) {
386
Fred Drake230cae72000-05-09 21:50:00 +0000387 case 'b': /* unsigned byte -- very short int */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000388 {
389 char *p = va_arg(*p_va, char *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000390 long ival = PyInt_AsLong(arg);
391 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000392 return "integer<b>";
Fred Drake230cae72000-05-09 21:50:00 +0000393 else if (ival < 0) {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000394 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000395 "unsigned byte integer is less than minimum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000396 return "integer<b>";
397 }
Fred Drake230cae72000-05-09 21:50:00 +0000398 else if (ival > UCHAR_MAX) {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000399 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000400 "unsigned byte integer is greater than maximum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000401 return "integer<b>";
402 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000403 else
Fred Drake230cae72000-05-09 21:50:00 +0000404 *p = (unsigned char) ival;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000405 break;
406 }
407
Jack Jansencc22fbe2000-08-05 21:29:58 +0000408 case 'B': /* byte sized bitfield - both signed and unsigned values allowed */
409 {
410 char *p = va_arg(*p_va, char *);
411 long ival = PyInt_AsLong(arg);
412 if (ival == -1 && PyErr_Occurred())
413 return "integer<b>";
414 else if (ival < SCHAR_MIN) {
415 PyErr_SetString(PyExc_OverflowError,
416 "byte-sized integer bitfield is less than minimum");
417 return "integer<B>";
418 }
Jack Jansena454ebd2000-09-15 12:52:19 +0000419 else if (ival > (int)UCHAR_MAX) {
Jack Jansencc22fbe2000-08-05 21:29:58 +0000420 PyErr_SetString(PyExc_OverflowError,
421 "byte-sized integer bitfield is greater than maximum");
422 return "integer<B>";
423 }
424 else
425 *p = (unsigned char) ival;
426 break;
427 }
428
Fred Drake230cae72000-05-09 21:50:00 +0000429 case 'h': /* signed short int */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000430 {
431 short *p = va_arg(*p_va, short *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000432 long ival = PyInt_AsLong(arg);
433 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000434 return "integer<h>";
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000435 else if (ival < SHRT_MIN) {
436 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000437 "signed short integer is less than minimum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000438 return "integer<h>";
439 }
440 else if (ival > SHRT_MAX) {
441 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000442 "signed short integer is greater than maximum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000443 return "integer<h>";
444 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000445 else
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000446 *p = (short) ival;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000447 break;
448 }
449
Jack Jansencc22fbe2000-08-05 21:29:58 +0000450 case 'H': /* short int sized bitfield, both signed and unsigned allowed */
Jack Jansend50338f2000-07-06 12:22:00 +0000451 {
452 unsigned short *p = va_arg(*p_va, unsigned short *);
453 long ival = PyInt_AsLong(arg);
454 if (ival == -1 && PyErr_Occurred())
455 return "integer<H>";
Jack Jansencc22fbe2000-08-05 21:29:58 +0000456 else if (ival < SHRT_MIN) {
Jack Jansend50338f2000-07-06 12:22:00 +0000457 PyErr_SetString(PyExc_OverflowError,
Jack Jansencc22fbe2000-08-05 21:29:58 +0000458 "short integer bitfield is less than minimum");
Jack Jansend50338f2000-07-06 12:22:00 +0000459 return "integer<H>";
460 }
461 else if (ival > USHRT_MAX) {
462 PyErr_SetString(PyExc_OverflowError,
Jack Jansencc22fbe2000-08-05 21:29:58 +0000463 "short integer bitfield is greater than maximum");
Jack Jansend50338f2000-07-06 12:22:00 +0000464 return "integer<H>";
465 }
466 else
467 *p = (unsigned short) ival;
468 break;
469 }
470
Fred Drake230cae72000-05-09 21:50:00 +0000471 case 'i': /* signed int */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000472 {
473 int *p = va_arg(*p_va, int *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000474 long ival = PyInt_AsLong(arg);
475 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000476 return "integer<i>";
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000477 else if (ival > INT_MAX) {
478 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000479 "signed integer is greater than maximum");
480 return "integer<i>";
481 }
482 else if (ival < INT_MIN) {
483 PyErr_SetString(PyExc_OverflowError,
484 "signed integer is less than minimum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000485 return "integer<i>";
486 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000487 else
488 *p = ival;
489 break;
490 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000491 case 'l': /* long int */
492 {
493 long *p = va_arg(*p_va, long *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000494 long ival = PyInt_AsLong(arg);
495 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000496 return "integer<l>";
497 else
498 *p = ival;
499 break;
500 }
501
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000502#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +0000503 case 'L': /* LONG_LONG */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000504 {
Guido van Rossum3293b071998-08-25 16:07:15 +0000505 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
506 LONG_LONG ival = PyLong_AsLongLong( arg );
507 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000508 return "long<L>";
509 } else {
510 *p = ival;
511 }
512 break;
513 }
514#endif
515
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000516 case 'f': /* float */
517 {
518 float *p = va_arg(*p_va, float *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000519 double dval = PyFloat_AsDouble(arg);
520 if (PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000521 return "float<f>";
522 else
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000523 *p = (float) dval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000524 break;
525 }
526
527 case 'd': /* double */
528 {
529 double *p = va_arg(*p_va, double *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000530 double dval = PyFloat_AsDouble(arg);
531 if (PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000532 return "float<d>";
533 else
534 *p = dval;
535 break;
536 }
537
Guido van Rossum530956d1996-07-21 02:27:43 +0000538#ifndef WITHOUT_COMPLEX
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000539 case 'D': /* complex double */
540 {
Guido van Rossum530956d1996-07-21 02:27:43 +0000541 Py_complex *p = va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000542 Py_complex cval;
543 cval = PyComplex_AsCComplex(arg);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000544 if (PyErr_Occurred())
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000545 return "complex<D>";
546 else
547 *p = cval;
548 break;
549 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000550#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000551
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000552 case 'c': /* char */
553 {
554 char *p = va_arg(*p_va, char *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 if (PyString_Check(arg) && PyString_Size(arg) == 1)
556 *p = PyString_AsString(arg)[0];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000557 else
558 return "char";
559 break;
560 }
561
562 case 's': /* string */
563 {
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000564 if (*format == '#') {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000565 void **p = (void **)va_arg(*p_va, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000566 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000567
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000568 if (PyString_Check(arg)) {
569 *p = PyString_AS_STRING(arg);
570 *q = PyString_GET_SIZE(arg);
571 }
572 else if (PyUnicode_Check(arg)) {
573 arg = _PyUnicode_AsDefaultEncodedString(
574 arg, NULL);
575 if (arg == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000576 return "(unicode conversion error)";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000577 *p = PyString_AS_STRING(arg);
578 *q = PyString_GET_SIZE(arg);
579 }
580 else { /* any buffer-like object */
581 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
582 int count;
583 if ( pb == NULL ||
584 pb->bf_getreadbuffer == NULL ||
585 pb->bf_getsegcount == NULL )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000586 return "string or read-only buffer";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000587 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000588 return "string or single-segment read-only buffer";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000589 if ( (count =
590 (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
591 return "(unspecified)";
592 *q = count;
593 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000594 format++;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000595 } else {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000596 char **p = va_arg(*p_va, char **);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000597
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000598 if (PyString_Check(arg))
Guido van Rossume826ef02000-03-10 23:02:17 +0000599 *p = PyString_AS_STRING(arg);
600 else if (PyUnicode_Check(arg)) {
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000601 arg = _PyUnicode_AsDefaultEncodedString(
602 arg, NULL);
Guido van Rossume826ef02000-03-10 23:02:17 +0000603 if (arg == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000604 return "(unicode conversion error)";
Guido van Rossume826ef02000-03-10 23:02:17 +0000605 *p = PyString_AS_STRING(arg);
606 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000607 else
608 return "string";
609 if ((int)strlen(*p) != PyString_Size(arg))
610 return "string without null bytes";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000611 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000612 break;
613 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000614
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000615 case 'z': /* string, may be NULL (None) */
616 {
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000617 if (*format == '#') { /* any buffer-like object */
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000618 void **p = (void **)va_arg(*p_va, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000619 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000620
621 if (arg == Py_None) {
622 *p = 0;
623 *q = 0;
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000624 }
625 else if (PyString_Check(arg)) {
626 *p = PyString_AS_STRING(arg);
627 *q = PyString_GET_SIZE(arg);
628 }
629 else if (PyUnicode_Check(arg)) {
630 arg = _PyUnicode_AsDefaultEncodedString(
631 arg, NULL);
632 if (arg == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000633 return "(unicode conversion error)";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000634 *p = PyString_AS_STRING(arg);
635 *q = PyString_GET_SIZE(arg);
636 }
637 else { /* any buffer-like object */
638 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
639 int count;
640 if ( pb == NULL ||
641 pb->bf_getreadbuffer == NULL ||
642 pb->bf_getsegcount == NULL )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000643 return "string or read-only buffer";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000644 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000645 return "string or single-segment read-only buffer";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000646 if ( (count =
647 (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
648 return "(unspecified)";
649 *q = count;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000650 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000651 format++;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000652 } else {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000653 char **p = va_arg(*p_va, char **);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000654
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000655 if (arg == Py_None)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000656 *p = 0;
657 else if (PyString_Check(arg))
658 *p = PyString_AsString(arg);
Guido van Rossume826ef02000-03-10 23:02:17 +0000659 else if (PyUnicode_Check(arg)) {
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000660 arg = _PyUnicode_AsDefaultEncodedString(
661 arg, NULL);
Guido van Rossume826ef02000-03-10 23:02:17 +0000662 if (arg == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000663 return "(unicode conversion error)";
Guido van Rossume826ef02000-03-10 23:02:17 +0000664 *p = PyString_AS_STRING(arg);
665 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000666 else
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000667 return "string or None";
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000668 if (*format == '#') {
669 int *q = va_arg(*p_va, int *);
670 if (arg == Py_None)
671 *q = 0;
672 else
673 *q = PyString_Size(arg);
674 format++;
675 }
676 else if (*p != NULL &&
677 (int)strlen(*p) != PyString_Size(arg))
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000678 return "string without null bytes or None";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000679 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000680 break;
681 }
682
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000683 case 'e': /* encoded string */
684 {
685 char **buffer;
686 const char *encoding;
687 PyObject *u, *s;
688 int size;
689
690 /* Get 'e' parameter: the encoding name */
691 encoding = (const char *)va_arg(*p_va, const char *);
692 if (encoding == NULL)
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000693 encoding = PyUnicode_GetDefaultEncoding();
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000694
695 /* Get 's' parameter: the output buffer to use */
696 if (*format != 's')
Guido van Rossum50fbb152000-03-28 02:00:29 +0000697 return "(unknown parser marker combination)";
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000698 buffer = (char **)va_arg(*p_va, char **);
699 format++;
700 if (buffer == NULL)
701 return "(buffer is NULL)";
702
703 /* Convert object to Unicode */
704 u = PyUnicode_FromObject(arg);
705 if (u == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000706 return "string or unicode or text buffer";
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000707
708 /* Encode object; use default error handling */
709 s = PyUnicode_AsEncodedString(u,
710 encoding,
711 NULL);
712 Py_DECREF(u);
713 if (s == NULL)
714 return "(encoding failed)";
715 if (!PyString_Check(s)) {
716 Py_DECREF(s);
717 return "(encoder failed to return a string)";
718 }
719 size = PyString_GET_SIZE(s);
720
721 /* Write output; output is guaranteed to be
722 0-terminated */
723 if (*format == '#') {
724 /* Using buffer length parameter '#':
725
726 - if *buffer is NULL, a new buffer
727 of the needed size is allocated and
728 the data copied into it; *buffer is
729 updated to point to the new buffer;
730 the caller is responsible for
Guido van Rossum24bdb042000-03-28 20:29:59 +0000731 PyMem_Free()ing it after usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000732
733 - if *buffer is not NULL, the data
734 is copied to *buffer; *buffer_len
735 has to be set to the size of the
736 buffer on input; buffer overflow is
737 signalled with an error; buffer has
738 to provide enough room for the
739 encoded string plus the trailing
740 0-byte
741
742 - in both cases, *buffer_len is
743 updated to the size of the buffer
744 /excluding/ the trailing 0-byte
745
746 */
747 int *buffer_len = va_arg(*p_va, int *);
748
749 format++;
750 if (buffer_len == NULL)
751 return "(buffer_len is NULL)";
752 if (*buffer == NULL) {
753 *buffer = PyMem_NEW(char, size + 1);
754 if (*buffer == NULL) {
755 Py_DECREF(s);
756 return "(memory error)";
757 }
758 } else {
759 if (size + 1 > *buffer_len) {
760 Py_DECREF(s);
761 return "(buffer overflow)";
762 }
763 }
764 memcpy(*buffer,
765 PyString_AS_STRING(s),
766 size + 1);
767 *buffer_len = size;
768 } else {
769 /* Using a 0-terminated buffer:
770
771 - the encoded string has to be
772 0-terminated for this variant to
773 work; if it is not, an error raised
774
775 - a new buffer of the needed size
776 is allocated and the data copied
777 into it; *buffer is updated to
778 point to the new buffer; the caller
Guido van Rossum24bdb042000-03-28 20:29:59 +0000779 is responsible for PyMem_Free()ing it
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000780 after usage
781
782 */
Guido van Rossum50fbb152000-03-28 02:00:29 +0000783 if ((int)strlen(PyString_AS_STRING(s)) != size)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000784 return "(encoded string without NULL bytes)";
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000785 *buffer = PyMem_NEW(char, size + 1);
786 if (*buffer == NULL) {
787 Py_DECREF(s);
788 return "(memory error)";
789 }
790 memcpy(*buffer,
791 PyString_AS_STRING(s),
792 size + 1);
793 }
794 Py_DECREF(s);
795 break;
796 }
797
Fred Drake25871c02000-05-03 15:17:02 +0000798 case 'u': /* raw unicode buffer (Py_UNICODE *) */
799 {
800 if (*format == '#') { /* any buffer-like object */
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000801 void **p = (void **)va_arg(*p_va, char **);
802 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Fred Drake25871c02000-05-03 15:17:02 +0000803 int *q = va_arg(*p_va, int *);
804 int count;
805
806 if ( pb == NULL ||
807 pb->bf_getreadbuffer == NULL ||
808 pb->bf_getsegcount == NULL )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000809 return "unicode or read-only buffer";
Fred Drake25871c02000-05-03 15:17:02 +0000810 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000811 return "unicode or single-segment read-only buffer";
Fred Drake25871c02000-05-03 15:17:02 +0000812 if ( (count =
813 (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
814 return "(unspecified)";
815 /* buffer interface returns bytes, we want
816 length in characters */
817 *q = count/(sizeof(Py_UNICODE));
818 format++;
819 } else {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000820 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Fred Drake25871c02000-05-03 15:17:02 +0000821
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000822 if (PyUnicode_Check(arg))
Fred Drake25871c02000-05-03 15:17:02 +0000823 *p = PyUnicode_AS_UNICODE(arg);
824 else
825 return "unicode";
826 }
827 break;
828 }
829
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000830 case 'S': /* string object */
831 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000832 PyObject **p = va_arg(*p_va, PyObject **);
833 if (PyString_Check(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000834 *p = arg;
835 else
836 return "string";
837 break;
838 }
839
Guido van Rossume826ef02000-03-10 23:02:17 +0000840 case 'U': /* Unicode object */
841 {
842 PyObject **p = va_arg(*p_va, PyObject **);
843 if (PyUnicode_Check(arg))
844 *p = arg;
845 else
846 return "unicode";
847 break;
848 }
849
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000850 case 'O': /* object */
851 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000852 PyTypeObject *type;
853 PyObject **p;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000854 if (*format == '!') {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000855 type = va_arg(*p_va, PyTypeObject*);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000856 p = va_arg(*p_va, PyObject **);
857 format++;
858 if (arg->ob_type == type)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000859 *p = arg;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000860 else
861 return type->tp_name;
862
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000863 }
864 else if (*format == '?') {
865 inquiry pred = va_arg(*p_va, inquiry);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000866 p = va_arg(*p_va, PyObject **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000867 format++;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000868 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000869 *p = arg;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000870 else
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000871 return "(unspecified)";
Guido van Rossumfccfe891998-05-15 22:04:07 +0000872
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000873 }
874 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000875 typedef int (*converter)(PyObject *, void *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000876 converter convert = va_arg(*p_va, converter);
877 void *addr = va_arg(*p_va, void *);
878 format++;
879 if (! (*convert)(arg, addr))
Guido van Rossum64fc6491995-01-21 14:09:37 +0000880 return "(unspecified)";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000881 }
882 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000883 p = va_arg(*p_va, PyObject **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000884 *p = arg;
885 }
886 break;
887 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000888
889
890 case 'w': /* memory buffer, read-write access */
891 {
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 || pb->bf_getwritebuffer == NULL ||
897 pb->bf_getsegcount == NULL )
898 return "read-write buffer";
899 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
900 return "single-segment read-write buffer";
901 if ( (count = pb->bf_getwritebuffer(arg, 0, p)) < 0 )
902 return "(unspecified)";
903 if (*format == '#') {
904 int *q = va_arg(*p_va, int *);
905
906 *q = count;
907 format++;
908 }
909 break;
910 }
911
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000912 case 't': /* 8-bit character buffer, read-only access */
913 {
914 const char **p = va_arg(*p_va, const char **);
915 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
916 int count;
917
918 if ( *format++ != '#' )
919 return "invalid use of 't' format character";
920 if ( !PyType_HasFeature(
921 arg->ob_type,
922 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
923 pb == NULL ||
924 pb->bf_getcharbuffer == NULL ||
925 pb->bf_getsegcount == NULL )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000926 return "string or read-only character buffer";
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000927 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000928 return "string or single-segment read-only buffer";
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000929 if ( (count = pb->bf_getcharbuffer(arg, 0, p)) < 0 )
930 return "(unspecified)";
931
932 *va_arg(*p_va, int *) = count;
933
934 break;
935 }
936
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000937
938 default:
939 return "impossible<bad format char>";
940
941 }
942
943 *p_format = format;
944 return NULL;
945}
Guido van Rossumaa354651996-08-19 19:32:04 +0000946
947
948/* Support for keyword arguments donated by
949 Geoff Philbrick <philbric@delphi.hks.com> */
950
Guido van Rossum79f25d91997-04-29 20:08:16 +0000951int PyArg_ParseTupleAndKeywords(PyObject *args,
952 PyObject *keywords,
Guido van Rossumaa354651996-08-19 19:32:04 +0000953 char *format,
954 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +0000955{
956 int retval;
957 va_list va;
Guido van Rossumaa354651996-08-19 19:32:04 +0000958
959 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +0000960 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
961 va_end(va);
962 return retval;
963}
964
965
966static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000967vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
968 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +0000969{
970 char msgbuf[256];
971 int levels[32];
972 char *fname = NULL;
973 char *message = NULL;
974 int min = -1;
975 int max = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +0000976 char *formatsave = format;
977 int i, len, tplen, kwlen;
978 char *msg, *ks, **p;
979 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000980 PyObject *key, *value;
Guido van Rossumaa354651996-08-19 19:32:04 +0000981
982 /* nested tuples cannot be parsed when using keyword arguments */
983
984 for (;;) {
985 int c = *format++;
986 if (c == '(') {
987 PyErr_SetString(PyExc_SystemError,
988 "tuple found in format when using keyword arguments");
989 return 0;
990 }
991 else if (c == '\0')
992 break;
993 else if (c == ':') {
994 fname = format;
995 break;
996 }
997 else if (c == ';') {
998 message = format;
999 break;
1000 }
Marc-André Lemburgbbcf2a72000-09-08 11:49:37 +00001001 else if (c == 'e')
1002 ; /* Pass */
Guido van Rossumaa354651996-08-19 19:32:04 +00001003 else if (isalpha(c))
1004 max++;
1005 else if (c == '|')
1006 min = max;
1007 }
1008
1009 if (min < 0)
1010 min = max;
1011
1012 format = formatsave;
1013
1014 if (!PyTuple_Check(args)) {
1015 PyErr_SetString(PyExc_SystemError,
1016 "new style getargs format but argument is not a tuple");
1017 return 0;
1018 }
1019
1020 tplen = PyTuple_Size(args);
1021
1022 /* do a cursory check of the keywords just to see how many we got */
1023
1024 if (keywords) {
1025 if (!PyDict_Check(keywords)) {
Jeremy Hyltona0ac40c2001-01-25 20:13:10 +00001026 if (keywords == NULL)
1027 PyErr_SetString(PyExc_SystemError,
1028 "NULL received when keyword dictionary expected");
1029 else
1030 PyErr_Format(PyExc_SystemError,
1031 "%s received when keyword dictionary expected",
1032 keywords->ob_type->tp_name);
Guido van Rossumaa354651996-08-19 19:32:04 +00001033 return 0;
1034 }
1035 kwlen = PyDict_Size(keywords);
1036 }
1037 else {
1038 kwlen = 0;
1039 }
1040
1041 /* make sure there are no duplicate values for an argument;
1042 its not clear when to use the term "keyword argument vs.
1043 keyword parameter in messages */
1044
1045 if (keywords) {
1046 for (i = 0; i < tplen; i++) {
1047 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1048 sprintf(msgbuf,
1049 "keyword parameter %s redefined",
1050 kwlist[i]);
1051 PyErr_SetString(PyExc_TypeError, msgbuf);
1052 return 0;
1053 }
1054 }
1055 }
1056 PyErr_Clear(); /* I'm not which Py functions set the error string */
1057
1058 /* required arguments missing from args can be supplied by keyword
1059 arguments */
1060
1061 len = tplen;
1062 if (keywords && tplen < min) {
1063 for (i = tplen; i < min; i++) {
1064 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1065 len++;
1066 }
1067 }
1068 }
1069 PyErr_Clear();
1070
1071 /* make sure we got an acceptable number of arguments; the message
1072 is a little confusing with keywords since keyword arguments
1073 which are supplied, but don't match the required arguments
1074 are not included in the "%d given" part of the message */
1075
1076 if (len < min || max < len) {
1077 if (message == NULL) {
1078 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001079 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001080 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001081 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001082 min==max ? "exactly"
1083 : len < min ? "at least" : "at most",
1084 len < min ? min : max,
1085 (len < min ? min : max) == 1 ? "" : "s",
1086 len);
1087 message = msgbuf;
1088 }
1089 PyErr_SetString(PyExc_TypeError, message);
1090 return 0;
1091 }
1092
1093 for (i = 0; i < tplen; i++) {
1094 if (*format == '|')
1095 format++;
1096 msg = convertitem(PyTuple_GetItem(args, i), &format, p_va,
1097 levels, msgbuf);
1098 if (msg) {
1099 seterror(i+1, msg, levels, fname, message);
1100 return 0;
1101 }
1102 }
1103
1104 /* handle no keyword parameters in call */
1105
1106 if (!keywords) return 1;
1107
1108 /* make sure the number of keywords in the keyword list matches the
1109 number of items in the format string */
1110
1111 nkwds = 0;
1112 p = kwlist;
1113 for (;;) {
1114 if (!*(p++)) break;
1115 nkwds++;
1116 }
1117
1118 if (nkwds != max) {
1119 PyErr_SetString(PyExc_SystemError,
1120 "number of items in format string and keyword list do not match");
1121 return 0;
1122 }
1123
1124 /* convert the keyword arguments; this uses the format
1125 string where it was left after processing args */
1126
1127 converted = 0;
1128 for (i = tplen; i < nkwds; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001129 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001130 if (*format == '|')
1131 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001132 item = PyMapping_GetItemString(keywords, kwlist[i]);
1133 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001134 msg = convertitem(item, &format, p_va, levels, msgbuf);
1135 if (msg) {
1136 seterror(i+1, msg, levels, fname, message);
1137 return 0;
1138 }
1139 converted++;
Barry Warsaw07050282000-12-11 20:01:55 +00001140 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001141 }
1142 else {
1143 PyErr_Clear();
1144 msg = skipitem(&format, p_va);
1145 if (msg) {
1146 seterror(i+1, msg, levels, fname, message);
1147 return 0;
1148 }
1149 }
1150 }
1151
1152 /* make sure there are no extraneous keyword arguments */
1153
1154 pos = 0;
1155 if (converted < kwlen) {
1156 while (PyDict_Next(keywords, &pos, &key, &value)) {
1157 match = 0;
1158 ks = PyString_AsString(key);
1159 for (i = 0; i < nkwds; i++) {
1160 if (!strcmp(ks, kwlist[i])) {
1161 match = 1;
1162 break;
1163 }
1164 }
1165 if (!match) {
1166 sprintf(msgbuf,
Guido van Rossum80dc16b2000-05-08 14:02:41 +00001167 "%s is an invalid keyword argument for this function",
Guido van Rossumaa354651996-08-19 19:32:04 +00001168 ks);
1169 PyErr_SetString(PyExc_TypeError, msgbuf);
1170 return 0;
1171 }
1172 }
1173 }
1174
1175 return 1;
1176}
1177
1178
1179static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001180skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001181{
1182 char *format = *p_format;
1183 char c = *format++;
1184
1185 switch (c) {
1186
1187 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001188 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001189 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001190 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001191 break;
1192 }
1193
1194 case 'h': /* short int */
1195 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001196 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001197 break;
1198 }
1199
Jack Jansencc22fbe2000-08-05 21:29:58 +00001200 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001201 {
1202 (void) va_arg(*p_va, unsigned short *);
1203 break;
1204 }
1205
Guido van Rossumaa354651996-08-19 19:32:04 +00001206 case 'i': /* int */
1207 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001208 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001209 break;
1210 }
1211
1212 case 'l': /* long int */
1213 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001214 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001215 break;
1216 }
1217
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001218#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001219 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001220 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001221 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001222 break;
1223 }
1224#endif
1225
Guido van Rossumaa354651996-08-19 19:32:04 +00001226 case 'f': /* float */
1227 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001228 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001229 break;
1230 }
1231
1232 case 'd': /* double */
1233 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001234 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001235 break;
1236 }
1237
1238#ifndef WITHOUT_COMPLEX
1239 case 'D': /* complex double */
1240 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001241 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001242 break;
1243 }
1244#endif /* WITHOUT_COMPLEX */
1245
1246 case 'c': /* char */
1247 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001248 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001249 break;
1250 }
1251
1252 case 's': /* string */
1253 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001254 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001255 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001256 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001257 format++;
1258 }
1259 break;
1260 }
1261
1262 case 'z': /* string */
1263 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001264 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001265 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001266 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001267 format++;
1268 }
1269 break;
1270 }
1271
1272 case 'S': /* string object */
1273 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001274 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001275 break;
1276 }
1277
1278 case 'O': /* object */
1279 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001280 if (*format == '!') {
1281 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001282 (void) va_arg(*p_va, PyTypeObject*);
1283 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001284 }
1285#if 0
1286/* I don't know what this is for */
1287 else if (*format == '?') {
1288 inquiry pred = va_arg(*p_va, inquiry);
1289 format++;
1290 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001291 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001292 }
1293 }
1294#endif
1295 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001296 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001297 (void) va_arg(*p_va, converter);
1298 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001299 format++;
1300 }
1301 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001302 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001303 }
1304 break;
1305 }
1306
1307 default:
1308 return "impossible<bad format char>";
1309
1310 }
1311
1312 *p_format = format;
1313 return NULL;
1314}