blob: 8c4b039c2a4494b9c3b1dad35232667b38d58151 [file] [log] [blame]
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001
2/* New getargs implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00005
Guido van Rossumc1d50531996-08-21 23:38:24 +00006#include <ctype.h>
7
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00008
Tim Petersdbd9ba62000-07-09 03:09:57 +00009int PyArg_Parse(PyObject *, char *, ...);
10int PyArg_ParseTuple(PyObject *, char *, ...);
11int PyArg_VaParse(PyObject *, char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000012
Tim Petersdbd9ba62000-07-09 03:09:57 +000013int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
14 char *, char **, ...);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000015
16/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000017static int vgetargs1(PyObject *, char *, va_list *, int);
18static void seterror(int, char *, int *, char *, char *);
19static char *convertitem(PyObject *, char **, va_list *, int *, char *);
20static char *converttuple(PyObject *, char **, va_list *,
21 int *, char *, int);
22static char *convertsimple(PyObject *, char **, va_list *, char *);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +000023static int convertbuffer(PyObject *, void **p, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000024
Tim Petersdbd9ba62000-07-09 03:09:57 +000025static int vgetargskeywords(PyObject *, PyObject *,
26 char *, char **, va_list *);
27static char *skipitem(char **, va_list *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000028
Fred Drake563dfc22001-10-23 14:41:08 +000029int
30PyArg_Parse(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000031{
32 int retval;
33 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000034
35 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000036 retval = vgetargs1(args, format, &va, 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000037 va_end(va);
38 return retval;
39}
40
41
Fred Drake563dfc22001-10-23 14:41:08 +000042int
43PyArg_ParseTuple(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000044{
45 int retval;
46 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000047
48 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000050 va_end(va);
51 return retval;
52}
53
54
55int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000056PyArg_VaParse(PyObject *args, char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000057{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000058 va_list lva;
59
60#ifdef VA_LIST_IS_ARRAY
61 memcpy(lva, va, sizeof(va_list));
62#else
63 lva = va;
64#endif
65
66 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000067}
68
69
70static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000071vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000072{
73 char msgbuf[256];
74 int levels[32];
75 char *fname = NULL;
76 char *message = NULL;
77 int min = -1;
78 int max = 0;
79 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000080 int endfmt = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000081 char *formatsave = format;
82 int i, len;
83 char *msg;
84
Tim Peters5c4d5bf2001-02-12 22:13:26 +000085 assert(compat || (args != (PyObject*)NULL));
86
Jeremy Hylton25916bd2001-05-29 17:46:19 +000087 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000088 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000089 switch (c) {
90 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000091 if (level == 0)
92 max++;
93 level++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000094 break;
95 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000096 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +000097 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000098 else
99 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000100 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000101 case '\0':
102 endfmt = 1;
103 break;
104 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000105 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000106 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000107 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000108 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000109 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000110 endfmt = 1;
111 break;
112 default:
113 if (level == 0) {
114 if (c == 'O')
115 max++;
116 else if (isalpha(c)) {
117 if (c != 'e') /* skip encoded */
118 max++;
119 } else if (c == '|')
120 min = max;
121 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000122 break;
123 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000124 }
125
126 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000127 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000128
129 if (min < 0)
130 min = max;
131
132 format = formatsave;
133
134 if (compat) {
135 if (max == 0) {
136 if (args == NULL)
137 return 1;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000138 PyOS_snprintf(msgbuf, sizeof(msgbuf),
139 "%.200s%s takes no arguments",
140 fname==NULL ? "function" : fname,
141 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000142 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000143 return 0;
144 }
145 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000146 if (args == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000147 PyOS_snprintf(msgbuf, sizeof(msgbuf),
148 "%.200s%s takes at least one argument",
149 fname==NULL ? "function" : fname,
150 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000151 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000152 return 0;
153 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000154 msg = convertitem(args, &format, p_va, levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000155 if (msg == NULL)
156 return 1;
157 seterror(levels[0], msg, levels+1, fname, message);
158 return 0;
159 }
160 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000161 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000162 "old style getargs format uses new features");
163 return 0;
164 }
165 }
166
Guido van Rossum79f25d91997-04-29 20:08:16 +0000167 if (!PyTuple_Check(args)) {
168 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000169 "new style getargs format but argument is not a tuple");
170 return 0;
171 }
172
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000173 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000174
175 if (len < min || max < len) {
176 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000177 PyOS_snprintf(msgbuf, sizeof(msgbuf),
178 "%.150s%s takes %s %d argument%s "
179 "(%d given)",
180 fname==NULL ? "function" : fname,
181 fname==NULL ? "" : "()",
182 min==max ? "exactly"
183 : len < min ? "at least" : "at most",
184 len < min ? min : max,
185 (len < min ? min : max) == 1 ? "" : "s",
186 len);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000187 message = msgbuf;
188 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000189 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000190 return 0;
191 }
192
193 for (i = 0; i < len; i++) {
194 if (*format == '|')
195 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000196 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
197 levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000198 if (msg) {
199 seterror(i+1, msg, levels, fname, message);
200 return 0;
201 }
202 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000203
Guido van Rossum730806d1998-04-10 22:27:42 +0000204 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000205 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000206 *format != '|' && *format != ':' && *format != ';') {
207 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000208 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000209 return 0;
210 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000211
212 return 1;
213}
214
215
216
217static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000218seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000219{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000220 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000221 int i;
222 char *p = buf;
223
Guido van Rossum79f25d91997-04-29 20:08:16 +0000224 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000225 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000226 else if (message == NULL) {
227 if (fname != NULL) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000228 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000229 p += strlen(p);
230 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000231 if (iarg != 0) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000232 PyOS_snprintf(p, sizeof(buf) - (buf - p),
233 "argument %d", iarg);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000234 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000235 p += strlen(p);
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000236 while (levels[i] > 0 && (int)(p-buf) < 220) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000237 PyOS_snprintf(p, sizeof(buf) - (buf - p),
238 ", item %d", levels[i]-1);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000239 p += strlen(p);
240 i++;
241 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000242 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000243 else {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000244 PyOS_snprintf(p, sizeof(buf) - (buf - p), "argument");
Ka-Ping Yee20579702001-01-15 22:14:16 +0000245 p += strlen(p);
246 }
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000247 PyOS_snprintf(p, sizeof(buf) - (buf - p), " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000248 message = buf;
249 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000250 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000251}
252
253
254/* Convert a tuple argument.
255 On entry, *p_format points to the character _after_ the opening '('.
256 On successful exit, *p_format points to the closing ')'.
257 If successful:
258 *p_format and *p_va are updated,
259 *levels and *msgbuf are untouched,
260 and NULL is returned.
261 If the argument is invalid:
262 *p_format is unchanged,
263 *p_va is undefined,
264 *levels is a 0-terminated list of item numbers,
265 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000266 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000267 <typename1> is the name of the expected type, and
268 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000269 and msgbuf is returned.
270*/
271
272static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000273converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
274 char *msgbuf, int toplevel)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000275{
276 int level = 0;
277 int n = 0;
278 char *format = *p_format;
279 int i;
280
281 for (;;) {
282 int c = *format++;
283 if (c == '(') {
284 if (level == 0)
285 n++;
286 level++;
287 }
288 else if (c == ')') {
289 if (level == 0)
290 break;
291 level--;
292 }
293 else if (c == ':' || c == ';' || c == '\0')
294 break;
295 else if (level == 0 && isalpha(c))
296 n++;
297 }
298
Ka-Ping Yee20579702001-01-15 22:14:16 +0000299 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000300 levels[0] = 0;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000301 PyOS_snprintf(msgbuf, sizeof(msgbuf),
302 toplevel ? "expected %d arguments, not %.50s" :
303 "must be %d-item sequence, not %.50s",
304 n,
305 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000306 return msgbuf;
307 }
308
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000309 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000310 levels[0] = 0;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000311 PyOS_snprintf(msgbuf, sizeof(msgbuf),
312 toplevel ? "expected %d arguments, not %d" :
313 "must be sequence of length %d, not %d",
314 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000315 return msgbuf;
316 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000317
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000318 format = *p_format;
319 for (i = 0; i < n; i++) {
320 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000321 PyObject *item;
322 item = PySequence_GetItem(arg, i);
323 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
324 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
325 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000326 if (msg != NULL) {
327 levels[0] = i+1;
328 return msg;
329 }
330 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000331
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000332 *p_format = format;
333 return NULL;
334}
335
336
337/* Convert a single item. */
338
339static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
341 char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000342{
343 char *msg;
344 char *format = *p_format;
345
346 if (*format == '(' /* ')' */) {
347 format++;
348 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
349 if (msg == NULL)
350 format++;
351 }
352 else {
353 msg = convertsimple(arg, &format, p_va, msgbuf);
354 if (msg != NULL)
355 levels[0] = 0;
356 }
357 if (msg == NULL)
358 *p_format = format;
359 return msg;
360}
361
362
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000363
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000364#define UNICODE_DEFAULT_ENCODING(arg) \
365 _PyUnicode_AsDefaultEncodedString(arg, NULL)
366
367/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000368
369static char *
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000370converterr(char *expected, PyObject *arg, char *msgbuf)
371{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000372 assert(expected != NULL);
373 assert(arg != NULL);
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000374 /* XXX use snprintf? */
375 sprintf(msgbuf,
376 "must be %.50s, not %.50s", expected,
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000377 arg == Py_None ? "None" : arg->ob_type->tp_name);
378 return msgbuf;
379}
380
381#define CONV_UNICODE "(unicode conversion error)"
382
383/* Convert a non-tuple argument. Return NULL if conversion went OK,
384 or a string with a message describing the failure. The message is
385 formatted as "must be <desired type>, not <actual type>".
386 When failing, an exception may or may not have been raised.
387 Don't call if a tuple is expected.
388*/
389
390static char *
391convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000392{
393 char *format = *p_format;
394 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000395 PyObject *uarg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000396
397 switch (c) {
398
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000399 case 'b': { /* unsigned byte -- very short int */
400 char *p = va_arg(*p_va, char *);
401 long ival = PyInt_AsLong(arg);
402 if (ival == -1 && PyErr_Occurred())
403 return converterr("integer<b>", arg, msgbuf);
404 else if (ival < 0) {
405 PyErr_SetString(PyExc_OverflowError,
406 "unsigned byte integer is less than minimum");
407 return converterr("integer<b>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000408 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000409 else if (ival > UCHAR_MAX) {
410 PyErr_SetString(PyExc_OverflowError,
411 "unsigned byte integer is greater than maximum");
412 return converterr("integer<b>", arg, msgbuf);
413 }
414 else
415 *p = (unsigned char) ival;
416 break;
417 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000418
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000419 case 'B': {/* byte sized bitfield - both signed and unsigned
420 values allowed */
421 char *p = va_arg(*p_va, char *);
422 long ival = PyInt_AsLong(arg);
423 if (ival == -1 && PyErr_Occurred())
424 return converterr("integer<b>", arg, msgbuf);
425 else if (ival < SCHAR_MIN) {
426 PyErr_SetString(PyExc_OverflowError,
427 "byte-sized integer bitfield is less than minimum");
428 return converterr("integer<B>", arg, msgbuf);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000429 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000430 else if (ival > (int)UCHAR_MAX) {
431 PyErr_SetString(PyExc_OverflowError,
432 "byte-sized integer bitfield is greater than maximum");
433 return converterr("integer<B>", arg, msgbuf);
434 }
435 else
436 *p = (unsigned char) ival;
437 break;
438 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000439
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000440 case 'h': {/* signed short int */
441 short *p = va_arg(*p_va, short *);
442 long ival = PyInt_AsLong(arg);
443 if (ival == -1 && PyErr_Occurred())
444 return converterr("integer<h>", arg, msgbuf);
445 else if (ival < SHRT_MIN) {
446 PyErr_SetString(PyExc_OverflowError,
447 "signed short integer is less than minimum");
448 return converterr("integer<h>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000449 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000450 else if (ival > SHRT_MAX) {
451 PyErr_SetString(PyExc_OverflowError,
452 "signed short integer is greater than maximum");
453 return converterr("integer<h>", arg, msgbuf);
454 }
455 else
456 *p = (short) ival;
457 break;
458 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000459
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000460 case 'H': { /* short int sized bitfield, both signed and
461 unsigned allowed */
462 unsigned short *p = va_arg(*p_va, unsigned short *);
463 long ival = PyInt_AsLong(arg);
464 if (ival == -1 && PyErr_Occurred())
465 return converterr("integer<H>", arg, msgbuf);
466 else if (ival < SHRT_MIN) {
467 PyErr_SetString(PyExc_OverflowError,
468 "short integer bitfield is less than minimum");
469 return converterr("integer<H>", arg, msgbuf);
Jack Jansend50338f2000-07-06 12:22:00 +0000470 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000471 else if (ival > USHRT_MAX) {
472 PyErr_SetString(PyExc_OverflowError,
473 "short integer bitfield is greater than maximum");
474 return converterr("integer<H>", arg, msgbuf);
475 }
476 else
477 *p = (unsigned short) ival;
478 break;
479 }
Jack Jansend50338f2000-07-06 12:22:00 +0000480
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000481 case 'i': {/* signed int */
482 int *p = va_arg(*p_va, int *);
483 long ival = PyInt_AsLong(arg);
484 if (ival == -1 && PyErr_Occurred())
485 return converterr("integer<i>", arg, msgbuf);
486 else if (ival > INT_MAX) {
487 PyErr_SetString(PyExc_OverflowError,
488 "signed integer is greater than maximum");
489 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000490 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000491 else if (ival < INT_MIN) {
492 PyErr_SetString(PyExc_OverflowError,
493 "signed integer is less than minimum");
494 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000495 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000496 else
497 *p = ival;
498 break;
499 }
500
501 case 'l': {/* long int */
502 long *p = va_arg(*p_va, long *);
503 long ival = PyInt_AsLong(arg);
504 if (ival == -1 && PyErr_Occurred())
505 return converterr("integer<l>", arg, msgbuf);
506 else
507 *p = ival;
508 break;
509 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000510
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000511#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000512 case 'L': {/* LONG_LONG */
513 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
514 LONG_LONG ival = PyLong_AsLongLong( arg );
515 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
516 return converterr("long<L>", arg, msgbuf);
517 } else {
518 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000519 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000520 break;
521 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000522#endif
523
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000524 case 'f': {/* float */
525 float *p = va_arg(*p_va, float *);
526 double dval = PyFloat_AsDouble(arg);
527 if (PyErr_Occurred())
528 return converterr("float<f>", arg, msgbuf);
529 else
530 *p = (float) dval;
531 break;
532 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000533
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000534 case 'd': {/* double */
535 double *p = va_arg(*p_va, double *);
536 double dval = PyFloat_AsDouble(arg);
537 if (PyErr_Occurred())
538 return converterr("float<d>", arg, msgbuf);
539 else
540 *p = dval;
541 break;
542 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000543
Guido van Rossum530956d1996-07-21 02:27:43 +0000544#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000545 case 'D': {/* complex double */
546 Py_complex *p = va_arg(*p_va, Py_complex *);
547 Py_complex cval;
548 cval = PyComplex_AsCComplex(arg);
549 if (PyErr_Occurred())
550 return converterr("complex<D>", arg, msgbuf);
551 else
552 *p = cval;
553 break;
554 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000555#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000556
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000557 case 'c': {/* char */
558 char *p = va_arg(*p_va, char *);
559 if (PyString_Check(arg) && PyString_Size(arg) == 1)
Jeremy Hylton0407aea2001-10-10 02:51:57 +0000560 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000561 else
562 return converterr("char", arg, msgbuf);
563 break;
564 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000565
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000566 case 's': {/* string */
567 if (*format == '#') {
568 void **p = (void **)va_arg(*p_va, char **);
569 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000570
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000571 if (PyString_Check(arg)) {
572 *p = PyString_AS_STRING(arg);
573 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000574 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000575#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000576 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000577 uarg = UNICODE_DEFAULT_ENCODING(arg);
578 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000579 return converterr(CONV_UNICODE,
580 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000581 *p = PyString_AS_STRING(uarg);
582 *q = PyString_GET_SIZE(uarg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000583 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000584#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000585 else { /* any buffer-like object */
586 char *buf;
587 int count = convertbuffer(arg, p, &buf);
588 if (count < 0)
589 return converterr(buf, arg, msgbuf);
590 *q = count;
591 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000592 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000593 } else {
594 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000595
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000596 if (PyString_Check(arg))
597 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000598#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000599 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000600 uarg = UNICODE_DEFAULT_ENCODING(arg);
601 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000602 return converterr(CONV_UNICODE,
603 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000604 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000605 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000606#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000607 else
608 return converterr("string", arg, msgbuf);
609 if ((int)strlen(*p) != PyString_Size(arg))
610 return converterr("string without null bytes",
611 arg, msgbuf);
612 }
613 break;
614 }
615
616 case 'z': {/* string, may be NULL (None) */
617 if (*format == '#') { /* any buffer-like object */
618 void **p = (void **)va_arg(*p_va, char **);
619 int *q = va_arg(*p_va, int *);
620
621 if (arg == Py_None) {
622 *p = 0;
623 *q = 0;
624 }
625 else if (PyString_Check(arg)) {
626 *p = PyString_AS_STRING(arg);
627 *q = PyString_GET_SIZE(arg);
628 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000629#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000630 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000631 uarg = UNICODE_DEFAULT_ENCODING(arg);
632 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000633 return converterr(CONV_UNICODE,
634 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000635 *p = PyString_AS_STRING(uarg);
636 *q = PyString_GET_SIZE(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000637 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000638#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000639 else { /* any buffer-like object */
640 char *buf;
641 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000642 if (count < 0)
643 return converterr(buf, arg, msgbuf);
644 *q = count;
645 }
646 format++;
647 } else {
648 char **p = va_arg(*p_va, char **);
649
650 if (arg == Py_None)
651 *p = 0;
652 else if (PyString_Check(arg))
Jeremy Hyltona4c8cd72001-10-10 02:51:08 +0000653 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000654#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000655 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000656 uarg = UNICODE_DEFAULT_ENCODING(arg);
657 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000658 return converterr(CONV_UNICODE,
659 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000660 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000661 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000662#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000663 else
664 return converterr("string or None",
665 arg, msgbuf);
666 if (*format == '#') {
667 int *q = va_arg(*p_va, int *);
668 if (arg == Py_None)
669 *q = 0;
670 else
671 *q = PyString_Size(arg);
672 format++;
673 }
674 else if (*p != NULL &&
675 (int)strlen(*p) != PyString_Size(arg))
676 return converterr(
677 "string without null bytes or None",
678 arg, msgbuf);
679 }
680 break;
681 }
682
683 case 'e': {/* encoded string */
684 char **buffer;
685 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000686 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000687 int size, recode_strings;
688
689 /* Get 'e' parameter: the encoding name */
690 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000691#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000692 if (encoding == NULL)
693 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000694#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000695
696 /* Get output buffer parameter:
697 's' (recode all objects via Unicode) or
698 't' (only recode non-string objects)
699 */
700 if (*format == 's')
701 recode_strings = 1;
702 else if (*format == 't')
703 recode_strings = 0;
704 else
705 return converterr(
706 "(unknown parser marker combination)",
707 arg, msgbuf);
708 buffer = (char **)va_arg(*p_va, char **);
709 format++;
710 if (buffer == NULL)
711 return converterr("(buffer is NULL)",
712 arg, msgbuf);
713
714 /* Encode object */
715 if (!recode_strings && PyString_Check(arg)) {
716 s = arg;
717 Py_INCREF(s);
718 }
719 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000720#ifdef Py_USING_UNICODE
721 PyObject *u;
722
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000723 /* Convert object to Unicode */
724 u = PyUnicode_FromObject(arg);
725 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000726 return converterr(
727 "string or unicode or text buffer",
728 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000729
730 /* Encode object; use default error handling */
731 s = PyUnicode_AsEncodedString(u,
732 encoding,
733 NULL);
734 Py_DECREF(u);
735 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000736 return converterr("(encoding failed)",
737 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000738 if (!PyString_Check(s)) {
739 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000740 return converterr(
741 "(encoder failed to return a string)",
742 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000743 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000744#else
745 return converterr("string<e>", arg, msgbuf);
746#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000747 }
748 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000749
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000750 /* Write output; output is guaranteed to be 0-terminated */
751 if (*format == '#') {
752 /* Using buffer length parameter '#':
753
754 - if *buffer is NULL, a new buffer of the
755 needed size is allocated and the data
756 copied into it; *buffer is updated to point
757 to the new buffer; the caller is
758 responsible for PyMem_Free()ing it after
759 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000760
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000761 - if *buffer is not NULL, the data is
762 copied to *buffer; *buffer_len has to be
763 set to the size of the buffer on input;
764 buffer overflow is signalled with an error;
765 buffer has to provide enough room for the
766 encoded string plus the trailing 0-byte
767
768 - in both cases, *buffer_len is updated to
769 the size of the buffer /excluding/ the
770 trailing 0-byte
771
772 */
773 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000774
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000775 format++;
776 if (buffer_len == NULL)
777 return converterr(
778 "(buffer_len is NULL)",
779 arg, msgbuf);
780 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000781 *buffer = PyMem_NEW(char, size + 1);
782 if (*buffer == NULL) {
783 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000784 return converterr(
785 "(memory error)",
786 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000787 }
Fred Drake25871c02000-05-03 15:17:02 +0000788 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000789 if (size + 1 > *buffer_len) {
790 Py_DECREF(s);
791 return converterr(
792 "(buffer overflow)",
793 arg, msgbuf);
794 }
Fred Drake25871c02000-05-03 15:17:02 +0000795 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000796 memcpy(*buffer,
797 PyString_AS_STRING(s),
798 size + 1);
799 *buffer_len = size;
800 } else {
801 /* Using a 0-terminated buffer:
802
803 - the encoded string has to be 0-terminated
804 for this variant to work; if it is not, an
805 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000806
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000807 - a new buffer of the needed size is
808 allocated and the data copied into it;
809 *buffer is updated to point to the new
810 buffer; the caller is responsible for
811 PyMem_Free()ing it after usage
812
813 */
814 if ((int)strlen(PyString_AS_STRING(s)) != size)
815 return converterr(
816 "(encoded string without NULL bytes)",
817 arg, msgbuf);
818 *buffer = PyMem_NEW(char, size + 1);
819 if (*buffer == NULL) {
820 Py_DECREF(s);
821 return converterr("(memory error)",
822 arg, msgbuf);
823 }
824 memcpy(*buffer,
825 PyString_AS_STRING(s),
826 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000827 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000828 Py_DECREF(s);
829 break;
830 }
831
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000832#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000833 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
834 if (*format == '#') { /* any buffer-like object */
835 void **p = (void **)va_arg(*p_va, char **);
836 int *q = va_arg(*p_va, int *);
837 char *buf;
838 int count = convertbuffer(arg, p, &buf);
839
840 if (count < 0)
841 return converterr(buf, arg, msgbuf);
842 *q = count/(sizeof(Py_UNICODE));
843 format++;
844 } else {
845 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
846
Guido van Rossume826ef02000-03-10 23:02:17 +0000847 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000848 *p = PyUnicode_AS_UNICODE(arg);
849 else
850 return converterr("unicode", arg, msgbuf);
851 }
852 break;
853 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000854#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000855
856 case 'S': { /* string object */
857 PyObject **p = va_arg(*p_va, PyObject **);
858 if (PyString_Check(arg))
859 *p = arg;
860 else
861 return converterr("string", arg, msgbuf);
862 break;
863 }
864
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000865#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000866 case 'U': { /* Unicode object */
867 PyObject **p = va_arg(*p_va, PyObject **);
868 if (PyUnicode_Check(arg))
869 *p = arg;
870 else
871 return converterr("unicode", arg, msgbuf);
872 break;
873 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000874#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000875
876 case 'O': { /* object */
877 PyTypeObject *type;
878 PyObject **p;
879 if (*format == '!') {
880 type = va_arg(*p_va, PyTypeObject*);
881 p = va_arg(*p_va, PyObject **);
882 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +0000883 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +0000884 *p = arg;
885 else
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000886 return converterr(type->tp_name, arg, msgbuf);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000887
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000888 }
889 else if (*format == '?') {
890 inquiry pred = va_arg(*p_va, inquiry);
891 p = va_arg(*p_va, PyObject **);
892 format++;
893 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000894 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000895 else
896 return converterr("(unspecified)",
897 arg, msgbuf);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000898
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000899 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000900 else if (*format == '&') {
901 typedef int (*converter)(PyObject *, void *);
902 converter convert = va_arg(*p_va, converter);
903 void *addr = va_arg(*p_va, void *);
904 format++;
905 if (! (*convert)(arg, addr))
906 return converterr("(unspecified)",
907 arg, msgbuf);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000908 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000909 else {
910 p = va_arg(*p_va, PyObject **);
911 *p = arg;
912 }
913 break;
914 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000915
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000916
917 case 'w': { /* memory buffer, read-write access */
918 void **p = va_arg(*p_va, void **);
919 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
920 int count;
921
922 if (pb == NULL ||
923 pb->bf_getwritebuffer == NULL ||
924 pb->bf_getsegcount == NULL)
925 return converterr("read-write buffer", arg, msgbuf);
926 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
927 return converterr("single-segment read-write buffer",
928 arg, msgbuf);
929 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
930 return converterr("(unspecified)", arg, msgbuf);
931 if (*format == '#') {
932 int *q = va_arg(*p_va, int *);
933
934 *q = count;
935 format++;
936 }
937 break;
938 }
939
940 case 't': { /* 8-bit character buffer, read-only access */
941 const char **p = va_arg(*p_va, const char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +0000942 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000943 int count;
944
945 if (*format++ != '#')
946 return converterr(
947 "invalid use of 't' format character",
948 arg, msgbuf);
949 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +0000950 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
951 pb == NULL || pb->bf_getcharbuffer == NULL ||
952 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000953 return converterr(
954 "string or read-only character buffer",
955 arg, msgbuf);
956
Jeremy Hylton4819e972001-10-11 14:40:37 +0000957 if (pb->bf_getsegcount(arg, NULL) != 1)
958 return converterr(
959 "string or single-segment read-only buffer",
960 arg, msgbuf);
961
962 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000963 if (count < 0)
Jeremy Hylton4819e972001-10-11 14:40:37 +0000964 return converterr("(unspecified)", arg, msgbuf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000965 *va_arg(*p_va, int *) = count;
966 break;
967 }
968
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000969 default:
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000970 return converterr("impossible<bad format char>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000971
972 }
973
974 *p_format = format;
975 return NULL;
976}
Guido van Rossumaa354651996-08-19 19:32:04 +0000977
Fred Drake563dfc22001-10-23 14:41:08 +0000978static int
979convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000980{
981 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
982 int count;
983 if (pb == NULL ||
984 pb->bf_getreadbuffer == NULL ||
985 pb->bf_getsegcount == NULL) {
986 *errmsg = "string or read-only buffer";
987 return -1;
988 }
989 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
990 *errmsg = "string or single-segment read-only buffer";
991 return -1;
992 }
993 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
994 *errmsg = "(unspecified)";
995 }
996 return count;
997}
Guido van Rossumaa354651996-08-19 19:32:04 +0000998
999/* Support for keyword arguments donated by
1000 Geoff Philbrick <philbric@delphi.hks.com> */
1001
Tim Petersf8cd3e82001-10-27 04:26:57 +00001002/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001003int
1004PyArg_ParseTupleAndKeywords(PyObject *args,
1005 PyObject *keywords,
1006 char *format,
1007 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001008{
1009 int retval;
1010 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001011
1012 if ((args == NULL || !PyTuple_Check(args)) ||
1013 (keywords != NULL && !PyDict_Check(keywords)) ||
1014 format == NULL ||
1015 kwlist == NULL)
1016 {
1017 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001018 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001019 }
1020
Guido van Rossumaa354651996-08-19 19:32:04 +00001021 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001022 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
1023 va_end(va);
1024 return retval;
1025}
1026
1027
1028static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001029vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
1030 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001031{
Tim Petersdc5eff92001-10-27 06:53:00 +00001032 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001033 int levels[32];
Tim Petersf8cd3e82001-10-27 04:26:57 +00001034 char *fname, *message;
1035 int min, max;
Tim Peters6fb26352001-10-27 04:38:11 +00001036 char *formatsave;
Tim Petersb639d492001-10-27 07:00:56 +00001037 int i, len, nargs, nkeywords;
Tim Petersc2f01122001-10-27 07:25:06 +00001038 char *msg, **p;
Tim Petersf4331c12001-10-27 00:17:34 +00001039
Tim Peters45772cd2001-10-27 03:58:40 +00001040 assert(args != NULL && PyTuple_Check(args));
1041 assert(keywords == NULL || PyDict_Check(keywords));
1042 assert(format != NULL);
1043 assert(kwlist != NULL);
1044 assert(p_va != NULL);
1045
Tim Petersf8cd3e82001-10-27 04:26:57 +00001046 /* Search the format:
1047 message <- error msg, if any (else NULL).
1048 name <- routine name, if any (else NULL).
1049 min <- # of required arguments, or -1 if all are required.
1050 max <- most arguments (required + optional).
Tim Peters62d48e12001-10-27 06:42:16 +00001051 Check that kwlist has a non-NULL entry for each arg.
Tim Petersf8cd3e82001-10-27 04:26:57 +00001052 Raise error if a tuple arg spec is found.
1053 */
1054 fname = message = NULL;
Tim Peters6fb26352001-10-27 04:38:11 +00001055 formatsave = format;
Tim Peters62d48e12001-10-27 06:42:16 +00001056 p = kwlist;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001057 min = -1;
1058 max = 0;
1059 while ((i = *format++) != '\0') {
Tim Peters62d48e12001-10-27 06:42:16 +00001060 if (isalpha(i) && i != 'e') {
Tim Petersf8cd3e82001-10-27 04:26:57 +00001061 max++;
Tim Peters62d48e12001-10-27 06:42:16 +00001062 if (*p == NULL) {
1063 /* kwlist is too short */
1064 PyErr_BadInternalCall();
1065 return 0;
1066 }
1067 p++;
1068 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001069 else if (i == '|')
1070 min = max;
1071 else if (i == ':') {
1072 fname = format;
1073 break;
1074 }
1075 else if (i == ';') {
1076 message = format;
1077 break;
1078 }
1079 else if (i == '(') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001080 PyErr_SetString(PyExc_SystemError,
1081 "tuple found in format when using keyword arguments");
1082 return 0;
1083 }
Tim Peters62d48e12001-10-27 06:42:16 +00001084 }
1085 format = formatsave;
1086 if (*p != NULL) {
1087 /* kwlist is too long */
1088 PyErr_BadInternalCall();
1089 return 0;
1090 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001091 if (min < 0) {
1092 /* All arguments are required. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001093 min = max;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001094 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001095
Tim Peters6fb26352001-10-27 04:38:11 +00001096 nargs = PyTuple_GET_SIZE(args);
Tim Petersb0872fc2001-10-27 04:45:34 +00001097 nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001098
Guido van Rossumaa354651996-08-19 19:32:04 +00001099 /* make sure there are no duplicate values for an argument;
1100 its not clear when to use the term "keyword argument vs.
1101 keyword parameter in messages */
Tim Petersb054be42001-10-27 05:07:41 +00001102 if (nkeywords > 0) {
Tim Peters6fb26352001-10-27 04:38:11 +00001103 for (i = 0; i < nargs; i++) {
Tim Petersa9f47392001-10-27 00:46:09 +00001104 char *thiskw = kwlist[i];
1105 if (thiskw == NULL)
1106 break;
Tim Peters077f5742001-10-27 05:50:39 +00001107 if (PyDict_GetItemString(keywords, thiskw)) {
Tim Petersb054be42001-10-27 05:07:41 +00001108 PyErr_Format(PyExc_TypeError,
1109 "keyword parameter '%s' was given "
1110 "by position and by name",
Tim Petersa9f47392001-10-27 00:46:09 +00001111 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001112 return 0;
1113 }
Tim Peters0af49162001-10-27 06:14:32 +00001114 else if (PyErr_Occurred())
1115 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001116 }
1117 }
Tim Peters61dde632001-10-27 05:30:17 +00001118
Guido van Rossumaa354651996-08-19 19:32:04 +00001119 /* required arguments missing from args can be supplied by keyword
Tim Peters62d48e12001-10-27 06:42:16 +00001120 arguments; set len to the number of posiitional arguments, and,
1121 if that's less than the minimum required, add in the number of
1122 required arguments that are supplied by keywords */
Tim Peters6fb26352001-10-27 04:38:11 +00001123 len = nargs;
Tim Peters62d48e12001-10-27 06:42:16 +00001124 if (nkeywords > 0 && nargs < min) {
Tim Peters6fb26352001-10-27 04:38:11 +00001125 for (i = nargs; i < min; i++) {
Tim Peters077f5742001-10-27 05:50:39 +00001126 if (PyDict_GetItemString(keywords, kwlist[i]))
Guido van Rossumaa354651996-08-19 19:32:04 +00001127 len++;
Tim Peters0af49162001-10-27 06:14:32 +00001128 else if (PyErr_Occurred())
1129 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001130 }
1131 }
Tim Peters0af49162001-10-27 06:14:32 +00001132
Guido van Rossumaa354651996-08-19 19:32:04 +00001133 /* make sure we got an acceptable number of arguments; the message
1134 is a little confusing with keywords since keyword arguments
1135 which are supplied, but don't match the required arguments
1136 are not included in the "%d given" part of the message */
Guido van Rossumaa354651996-08-19 19:32:04 +00001137 if (len < min || max < len) {
1138 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +00001139 PyOS_snprintf(msgbuf, sizeof(msgbuf),
1140 "%.200s%s takes %s %d argument%s "
1141 "(%d given)",
1142 fname==NULL ? "function" : fname,
1143 fname==NULL ? "" : "()",
1144 min==max ? "exactly"
1145 : len < min ? "at least" : "at most",
1146 len < min ? min : max,
1147 (len < min ? min : max) == 1 ? "" : "s",
1148 len);
Guido van Rossumaa354651996-08-19 19:32:04 +00001149 message = msgbuf;
1150 }
1151 PyErr_SetString(PyExc_TypeError, message);
1152 return 0;
1153 }
Tim Petersc2f01122001-10-27 07:25:06 +00001154
1155 /* convert the positional arguments */
Tim Peters6fb26352001-10-27 04:38:11 +00001156 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001157 if (*format == '|')
1158 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001159 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001160 levels, msgbuf);
1161 if (msg) {
1162 seterror(i+1, msg, levels, fname, message);
1163 return 0;
1164 }
1165 }
1166
Tim Petersc2f01122001-10-27 07:25:06 +00001167 /* handle no keyword parameters in call */
Tim Petersb054be42001-10-27 05:07:41 +00001168 if (nkeywords == 0)
Tim Peters28bf7a92001-10-27 04:33:41 +00001169 return 1;
Tim Petersb054be42001-10-27 05:07:41 +00001170
Guido van Rossumaa354651996-08-19 19:32:04 +00001171 /* convert the keyword arguments; this uses the format
1172 string where it was left after processing args */
Tim Petersb639d492001-10-27 07:00:56 +00001173 for (i = nargs; i < max; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001175 if (*format == '|')
1176 format++;
Tim Peters077f5742001-10-27 05:50:39 +00001177 item = PyDict_GetItemString(keywords, kwlist[i]);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001178 if (item != NULL) {
Tim Peters077f5742001-10-27 05:50:39 +00001179 Py_INCREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001180 msg = convertitem(item, &format, p_va, levels, msgbuf);
Tim Peters077f5742001-10-27 05:50:39 +00001181 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001182 if (msg) {
1183 seterror(i+1, msg, levels, fname, message);
1184 return 0;
1185 }
Tim Petersc2f01122001-10-27 07:25:06 +00001186 --nkeywords;
1187 if (nkeywords == 0)
1188 break;
Guido van Rossumaa354651996-08-19 19:32:04 +00001189 }
Tim Peters0af49162001-10-27 06:14:32 +00001190 else if (PyErr_Occurred())
1191 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001192 else {
Guido van Rossumaa354651996-08-19 19:32:04 +00001193 msg = skipitem(&format, p_va);
1194 if (msg) {
1195 seterror(i+1, msg, levels, fname, message);
1196 return 0;
1197 }
1198 }
1199 }
Tim Petersb054be42001-10-27 05:07:41 +00001200
Guido van Rossumaa354651996-08-19 19:32:04 +00001201 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001202 if (nkeywords > 0) {
1203 PyObject *key, *value;
1204 int pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001205 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001206 int match = 0;
1207 char *ks = PyString_AsString(key);
Tim Petersb639d492001-10-27 07:00:56 +00001208 for (i = 0; i < max; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001209 if (!strcmp(ks, kwlist[i])) {
1210 match = 1;
1211 break;
1212 }
1213 }
1214 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001215 PyErr_Format(PyExc_TypeError,
1216 "'%s' is an invalid keyword "
1217 "argument for this function",
1218 ks);
Guido van Rossumaa354651996-08-19 19:32:04 +00001219 return 0;
1220 }
1221 }
1222 }
Tim Petersc2f01122001-10-27 07:25:06 +00001223
Guido van Rossumaa354651996-08-19 19:32:04 +00001224 return 1;
1225}
1226
1227
1228static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001229skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001230{
1231 char *format = *p_format;
1232 char c = *format++;
1233
1234 switch (c) {
1235
1236 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001237 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001238 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001239 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001240 break;
1241 }
1242
1243 case 'h': /* short int */
1244 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001245 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001246 break;
1247 }
1248
Jack Jansencc22fbe2000-08-05 21:29:58 +00001249 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001250 {
1251 (void) va_arg(*p_va, unsigned short *);
1252 break;
1253 }
1254
Guido van Rossumaa354651996-08-19 19:32:04 +00001255 case 'i': /* int */
1256 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001257 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001258 break;
1259 }
1260
1261 case 'l': /* long int */
1262 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001263 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001264 break;
1265 }
1266
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001267#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001268 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001269 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001270 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001271 break;
1272 }
1273#endif
1274
Guido van Rossumaa354651996-08-19 19:32:04 +00001275 case 'f': /* float */
1276 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001277 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001278 break;
1279 }
1280
1281 case 'd': /* double */
1282 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001283 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001284 break;
1285 }
1286
1287#ifndef WITHOUT_COMPLEX
1288 case 'D': /* complex double */
1289 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001290 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001291 break;
1292 }
1293#endif /* WITHOUT_COMPLEX */
1294
1295 case 'c': /* char */
1296 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001297 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001298 break;
1299 }
1300
1301 case 's': /* string */
1302 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001303 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001304 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001305 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001306 format++;
1307 }
1308 break;
1309 }
1310
1311 case 'z': /* string */
1312 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001313 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001314 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001315 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001316 format++;
1317 }
1318 break;
1319 }
1320
1321 case 'S': /* string object */
1322 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001323 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001324 break;
1325 }
1326
1327 case 'O': /* object */
1328 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001329 if (*format == '!') {
1330 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331 (void) va_arg(*p_va, PyTypeObject*);
1332 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001333 }
1334#if 0
1335/* I don't know what this is for */
1336 else if (*format == '?') {
1337 inquiry pred = va_arg(*p_va, inquiry);
1338 format++;
1339 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001340 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001341 }
1342 }
1343#endif
1344 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001345 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001346 (void) va_arg(*p_va, converter);
1347 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001348 format++;
1349 }
1350 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001351 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001352 }
1353 break;
1354 }
1355
1356 default:
1357 return "impossible<bad format char>";
1358
1359 }
1360
1361 *p_format = format;
1362 return NULL;
1363}
Fred Drakee4616e62001-10-23 21:09:29 +00001364
1365
1366int
1367PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
1368{
1369 int i, l;
1370 PyObject **o;
1371 va_list vargs;
1372
1373#ifdef HAVE_STDARG_PROTOTYPES
1374 va_start(vargs, max);
1375#else
1376 va_start(vargs);
1377#endif
1378
1379 assert(min >= 0);
1380 assert(min <= max);
1381 if (!PyTuple_Check(args)) {
1382 PyErr_SetString(PyExc_SystemError,
1383 "PyArg_UnpackTuple() argument list is not a tuple");
1384 return 0;
1385 }
1386 l = PyTuple_GET_SIZE(args);
1387 if (l < min) {
1388 if (name != NULL)
1389 PyErr_Format(
1390 PyExc_TypeError,
1391 "%s expected %s%d arguments, got %d",
1392 name, (min == max ? "" : "at least "), min, l);
1393 else
1394 PyErr_Format(
1395 PyExc_TypeError,
1396 "unpacked tuple should have %s%d elements,"
1397 " but has %d",
1398 (min == max ? "" : "at least "), min, l);
1399 va_end(vargs);
1400 return 0;
1401 }
1402 if (l > max) {
1403 if (name != NULL)
1404 PyErr_Format(
1405 PyExc_TypeError,
1406 "%s expected %s%d arguments, got %d",
1407 name, (min == max ? "" : "at most "), max, l);
1408 else
1409 PyErr_Format(
1410 PyExc_TypeError,
1411 "unpacked tuple should have %s%d elements,"
1412 " but has %d",
1413 (min == max ? "" : "at most "), max, l);
1414 va_end(vargs);
1415 return 0;
1416 }
1417 for (i = 0; i < l; i++) {
1418 o = va_arg(vargs, PyObject **);
1419 *o = PyTuple_GET_ITEM(args, i);
1420 }
1421 va_end(vargs);
1422 return 1;
1423}