blob: 00f298a62794a824fa879013b7eff11fa30a5a10 [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
Tim Peters5c4d5bf2001-02-12 22:13:26 +000087 assert(compat || (args != (PyObject*)NULL));
88
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000089 for (;;) {
90 int c = *format++;
91 if (c == '(' /* ')' */) {
92 if (level == 0)
93 max++;
94 level++;
95 }
96 else if (/* '(' */ c == ')') {
97 if (level == 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +000098 Py_FatalError(/* '(' */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000099 "excess ')' in getargs format");
100 else
101 level--;
102 }
103 else if (c == '\0')
104 break;
105 else if (c == ':') {
106 fname = format;
107 break;
108 }
109 else if (c == ';') {
110 message = format;
111 break;
112 }
113 else if (level != 0)
114 ; /* Pass */
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000115 else if (c == 'e')
116 ; /* Pass */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000117 else if (isalpha(c))
118 max++;
119 else if (c == '|')
120 min = max;
121 }
122
123 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000124 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000125
126 if (min < 0)
127 min = max;
128
129 format = formatsave;
130
131 if (compat) {
132 if (max == 0) {
133 if (args == NULL)
134 return 1;
Ka-Ping Yee20579702001-01-15 22:14:16 +0000135 sprintf(msgbuf, "%s%s takes no arguments",
136 fname==NULL ? "function" : fname,
137 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000138 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000139 return 0;
140 }
141 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000142 if (args == NULL) {
143 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000144 "%s%s takes at least one argument",
145 fname==NULL ? "function" : fname,
146 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000147 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000148 return 0;
149 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000150 msg = convertitem(args, &format, p_va, levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000151 if (msg == NULL)
152 return 1;
153 seterror(levels[0], msg, levels+1, fname, message);
154 return 0;
155 }
156 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000157 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000158 "old style getargs format uses new features");
159 return 0;
160 }
161 }
162
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 if (!PyTuple_Check(args)) {
164 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000165 "new style getargs format but argument is not a tuple");
166 return 0;
167 }
168
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 len = PyTuple_Size(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000170
171 if (len < min || max < len) {
172 if (message == NULL) {
173 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000174 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000175 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000176 fname==NULL ? "" : "()",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000177 min==max ? "exactly"
178 : len < min ? "at least" : "at most",
179 len < min ? min : max,
180 (len < min ? min : max) == 1 ? "" : "s",
181 len);
182 message = msgbuf;
183 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000185 return 0;
186 }
187
188 for (i = 0; i < len; i++) {
189 if (*format == '|')
190 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 msg = convertitem(PyTuple_GetItem(args, i), &format, p_va,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000192 levels, msgbuf);
193 if (msg) {
194 seterror(i+1, msg, levels, fname, message);
195 return 0;
196 }
197 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000198
Guido van Rossum730806d1998-04-10 22:27:42 +0000199 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000200 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000201 *format != '|' && *format != ':' && *format != ';') {
202 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000203 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000204 return 0;
205 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000206
207 return 1;
208}
209
210
211
212static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000213seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000214{
215 char buf[256];
216 int i;
217 char *p = buf;
218
Guido van Rossum79f25d91997-04-29 20:08:16 +0000219 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000220 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000221 else if (message == NULL) {
222 if (fname != NULL) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000223 sprintf(p, "%s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000224 p += strlen(p);
225 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000226 if (iarg != 0) {
227 sprintf(p, "argument %d", iarg);
228 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000229 p += strlen(p);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000230 while (levels[i] > 0) {
231 sprintf(p, ", item %d", levels[i]-1);
232 p += strlen(p);
233 i++;
234 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000235 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000236 else {
237 sprintf(p, "argument");
238 p += strlen(p);
239 }
240 sprintf(p, " %s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000241 message = buf;
242 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000244}
245
246
247/* Convert a tuple argument.
248 On entry, *p_format points to the character _after_ the opening '('.
249 On successful exit, *p_format points to the closing ')'.
250 If successful:
251 *p_format and *p_va are updated,
252 *levels and *msgbuf are untouched,
253 and NULL is returned.
254 If the argument is invalid:
255 *p_format is unchanged,
256 *p_va is undefined,
257 *levels is a 0-terminated list of item numbers,
258 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000259 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000260 <typename1> is the name of the expected type, and
261 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000262 and msgbuf is returned.
263*/
264
265static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000266converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
267 char *msgbuf, int toplevel)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000268{
269 int level = 0;
270 int n = 0;
271 char *format = *p_format;
272 int i;
273
274 for (;;) {
275 int c = *format++;
276 if (c == '(') {
277 if (level == 0)
278 n++;
279 level++;
280 }
281 else if (c == ')') {
282 if (level == 0)
283 break;
284 level--;
285 }
286 else if (c == ':' || c == ';' || c == '\0')
287 break;
288 else if (level == 0 && isalpha(c))
289 n++;
290 }
291
Ka-Ping Yee20579702001-01-15 22:14:16 +0000292 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000293 levels[0] = 0;
294 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000295 toplevel ? "expected %d arguments, not %s" :
296 "must be %d-item sequence, not %s",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000297 n, arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000298 return msgbuf;
299 }
300
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000301 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000302 levels[0] = 0;
303 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000304 toplevel ? "expected %d arguments, not %d" :
305 "must be sequence of length %d, not %d",
306 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000307 return msgbuf;
308 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000309
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000310 format = *p_format;
311 for (i = 0; i < n; i++) {
312 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000313 PyObject *item;
314 item = PySequence_GetItem(arg, i);
315 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
316 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
317 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000318 if (msg != NULL) {
319 levels[0] = i+1;
320 return msg;
321 }
322 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000323
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000324 *p_format = format;
325 return NULL;
326}
327
328
329/* Convert a single item. */
330
331static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
333 char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000334{
335 char *msg;
336 char *format = *p_format;
337
338 if (*format == '(' /* ')' */) {
339 format++;
340 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
341 if (msg == NULL)
342 format++;
343 }
344 else {
345 msg = convertsimple(arg, &format, p_va, msgbuf);
346 if (msg != NULL)
347 levels[0] = 0;
348 }
349 if (msg == NULL)
350 *p_format = format;
351 return msg;
352}
353
354
355/* Convert a non-tuple argument. Adds to convertsimple1 functionality
Ka-Ping Yee20579702001-01-15 22:14:16 +0000356 by formatting messages as "must be <desired type>, not <actual type>". */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000357
358static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000359convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000360{
361 char *msg = convertsimple1(arg, p_format, p_va);
362 if (msg != NULL) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000363 sprintf(msgbuf, "must be %.50s, not %.50s", msg,
Guido van Rossum79f25d91997-04-29 20:08:16 +0000364 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000365 msg = msgbuf;
366 }
367 return msg;
368}
369
370
Guido van Rossum700c6ff2000-04-27 20:13:18 +0000371/* Internal API needed by convertsimple1(): */
372extern
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000373PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
Guido van Rossum700c6ff2000-04-27 20:13:18 +0000374 const char *errors);
375
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000376/* Convert a non-tuple argument. Return NULL if conversion went OK,
377 or a string representing the expected type if the conversion failed.
378 When failing, an exception may or may not have been raised.
379 Don't call if a tuple is expected. */
380
381static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000382convertsimple1(PyObject *arg, char **p_format, va_list *p_va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000383{
384 char *format = *p_format;
385 char c = *format++;
386
387 switch (c) {
388
Fred Drake230cae72000-05-09 21:50:00 +0000389 case 'b': /* unsigned byte -- very short int */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000390 {
391 char *p = va_arg(*p_va, char *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000392 long ival = PyInt_AsLong(arg);
393 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000394 return "integer<b>";
Fred Drake230cae72000-05-09 21:50:00 +0000395 else if (ival < 0) {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000396 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000397 "unsigned byte integer is less than minimum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000398 return "integer<b>";
399 }
Fred Drake230cae72000-05-09 21:50:00 +0000400 else if (ival > UCHAR_MAX) {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000401 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000402 "unsigned byte integer is greater than maximum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000403 return "integer<b>";
404 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000405 else
Fred Drake230cae72000-05-09 21:50:00 +0000406 *p = (unsigned char) ival;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000407 break;
408 }
409
Jack Jansencc22fbe2000-08-05 21:29:58 +0000410 case 'B': /* byte sized bitfield - both signed and unsigned values allowed */
411 {
412 char *p = va_arg(*p_va, char *);
413 long ival = PyInt_AsLong(arg);
414 if (ival == -1 && PyErr_Occurred())
415 return "integer<b>";
416 else if (ival < SCHAR_MIN) {
417 PyErr_SetString(PyExc_OverflowError,
418 "byte-sized integer bitfield is less than minimum");
419 return "integer<B>";
420 }
Jack Jansena454ebd2000-09-15 12:52:19 +0000421 else if (ival > (int)UCHAR_MAX) {
Jack Jansencc22fbe2000-08-05 21:29:58 +0000422 PyErr_SetString(PyExc_OverflowError,
423 "byte-sized integer bitfield is greater than maximum");
424 return "integer<B>";
425 }
426 else
427 *p = (unsigned char) ival;
428 break;
429 }
430
Fred Drake230cae72000-05-09 21:50:00 +0000431 case 'h': /* signed short int */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000432 {
433 short *p = va_arg(*p_va, short *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000434 long ival = PyInt_AsLong(arg);
435 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000436 return "integer<h>";
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000437 else if (ival < SHRT_MIN) {
438 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000439 "signed short integer is less than minimum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000440 return "integer<h>";
441 }
442 else if (ival > SHRT_MAX) {
443 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000444 "signed short integer is greater than maximum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000445 return "integer<h>";
446 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000447 else
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000448 *p = (short) ival;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000449 break;
450 }
451
Jack Jansencc22fbe2000-08-05 21:29:58 +0000452 case 'H': /* short int sized bitfield, both signed and unsigned allowed */
Jack Jansend50338f2000-07-06 12:22:00 +0000453 {
454 unsigned short *p = va_arg(*p_va, unsigned short *);
455 long ival = PyInt_AsLong(arg);
456 if (ival == -1 && PyErr_Occurred())
457 return "integer<H>";
Jack Jansencc22fbe2000-08-05 21:29:58 +0000458 else if (ival < SHRT_MIN) {
Jack Jansend50338f2000-07-06 12:22:00 +0000459 PyErr_SetString(PyExc_OverflowError,
Jack Jansencc22fbe2000-08-05 21:29:58 +0000460 "short integer bitfield is less than minimum");
Jack Jansend50338f2000-07-06 12:22:00 +0000461 return "integer<H>";
462 }
463 else if (ival > USHRT_MAX) {
464 PyErr_SetString(PyExc_OverflowError,
Jack Jansencc22fbe2000-08-05 21:29:58 +0000465 "short integer bitfield is greater than maximum");
Jack Jansend50338f2000-07-06 12:22:00 +0000466 return "integer<H>";
467 }
468 else
469 *p = (unsigned short) ival;
470 break;
471 }
472
Fred Drake230cae72000-05-09 21:50:00 +0000473 case 'i': /* signed int */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000474 {
475 int *p = va_arg(*p_va, int *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000476 long ival = PyInt_AsLong(arg);
477 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000478 return "integer<i>";
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000479 else if (ival > INT_MAX) {
480 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum5e08cb82000-06-28 23:53:56 +0000481 "signed integer is greater than maximum");
482 return "integer<i>";
483 }
484 else if (ival < INT_MIN) {
485 PyErr_SetString(PyExc_OverflowError,
486 "signed integer is less than minimum");
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000487 return "integer<i>";
488 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000489 else
490 *p = ival;
491 break;
492 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000493 case 'l': /* long int */
494 {
495 long *p = va_arg(*p_va, long *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000496 long ival = PyInt_AsLong(arg);
497 if (ival == -1 && PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000498 return "integer<l>";
499 else
500 *p = ival;
501 break;
502 }
503
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000504#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +0000505 case 'L': /* LONG_LONG */
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000506 {
Guido van Rossum3293b071998-08-25 16:07:15 +0000507 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
508 LONG_LONG ival = PyLong_AsLongLong( arg );
509 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000510 return "long<L>";
511 } else {
512 *p = ival;
513 }
514 break;
515 }
516#endif
517
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000518 case 'f': /* float */
519 {
520 float *p = va_arg(*p_va, float *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000521 double dval = PyFloat_AsDouble(arg);
522 if (PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000523 return "float<f>";
524 else
Guido van Rossum6bf62da1997-04-11 20:37:35 +0000525 *p = (float) dval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000526 break;
527 }
528
529 case 'd': /* double */
530 {
531 double *p = va_arg(*p_va, double *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000532 double dval = PyFloat_AsDouble(arg);
533 if (PyErr_Occurred())
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000534 return "float<d>";
535 else
536 *p = dval;
537 break;
538 }
539
Guido van Rossum530956d1996-07-21 02:27:43 +0000540#ifndef WITHOUT_COMPLEX
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000541 case 'D': /* complex double */
542 {
Guido van Rossum530956d1996-07-21 02:27:43 +0000543 Py_complex *p = va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +0000544 Py_complex cval;
545 cval = PyComplex_AsCComplex(arg);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000546 if (PyErr_Occurred())
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000547 return "complex<D>";
548 else
549 *p = cval;
550 break;
551 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000552#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000553
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000554 case 'c': /* char */
555 {
556 char *p = va_arg(*p_va, char *);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000557 if (PyString_Check(arg) && PyString_Size(arg) == 1)
558 *p = PyString_AsString(arg)[0];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000559 else
560 return "char";
561 break;
562 }
563
564 case 's': /* string */
565 {
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000566 if (*format == '#') {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000567 void **p = (void **)va_arg(*p_va, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000568 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000569
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000570 if (PyString_Check(arg)) {
571 *p = PyString_AS_STRING(arg);
572 *q = PyString_GET_SIZE(arg);
573 }
574 else if (PyUnicode_Check(arg)) {
575 arg = _PyUnicode_AsDefaultEncodedString(
576 arg, NULL);
577 if (arg == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000578 return "(unicode conversion error)";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000579 *p = PyString_AS_STRING(arg);
580 *q = PyString_GET_SIZE(arg);
581 }
582 else { /* any buffer-like object */
583 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
584 int count;
585 if ( pb == NULL ||
586 pb->bf_getreadbuffer == NULL ||
587 pb->bf_getsegcount == NULL )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000588 return "string or read-only buffer";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000589 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000590 return "string or single-segment read-only buffer";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000591 if ( (count =
592 (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
593 return "(unspecified)";
594 *q = count;
595 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000596 format++;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000597 } else {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000598 char **p = va_arg(*p_va, char **);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000599
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000600 if (PyString_Check(arg))
Guido van Rossume826ef02000-03-10 23:02:17 +0000601 *p = PyString_AS_STRING(arg);
602 else if (PyUnicode_Check(arg)) {
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000603 arg = _PyUnicode_AsDefaultEncodedString(
604 arg, NULL);
Guido van Rossume826ef02000-03-10 23:02:17 +0000605 if (arg == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000606 return "(unicode conversion error)";
Guido van Rossume826ef02000-03-10 23:02:17 +0000607 *p = PyString_AS_STRING(arg);
608 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000609 else
610 return "string";
611 if ((int)strlen(*p) != PyString_Size(arg))
612 return "string without null bytes";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000613 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000614 break;
615 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000616
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000617 case 'z': /* string, may be NULL (None) */
618 {
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000619 if (*format == '#') { /* any buffer-like object */
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000620 void **p = (void **)va_arg(*p_va, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000621 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000622
623 if (arg == Py_None) {
624 *p = 0;
625 *q = 0;
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000626 }
627 else if (PyString_Check(arg)) {
628 *p = PyString_AS_STRING(arg);
629 *q = PyString_GET_SIZE(arg);
630 }
631 else if (PyUnicode_Check(arg)) {
632 arg = _PyUnicode_AsDefaultEncodedString(
633 arg, NULL);
634 if (arg == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000635 return "(unicode conversion error)";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000636 *p = PyString_AS_STRING(arg);
637 *q = PyString_GET_SIZE(arg);
638 }
639 else { /* any buffer-like object */
640 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
641 int count;
642 if ( pb == NULL ||
643 pb->bf_getreadbuffer == NULL ||
644 pb->bf_getsegcount == NULL )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000645 return "string or read-only buffer";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000646 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000647 return "string or single-segment read-only buffer";
Marc-André Lemburg0afff382000-09-21 21:08:30 +0000648 if ( (count =
649 (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
650 return "(unspecified)";
651 *q = count;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000652 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000653 format++;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000654 } else {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000655 char **p = va_arg(*p_va, char **);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000656
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000657 if (arg == Py_None)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000658 *p = 0;
659 else if (PyString_Check(arg))
660 *p = PyString_AsString(arg);
Guido van Rossume826ef02000-03-10 23:02:17 +0000661 else if (PyUnicode_Check(arg)) {
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000662 arg = _PyUnicode_AsDefaultEncodedString(
663 arg, NULL);
Guido van Rossume826ef02000-03-10 23:02:17 +0000664 if (arg == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000665 return "(unicode conversion error)";
Guido van Rossume826ef02000-03-10 23:02:17 +0000666 *p = PyString_AS_STRING(arg);
667 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000668 else
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000669 return "string or None";
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000670 if (*format == '#') {
671 int *q = va_arg(*p_va, int *);
672 if (arg == Py_None)
673 *q = 0;
674 else
675 *q = PyString_Size(arg);
676 format++;
677 }
678 else if (*p != NULL &&
679 (int)strlen(*p) != PyString_Size(arg))
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000680 return "string without null bytes or None";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000681 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000682 break;
683 }
684
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000685 case 'e': /* encoded string */
686 {
687 char **buffer;
688 const char *encoding;
689 PyObject *u, *s;
690 int size;
691
692 /* Get 'e' parameter: the encoding name */
693 encoding = (const char *)va_arg(*p_va, const char *);
694 if (encoding == NULL)
Marc-André Lemburgbff879c2000-08-03 18:46:08 +0000695 encoding = PyUnicode_GetDefaultEncoding();
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000696
697 /* Get 's' parameter: the output buffer to use */
698 if (*format != 's')
Guido van Rossum50fbb152000-03-28 02:00:29 +0000699 return "(unknown parser marker combination)";
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000700 buffer = (char **)va_arg(*p_va, char **);
701 format++;
702 if (buffer == NULL)
703 return "(buffer is NULL)";
704
705 /* Convert object to Unicode */
706 u = PyUnicode_FromObject(arg);
707 if (u == NULL)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000708 return "string or unicode or text buffer";
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000709
710 /* Encode object; use default error handling */
711 s = PyUnicode_AsEncodedString(u,
712 encoding,
713 NULL);
714 Py_DECREF(u);
715 if (s == NULL)
716 return "(encoding failed)";
717 if (!PyString_Check(s)) {
718 Py_DECREF(s);
719 return "(encoder failed to return a string)";
720 }
721 size = PyString_GET_SIZE(s);
722
723 /* Write output; output is guaranteed to be
724 0-terminated */
725 if (*format == '#') {
726 /* Using buffer length parameter '#':
727
728 - if *buffer is NULL, a new buffer
729 of the needed size is allocated and
730 the data copied into it; *buffer is
731 updated to point to the new buffer;
732 the caller is responsible for
Guido van Rossum24bdb042000-03-28 20:29:59 +0000733 PyMem_Free()ing it after usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000734
735 - if *buffer is not NULL, the data
736 is copied to *buffer; *buffer_len
737 has to be set to the size of the
738 buffer on input; buffer overflow is
739 signalled with an error; buffer has
740 to provide enough room for the
741 encoded string plus the trailing
742 0-byte
743
744 - in both cases, *buffer_len is
745 updated to the size of the buffer
746 /excluding/ the trailing 0-byte
747
748 */
749 int *buffer_len = va_arg(*p_va, int *);
750
751 format++;
752 if (buffer_len == NULL)
753 return "(buffer_len is NULL)";
754 if (*buffer == NULL) {
755 *buffer = PyMem_NEW(char, size + 1);
756 if (*buffer == NULL) {
757 Py_DECREF(s);
758 return "(memory error)";
759 }
760 } else {
761 if (size + 1 > *buffer_len) {
762 Py_DECREF(s);
763 return "(buffer overflow)";
764 }
765 }
766 memcpy(*buffer,
767 PyString_AS_STRING(s),
768 size + 1);
769 *buffer_len = size;
770 } else {
771 /* Using a 0-terminated buffer:
772
773 - the encoded string has to be
774 0-terminated for this variant to
775 work; if it is not, an error raised
776
777 - a new buffer of the needed size
778 is allocated and the data copied
779 into it; *buffer is updated to
780 point to the new buffer; the caller
Guido van Rossum24bdb042000-03-28 20:29:59 +0000781 is responsible for PyMem_Free()ing it
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000782 after usage
783
784 */
Guido van Rossum50fbb152000-03-28 02:00:29 +0000785 if ((int)strlen(PyString_AS_STRING(s)) != size)
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000786 return "(encoded string without NULL bytes)";
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000787 *buffer = PyMem_NEW(char, size + 1);
788 if (*buffer == NULL) {
789 Py_DECREF(s);
790 return "(memory error)";
791 }
792 memcpy(*buffer,
793 PyString_AS_STRING(s),
794 size + 1);
795 }
796 Py_DECREF(s);
797 break;
798 }
799
Fred Drake25871c02000-05-03 15:17:02 +0000800 case 'u': /* raw unicode buffer (Py_UNICODE *) */
801 {
802 if (*format == '#') { /* any buffer-like object */
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000803 void **p = (void **)va_arg(*p_va, char **);
804 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Fred Drake25871c02000-05-03 15:17:02 +0000805 int *q = va_arg(*p_va, int *);
806 int count;
807
808 if ( pb == NULL ||
809 pb->bf_getreadbuffer == NULL ||
810 pb->bf_getsegcount == NULL )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000811 return "unicode or read-only buffer";
Fred Drake25871c02000-05-03 15:17:02 +0000812 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000813 return "unicode or single-segment read-only buffer";
Fred Drake25871c02000-05-03 15:17:02 +0000814 if ( (count =
815 (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
816 return "(unspecified)";
817 /* buffer interface returns bytes, we want
818 length in characters */
819 *q = count/(sizeof(Py_UNICODE));
820 format++;
821 } else {
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000822 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Fred Drake25871c02000-05-03 15:17:02 +0000823
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000824 if (PyUnicode_Check(arg))
Fred Drake25871c02000-05-03 15:17:02 +0000825 *p = PyUnicode_AS_UNICODE(arg);
826 else
827 return "unicode";
828 }
829 break;
830 }
831
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000832 case 'S': /* string object */
833 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000834 PyObject **p = va_arg(*p_va, PyObject **);
835 if (PyString_Check(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000836 *p = arg;
837 else
838 return "string";
839 break;
840 }
841
Guido van Rossume826ef02000-03-10 23:02:17 +0000842 case 'U': /* Unicode object */
843 {
844 PyObject **p = va_arg(*p_va, PyObject **);
845 if (PyUnicode_Check(arg))
846 *p = arg;
847 else
848 return "unicode";
849 break;
850 }
851
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000852 case 'O': /* object */
853 {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000854 PyTypeObject *type;
855 PyObject **p;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000856 if (*format == '!') {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000857 type = va_arg(*p_va, PyTypeObject*);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000858 p = va_arg(*p_va, PyObject **);
859 format++;
860 if (arg->ob_type == type)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000861 *p = arg;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000862 else
863 return type->tp_name;
864
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000865 }
866 else if (*format == '?') {
867 inquiry pred = va_arg(*p_va, inquiry);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000868 p = va_arg(*p_va, PyObject **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000869 format++;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000870 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000871 *p = arg;
Guido van Rossumfccfe891998-05-15 22:04:07 +0000872 else
Guido van Rossum80dc16b2000-05-08 14:02:41 +0000873 return "(unspecified)";
Guido van Rossumfccfe891998-05-15 22:04:07 +0000874
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000875 }
876 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +0000877 typedef int (*converter)(PyObject *, void *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000878 converter convert = va_arg(*p_va, converter);
879 void *addr = va_arg(*p_va, void *);
880 format++;
881 if (! (*convert)(arg, addr))
Guido van Rossum64fc6491995-01-21 14:09:37 +0000882 return "(unspecified)";
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000883 }
884 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000885 p = va_arg(*p_va, PyObject **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000886 *p = arg;
887 }
888 break;
889 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000890
891
892 case 'w': /* memory buffer, read-write access */
893 {
894 void **p = va_arg(*p_va, void **);
895 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
896 int count;
897
898 if ( pb == NULL || pb->bf_getwritebuffer == NULL ||
899 pb->bf_getsegcount == NULL )
900 return "read-write buffer";
901 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
902 return "single-segment read-write buffer";
903 if ( (count = pb->bf_getwritebuffer(arg, 0, p)) < 0 )
904 return "(unspecified)";
905 if (*format == '#') {
906 int *q = va_arg(*p_va, int *);
907
908 *q = count;
909 format++;
910 }
911 break;
912 }
913
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000914 case 't': /* 8-bit character buffer, read-only access */
915 {
916 const char **p = va_arg(*p_va, const char **);
917 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
918 int count;
919
920 if ( *format++ != '#' )
921 return "invalid use of 't' format character";
922 if ( !PyType_HasFeature(
923 arg->ob_type,
924 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
925 pb == NULL ||
926 pb->bf_getcharbuffer == NULL ||
927 pb->bf_getsegcount == NULL )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000928 return "string or read-only character buffer";
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000929 if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
Guido van Rossum60a1e7f2000-12-01 12:59:05 +0000930 return "string or single-segment read-only buffer";
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000931 if ( (count = pb->bf_getcharbuffer(arg, 0, p)) < 0 )
932 return "(unspecified)";
933
934 *va_arg(*p_va, int *) = count;
935
936 break;
937 }
938
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000939
940 default:
941 return "impossible<bad format char>";
942
943 }
944
945 *p_format = format;
946 return NULL;
947}
Guido van Rossumaa354651996-08-19 19:32:04 +0000948
949
950/* Support for keyword arguments donated by
951 Geoff Philbrick <philbric@delphi.hks.com> */
952
Guido van Rossum79f25d91997-04-29 20:08:16 +0000953int PyArg_ParseTupleAndKeywords(PyObject *args,
954 PyObject *keywords,
Guido van Rossumaa354651996-08-19 19:32:04 +0000955 char *format,
956 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +0000957{
958 int retval;
959 va_list va;
Guido van Rossumaa354651996-08-19 19:32:04 +0000960
961 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +0000962 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
963 va_end(va);
964 return retval;
965}
966
967
968static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000969vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
970 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +0000971{
972 char msgbuf[256];
973 int levels[32];
974 char *fname = NULL;
975 char *message = NULL;
976 int min = -1;
977 int max = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +0000978 char *formatsave = format;
979 int i, len, tplen, kwlen;
980 char *msg, *ks, **p;
981 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000982 PyObject *key, *value;
Guido van Rossumaa354651996-08-19 19:32:04 +0000983
984 /* nested tuples cannot be parsed when using keyword arguments */
985
986 for (;;) {
987 int c = *format++;
988 if (c == '(') {
989 PyErr_SetString(PyExc_SystemError,
990 "tuple found in format when using keyword arguments");
991 return 0;
992 }
993 else if (c == '\0')
994 break;
995 else if (c == ':') {
996 fname = format;
997 break;
998 }
999 else if (c == ';') {
1000 message = format;
1001 break;
1002 }
Marc-André Lemburgbbcf2a72000-09-08 11:49:37 +00001003 else if (c == 'e')
1004 ; /* Pass */
Guido van Rossumaa354651996-08-19 19:32:04 +00001005 else if (isalpha(c))
1006 max++;
1007 else if (c == '|')
1008 min = max;
1009 }
1010
1011 if (min < 0)
1012 min = max;
1013
1014 format = formatsave;
1015
1016 if (!PyTuple_Check(args)) {
1017 PyErr_SetString(PyExc_SystemError,
1018 "new style getargs format but argument is not a tuple");
1019 return 0;
1020 }
1021
1022 tplen = PyTuple_Size(args);
1023
1024 /* do a cursory check of the keywords just to see how many we got */
1025
1026 if (keywords) {
1027 if (!PyDict_Check(keywords)) {
Jeremy Hyltona0ac40c2001-01-25 20:13:10 +00001028 if (keywords == NULL)
1029 PyErr_SetString(PyExc_SystemError,
1030 "NULL received when keyword dictionary expected");
1031 else
1032 PyErr_Format(PyExc_SystemError,
1033 "%s received when keyword dictionary expected",
1034 keywords->ob_type->tp_name);
Guido van Rossumaa354651996-08-19 19:32:04 +00001035 return 0;
1036 }
1037 kwlen = PyDict_Size(keywords);
1038 }
1039 else {
1040 kwlen = 0;
1041 }
1042
1043 /* make sure there are no duplicate values for an argument;
1044 its not clear when to use the term "keyword argument vs.
1045 keyword parameter in messages */
1046
1047 if (keywords) {
1048 for (i = 0; i < tplen; i++) {
1049 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1050 sprintf(msgbuf,
1051 "keyword parameter %s redefined",
1052 kwlist[i]);
1053 PyErr_SetString(PyExc_TypeError, msgbuf);
1054 return 0;
1055 }
1056 }
1057 }
1058 PyErr_Clear(); /* I'm not which Py functions set the error string */
1059
1060 /* required arguments missing from args can be supplied by keyword
1061 arguments */
1062
1063 len = tplen;
1064 if (keywords && tplen < min) {
1065 for (i = tplen; i < min; i++) {
1066 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1067 len++;
1068 }
1069 }
1070 }
1071 PyErr_Clear();
1072
1073 /* make sure we got an acceptable number of arguments; the message
1074 is a little confusing with keywords since keyword arguments
1075 which are supplied, but don't match the required arguments
1076 are not included in the "%d given" part of the message */
1077
1078 if (len < min || max < len) {
1079 if (message == NULL) {
1080 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001081 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001082 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001083 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001084 min==max ? "exactly"
1085 : len < min ? "at least" : "at most",
1086 len < min ? min : max,
1087 (len < min ? min : max) == 1 ? "" : "s",
1088 len);
1089 message = msgbuf;
1090 }
1091 PyErr_SetString(PyExc_TypeError, message);
1092 return 0;
1093 }
1094
1095 for (i = 0; i < tplen; i++) {
1096 if (*format == '|')
1097 format++;
1098 msg = convertitem(PyTuple_GetItem(args, i), &format, p_va,
1099 levels, msgbuf);
1100 if (msg) {
1101 seterror(i+1, msg, levels, fname, message);
1102 return 0;
1103 }
1104 }
1105
1106 /* handle no keyword parameters in call */
1107
1108 if (!keywords) return 1;
1109
1110 /* make sure the number of keywords in the keyword list matches the
1111 number of items in the format string */
1112
1113 nkwds = 0;
1114 p = kwlist;
1115 for (;;) {
1116 if (!*(p++)) break;
1117 nkwds++;
1118 }
1119
1120 if (nkwds != max) {
1121 PyErr_SetString(PyExc_SystemError,
1122 "number of items in format string and keyword list do not match");
1123 return 0;
1124 }
1125
1126 /* convert the keyword arguments; this uses the format
1127 string where it was left after processing args */
1128
1129 converted = 0;
1130 for (i = tplen; i < nkwds; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001131 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001132 if (*format == '|')
1133 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001134 item = PyMapping_GetItemString(keywords, kwlist[i]);
1135 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001136 msg = convertitem(item, &format, p_va, levels, msgbuf);
1137 if (msg) {
1138 seterror(i+1, msg, levels, fname, message);
1139 return 0;
1140 }
1141 converted++;
Barry Warsaw07050282000-12-11 20:01:55 +00001142 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001143 }
1144 else {
1145 PyErr_Clear();
1146 msg = skipitem(&format, p_va);
1147 if (msg) {
1148 seterror(i+1, msg, levels, fname, message);
1149 return 0;
1150 }
1151 }
1152 }
1153
1154 /* make sure there are no extraneous keyword arguments */
1155
1156 pos = 0;
1157 if (converted < kwlen) {
1158 while (PyDict_Next(keywords, &pos, &key, &value)) {
1159 match = 0;
1160 ks = PyString_AsString(key);
1161 for (i = 0; i < nkwds; i++) {
1162 if (!strcmp(ks, kwlist[i])) {
1163 match = 1;
1164 break;
1165 }
1166 }
1167 if (!match) {
1168 sprintf(msgbuf,
Guido van Rossum80dc16b2000-05-08 14:02:41 +00001169 "%s is an invalid keyword argument for this function",
Guido van Rossumaa354651996-08-19 19:32:04 +00001170 ks);
1171 PyErr_SetString(PyExc_TypeError, msgbuf);
1172 return 0;
1173 }
1174 }
1175 }
1176
1177 return 1;
1178}
1179
1180
1181static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001182skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001183{
1184 char *format = *p_format;
1185 char c = *format++;
1186
1187 switch (c) {
1188
1189 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001190 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001191 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001192 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001193 break;
1194 }
1195
1196 case 'h': /* short int */
1197 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001198 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001199 break;
1200 }
1201
Jack Jansencc22fbe2000-08-05 21:29:58 +00001202 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001203 {
1204 (void) va_arg(*p_va, unsigned short *);
1205 break;
1206 }
1207
Guido van Rossumaa354651996-08-19 19:32:04 +00001208 case 'i': /* int */
1209 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001210 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001211 break;
1212 }
1213
1214 case 'l': /* long int */
1215 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001216 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001217 break;
1218 }
1219
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001220#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001221 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001222 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001223 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001224 break;
1225 }
1226#endif
1227
Guido van Rossumaa354651996-08-19 19:32:04 +00001228 case 'f': /* float */
1229 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001230 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001231 break;
1232 }
1233
1234 case 'd': /* double */
1235 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001236 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001237 break;
1238 }
1239
1240#ifndef WITHOUT_COMPLEX
1241 case 'D': /* complex double */
1242 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001243 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001244 break;
1245 }
1246#endif /* WITHOUT_COMPLEX */
1247
1248 case 'c': /* char */
1249 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001250 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001251 break;
1252 }
1253
1254 case 's': /* string */
1255 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001256 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001257 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001258 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001259 format++;
1260 }
1261 break;
1262 }
1263
1264 case 'z': /* string */
1265 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001266 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001267 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001268 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001269 format++;
1270 }
1271 break;
1272 }
1273
1274 case 'S': /* string object */
1275 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001276 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001277 break;
1278 }
1279
1280 case 'O': /* object */
1281 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001282 if (*format == '!') {
1283 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001284 (void) va_arg(*p_va, PyTypeObject*);
1285 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001286 }
1287#if 0
1288/* I don't know what this is for */
1289 else if (*format == '?') {
1290 inquiry pred = va_arg(*p_va, inquiry);
1291 format++;
1292 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001293 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001294 }
1295 }
1296#endif
1297 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001298 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001299 (void) va_arg(*p_va, converter);
1300 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001301 format++;
1302 }
1303 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001304 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001305 }
1306 break;
1307 }
1308
1309 default:
1310 return "impossible<bad format char>";
1311
1312 }
1313
1314 *p_format = format;
1315 return NULL;
1316}