blob: b7bbb5a80af26671a39a00571a29b5f265895f78 [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) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000227 /* XXX snprintf */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000228 if (fname != NULL) {
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000229 sprintf(p, "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000230 p += strlen(p);
231 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000232 if (iarg != 0) {
233 sprintf(p, "argument %d", iarg);
234 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000235 p += strlen(p);
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000236 while (levels[i] > 0 && (int)(p-buf) < 220) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000237 sprintf(p, ", item %d", levels[i]-1);
238 p += strlen(p);
239 i++;
240 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000241 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000242 else {
243 sprintf(p, "argument");
244 p += strlen(p);
245 }
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000246 sprintf(p, " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000247 message = buf;
248 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000249 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000250}
251
252
253/* Convert a tuple argument.
254 On entry, *p_format points to the character _after_ the opening '('.
255 On successful exit, *p_format points to the closing ')'.
256 If successful:
257 *p_format and *p_va are updated,
258 *levels and *msgbuf are untouched,
259 and NULL is returned.
260 If the argument is invalid:
261 *p_format is unchanged,
262 *p_va is undefined,
263 *levels is a 0-terminated list of item numbers,
264 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000265 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000266 <typename1> is the name of the expected type, and
267 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000268 and msgbuf is returned.
269*/
270
271static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000272converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
273 char *msgbuf, int toplevel)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000274{
275 int level = 0;
276 int n = 0;
277 char *format = *p_format;
278 int i;
279
280 for (;;) {
281 int c = *format++;
282 if (c == '(') {
283 if (level == 0)
284 n++;
285 level++;
286 }
287 else if (c == ')') {
288 if (level == 0)
289 break;
290 level--;
291 }
292 else if (c == ':' || c == ';' || c == '\0')
293 break;
294 else if (level == 0 && isalpha(c))
295 n++;
296 }
297
Ka-Ping Yee20579702001-01-15 22:14:16 +0000298 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000299 levels[0] = 0;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000300 PyOS_snprintf(msgbuf, sizeof(msgbuf),
301 toplevel ? "expected %d arguments, not %.50s" :
302 "must be %d-item sequence, not %.50s",
303 n,
304 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000305 return msgbuf;
306 }
307
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000308 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000309 levels[0] = 0;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000310 PyOS_snprintf(msgbuf, sizeof(msgbuf),
311 toplevel ? "expected %d arguments, not %d" :
312 "must be sequence of length %d, not %d",
313 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000314 return msgbuf;
315 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000316
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000317 format = *p_format;
318 for (i = 0; i < n; i++) {
319 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000320 PyObject *item;
321 item = PySequence_GetItem(arg, i);
322 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
323 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
324 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000325 if (msg != NULL) {
326 levels[0] = i+1;
327 return msg;
328 }
329 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000330
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000331 *p_format = format;
332 return NULL;
333}
334
335
336/* Convert a single item. */
337
338static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
340 char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000341{
342 char *msg;
343 char *format = *p_format;
344
345 if (*format == '(' /* ')' */) {
346 format++;
347 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
348 if (msg == NULL)
349 format++;
350 }
351 else {
352 msg = convertsimple(arg, &format, p_va, msgbuf);
353 if (msg != NULL)
354 levels[0] = 0;
355 }
356 if (msg == NULL)
357 *p_format = format;
358 return msg;
359}
360
361
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000362
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000363#define UNICODE_DEFAULT_ENCODING(arg) \
364 _PyUnicode_AsDefaultEncodedString(arg, NULL)
365
366/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000367
368static char *
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000369converterr(char *expected, PyObject *arg, char *msgbuf)
370{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000371 assert(expected != NULL);
372 assert(arg != NULL);
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000373 /* XXX use snprintf? */
374 sprintf(msgbuf,
375 "must be %.50s, not %.50s", expected,
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000376 arg == Py_None ? "None" : arg->ob_type->tp_name);
377 return msgbuf;
378}
379
380#define CONV_UNICODE "(unicode conversion error)"
381
382/* Convert a non-tuple argument. Return NULL if conversion went OK,
383 or a string with a message describing the failure. The message is
384 formatted as "must be <desired type>, not <actual type>".
385 When failing, an exception may or may not have been raised.
386 Don't call if a tuple is expected.
387*/
388
389static char *
390convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000391{
392 char *format = *p_format;
393 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000394 PyObject *uarg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000395
396 switch (c) {
397
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000398 case 'b': { /* unsigned byte -- very short int */
399 char *p = va_arg(*p_va, char *);
400 long ival = PyInt_AsLong(arg);
401 if (ival == -1 && PyErr_Occurred())
402 return converterr("integer<b>", arg, msgbuf);
403 else if (ival < 0) {
404 PyErr_SetString(PyExc_OverflowError,
405 "unsigned byte integer is less than minimum");
406 return converterr("integer<b>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000407 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000408 else if (ival > UCHAR_MAX) {
409 PyErr_SetString(PyExc_OverflowError,
410 "unsigned byte integer is greater than maximum");
411 return converterr("integer<b>", arg, msgbuf);
412 }
413 else
414 *p = (unsigned char) ival;
415 break;
416 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000417
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000418 case 'B': {/* byte sized bitfield - both signed and unsigned
419 values allowed */
420 char *p = va_arg(*p_va, char *);
421 long ival = PyInt_AsLong(arg);
422 if (ival == -1 && PyErr_Occurred())
423 return converterr("integer<b>", arg, msgbuf);
424 else if (ival < SCHAR_MIN) {
425 PyErr_SetString(PyExc_OverflowError,
426 "byte-sized integer bitfield is less than minimum");
427 return converterr("integer<B>", arg, msgbuf);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000428 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000429 else if (ival > (int)UCHAR_MAX) {
430 PyErr_SetString(PyExc_OverflowError,
431 "byte-sized integer bitfield is greater than maximum");
432 return converterr("integer<B>", arg, msgbuf);
433 }
434 else
435 *p = (unsigned char) ival;
436 break;
437 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000438
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000439 case 'h': {/* signed short int */
440 short *p = va_arg(*p_va, short *);
441 long ival = PyInt_AsLong(arg);
442 if (ival == -1 && PyErr_Occurred())
443 return converterr("integer<h>", arg, msgbuf);
444 else if (ival < SHRT_MIN) {
445 PyErr_SetString(PyExc_OverflowError,
446 "signed short integer is less than minimum");
447 return converterr("integer<h>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000448 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000449 else if (ival > SHRT_MAX) {
450 PyErr_SetString(PyExc_OverflowError,
451 "signed short integer is greater than maximum");
452 return converterr("integer<h>", arg, msgbuf);
453 }
454 else
455 *p = (short) ival;
456 break;
457 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000458
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000459 case 'H': { /* short int sized bitfield, both signed and
460 unsigned allowed */
461 unsigned short *p = va_arg(*p_va, unsigned short *);
462 long ival = PyInt_AsLong(arg);
463 if (ival == -1 && PyErr_Occurred())
464 return converterr("integer<H>", arg, msgbuf);
465 else if (ival < SHRT_MIN) {
466 PyErr_SetString(PyExc_OverflowError,
467 "short integer bitfield is less than minimum");
468 return converterr("integer<H>", arg, msgbuf);
Jack Jansend50338f2000-07-06 12:22:00 +0000469 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000470 else if (ival > USHRT_MAX) {
471 PyErr_SetString(PyExc_OverflowError,
472 "short integer bitfield is greater than maximum");
473 return converterr("integer<H>", arg, msgbuf);
474 }
475 else
476 *p = (unsigned short) ival;
477 break;
478 }
Jack Jansend50338f2000-07-06 12:22:00 +0000479
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000480 case 'i': {/* signed int */
481 int *p = va_arg(*p_va, int *);
482 long ival = PyInt_AsLong(arg);
483 if (ival == -1 && PyErr_Occurred())
484 return converterr("integer<i>", arg, msgbuf);
485 else if (ival > INT_MAX) {
486 PyErr_SetString(PyExc_OverflowError,
487 "signed integer is greater than maximum");
488 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000489 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000490 else if (ival < INT_MIN) {
491 PyErr_SetString(PyExc_OverflowError,
492 "signed integer is less than minimum");
493 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000494 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000495 else
496 *p = ival;
497 break;
498 }
499
500 case 'l': {/* long int */
501 long *p = va_arg(*p_va, long *);
502 long ival = PyInt_AsLong(arg);
503 if (ival == -1 && PyErr_Occurred())
504 return converterr("integer<l>", arg, msgbuf);
505 else
506 *p = ival;
507 break;
508 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000509
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000510#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000511 case 'L': {/* LONG_LONG */
512 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
513 LONG_LONG ival = PyLong_AsLongLong( arg );
514 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
515 return converterr("long<L>", arg, msgbuf);
516 } else {
517 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000518 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000519 break;
520 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000521#endif
522
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000523 case 'f': {/* float */
524 float *p = va_arg(*p_va, float *);
525 double dval = PyFloat_AsDouble(arg);
526 if (PyErr_Occurred())
527 return converterr("float<f>", arg, msgbuf);
528 else
529 *p = (float) dval;
530 break;
531 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000532
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000533 case 'd': {/* double */
534 double *p = va_arg(*p_va, double *);
535 double dval = PyFloat_AsDouble(arg);
536 if (PyErr_Occurred())
537 return converterr("float<d>", arg, msgbuf);
538 else
539 *p = dval;
540 break;
541 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000542
Guido van Rossum530956d1996-07-21 02:27:43 +0000543#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000544 case 'D': {/* complex double */
545 Py_complex *p = va_arg(*p_va, Py_complex *);
546 Py_complex cval;
547 cval = PyComplex_AsCComplex(arg);
548 if (PyErr_Occurred())
549 return converterr("complex<D>", arg, msgbuf);
550 else
551 *p = cval;
552 break;
553 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000554#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000555
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000556 case 'c': {/* char */
557 char *p = va_arg(*p_va, char *);
558 if (PyString_Check(arg) && PyString_Size(arg) == 1)
Jeremy Hylton0407aea2001-10-10 02:51:57 +0000559 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000560 else
561 return converterr("char", arg, msgbuf);
562 break;
563 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000564
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000565 case 's': {/* string */
566 if (*format == '#') {
567 void **p = (void **)va_arg(*p_va, char **);
568 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000569
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000570 if (PyString_Check(arg)) {
571 *p = PyString_AS_STRING(arg);
572 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000573 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000574#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000575 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000576 uarg = UNICODE_DEFAULT_ENCODING(arg);
577 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000578 return converterr(CONV_UNICODE,
579 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000580 *p = PyString_AS_STRING(uarg);
581 *q = PyString_GET_SIZE(uarg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000582 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000583#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000584 else { /* any buffer-like object */
585 char *buf;
586 int count = convertbuffer(arg, p, &buf);
587 if (count < 0)
588 return converterr(buf, arg, msgbuf);
589 *q = count;
590 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000591 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000592 } else {
593 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000594
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000595 if (PyString_Check(arg))
596 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000597#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000598 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000599 uarg = UNICODE_DEFAULT_ENCODING(arg);
600 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000601 return converterr(CONV_UNICODE,
602 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000603 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000604 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000605#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000606 else
607 return converterr("string", arg, msgbuf);
608 if ((int)strlen(*p) != PyString_Size(arg))
609 return converterr("string without null bytes",
610 arg, msgbuf);
611 }
612 break;
613 }
614
615 case 'z': {/* string, may be NULL (None) */
616 if (*format == '#') { /* any buffer-like object */
617 void **p = (void **)va_arg(*p_va, char **);
618 int *q = va_arg(*p_va, int *);
619
620 if (arg == Py_None) {
621 *p = 0;
622 *q = 0;
623 }
624 else if (PyString_Check(arg)) {
625 *p = PyString_AS_STRING(arg);
626 *q = PyString_GET_SIZE(arg);
627 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000628#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000629 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000630 uarg = UNICODE_DEFAULT_ENCODING(arg);
631 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000632 return converterr(CONV_UNICODE,
633 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000634 *p = PyString_AS_STRING(uarg);
635 *q = PyString_GET_SIZE(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000636 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000637#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000638 else { /* any buffer-like object */
639 char *buf;
640 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000641 if (count < 0)
642 return converterr(buf, arg, msgbuf);
643 *q = count;
644 }
645 format++;
646 } else {
647 char **p = va_arg(*p_va, char **);
648
649 if (arg == Py_None)
650 *p = 0;
651 else if (PyString_Check(arg))
Jeremy Hyltona4c8cd72001-10-10 02:51:08 +0000652 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000653#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000654 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000655 uarg = UNICODE_DEFAULT_ENCODING(arg);
656 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000657 return converterr(CONV_UNICODE,
658 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000659 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000660 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000661#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000662 else
663 return converterr("string or None",
664 arg, msgbuf);
665 if (*format == '#') {
666 int *q = va_arg(*p_va, int *);
667 if (arg == Py_None)
668 *q = 0;
669 else
670 *q = PyString_Size(arg);
671 format++;
672 }
673 else if (*p != NULL &&
674 (int)strlen(*p) != PyString_Size(arg))
675 return converterr(
676 "string without null bytes or None",
677 arg, msgbuf);
678 }
679 break;
680 }
681
682 case 'e': {/* encoded string */
683 char **buffer;
684 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000685 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000686 int size, recode_strings;
687
688 /* Get 'e' parameter: the encoding name */
689 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000690#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000691 if (encoding == NULL)
692 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000693#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000694
695 /* Get output buffer parameter:
696 's' (recode all objects via Unicode) or
697 't' (only recode non-string objects)
698 */
699 if (*format == 's')
700 recode_strings = 1;
701 else if (*format == 't')
702 recode_strings = 0;
703 else
704 return converterr(
705 "(unknown parser marker combination)",
706 arg, msgbuf);
707 buffer = (char **)va_arg(*p_va, char **);
708 format++;
709 if (buffer == NULL)
710 return converterr("(buffer is NULL)",
711 arg, msgbuf);
712
713 /* Encode object */
714 if (!recode_strings && PyString_Check(arg)) {
715 s = arg;
716 Py_INCREF(s);
717 }
718 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000719#ifdef Py_USING_UNICODE
720 PyObject *u;
721
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000722 /* Convert object to Unicode */
723 u = PyUnicode_FromObject(arg);
724 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000725 return converterr(
726 "string or unicode or text buffer",
727 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000728
729 /* Encode object; use default error handling */
730 s = PyUnicode_AsEncodedString(u,
731 encoding,
732 NULL);
733 Py_DECREF(u);
734 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000735 return converterr("(encoding failed)",
736 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000737 if (!PyString_Check(s)) {
738 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000739 return converterr(
740 "(encoder failed to return a string)",
741 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000742 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000743#else
744 return converterr("string<e>", arg, msgbuf);
745#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000746 }
747 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000748
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000749 /* Write output; output is guaranteed to be 0-terminated */
750 if (*format == '#') {
751 /* Using buffer length parameter '#':
752
753 - if *buffer is NULL, a new buffer of the
754 needed size is allocated and the data
755 copied into it; *buffer is updated to point
756 to the new buffer; the caller is
757 responsible for PyMem_Free()ing it after
758 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000759
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000760 - if *buffer is not NULL, the data is
761 copied to *buffer; *buffer_len has to be
762 set to the size of the buffer on input;
763 buffer overflow is signalled with an error;
764 buffer has to provide enough room for the
765 encoded string plus the trailing 0-byte
766
767 - in both cases, *buffer_len is updated to
768 the size of the buffer /excluding/ the
769 trailing 0-byte
770
771 */
772 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000773
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000774 format++;
775 if (buffer_len == NULL)
776 return converterr(
777 "(buffer_len is NULL)",
778 arg, msgbuf);
779 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000780 *buffer = PyMem_NEW(char, size + 1);
781 if (*buffer == NULL) {
782 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000783 return converterr(
784 "(memory error)",
785 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000786 }
Fred Drake25871c02000-05-03 15:17:02 +0000787 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000788 if (size + 1 > *buffer_len) {
789 Py_DECREF(s);
790 return converterr(
791 "(buffer overflow)",
792 arg, msgbuf);
793 }
Fred Drake25871c02000-05-03 15:17:02 +0000794 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000795 memcpy(*buffer,
796 PyString_AS_STRING(s),
797 size + 1);
798 *buffer_len = size;
799 } else {
800 /* Using a 0-terminated buffer:
801
802 - the encoded string has to be 0-terminated
803 for this variant to work; if it is not, an
804 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000805
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000806 - a new buffer of the needed size is
807 allocated and the data copied into it;
808 *buffer is updated to point to the new
809 buffer; the caller is responsible for
810 PyMem_Free()ing it after usage
811
812 */
813 if ((int)strlen(PyString_AS_STRING(s)) != size)
814 return converterr(
815 "(encoded string without NULL bytes)",
816 arg, msgbuf);
817 *buffer = PyMem_NEW(char, size + 1);
818 if (*buffer == NULL) {
819 Py_DECREF(s);
820 return converterr("(memory error)",
821 arg, msgbuf);
822 }
823 memcpy(*buffer,
824 PyString_AS_STRING(s),
825 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000826 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000827 Py_DECREF(s);
828 break;
829 }
830
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000831#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000832 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
833 if (*format == '#') { /* any buffer-like object */
834 void **p = (void **)va_arg(*p_va, char **);
835 int *q = va_arg(*p_va, int *);
836 char *buf;
837 int count = convertbuffer(arg, p, &buf);
838
839 if (count < 0)
840 return converterr(buf, arg, msgbuf);
841 *q = count/(sizeof(Py_UNICODE));
842 format++;
843 } else {
844 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
845
Guido van Rossume826ef02000-03-10 23:02:17 +0000846 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000847 *p = PyUnicode_AS_UNICODE(arg);
848 else
849 return converterr("unicode", arg, msgbuf);
850 }
851 break;
852 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000853#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000854
855 case 'S': { /* string object */
856 PyObject **p = va_arg(*p_va, PyObject **);
857 if (PyString_Check(arg))
858 *p = arg;
859 else
860 return converterr("string", arg, msgbuf);
861 break;
862 }
863
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000864#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000865 case 'U': { /* Unicode object */
866 PyObject **p = va_arg(*p_va, PyObject **);
867 if (PyUnicode_Check(arg))
868 *p = arg;
869 else
870 return converterr("unicode", arg, msgbuf);
871 break;
872 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000873#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000874
875 case 'O': { /* object */
876 PyTypeObject *type;
877 PyObject **p;
878 if (*format == '!') {
879 type = va_arg(*p_va, PyTypeObject*);
880 p = va_arg(*p_va, PyObject **);
881 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +0000882 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +0000883 *p = arg;
884 else
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000885 return converterr(type->tp_name, arg, msgbuf);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000886
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000887 }
888 else if (*format == '?') {
889 inquiry pred = va_arg(*p_va, inquiry);
890 p = va_arg(*p_va, PyObject **);
891 format++;
892 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000893 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000894 else
895 return converterr("(unspecified)",
896 arg, msgbuf);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000897
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000898 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000899 else if (*format == '&') {
900 typedef int (*converter)(PyObject *, void *);
901 converter convert = va_arg(*p_va, converter);
902 void *addr = va_arg(*p_va, void *);
903 format++;
904 if (! (*convert)(arg, addr))
905 return converterr("(unspecified)",
906 arg, msgbuf);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000907 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000908 else {
909 p = va_arg(*p_va, PyObject **);
910 *p = arg;
911 }
912 break;
913 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000914
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000915
916 case 'w': { /* memory buffer, read-write access */
917 void **p = va_arg(*p_va, void **);
918 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
919 int count;
920
921 if (pb == NULL ||
922 pb->bf_getwritebuffer == NULL ||
923 pb->bf_getsegcount == NULL)
924 return converterr("read-write buffer", arg, msgbuf);
925 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
926 return converterr("single-segment read-write buffer",
927 arg, msgbuf);
928 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
929 return converterr("(unspecified)", arg, msgbuf);
930 if (*format == '#') {
931 int *q = va_arg(*p_va, int *);
932
933 *q = count;
934 format++;
935 }
936 break;
937 }
938
939 case 't': { /* 8-bit character buffer, read-only access */
940 const char **p = va_arg(*p_va, const char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +0000941 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000942 int count;
943
944 if (*format++ != '#')
945 return converterr(
946 "invalid use of 't' format character",
947 arg, msgbuf);
948 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +0000949 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
950 pb == NULL || pb->bf_getcharbuffer == NULL ||
951 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000952 return converterr(
953 "string or read-only character buffer",
954 arg, msgbuf);
955
Jeremy Hylton4819e972001-10-11 14:40:37 +0000956 if (pb->bf_getsegcount(arg, NULL) != 1)
957 return converterr(
958 "string or single-segment read-only buffer",
959 arg, msgbuf);
960
961 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000962 if (count < 0)
Jeremy Hylton4819e972001-10-11 14:40:37 +0000963 return converterr("(unspecified)", arg, msgbuf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000964 *va_arg(*p_va, int *) = count;
965 break;
966 }
967
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000968 default:
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000969 return converterr("impossible<bad format char>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000970
971 }
972
973 *p_format = format;
974 return NULL;
975}
Guido van Rossumaa354651996-08-19 19:32:04 +0000976
Fred Drake563dfc22001-10-23 14:41:08 +0000977static int
978convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000979{
980 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
981 int count;
982 if (pb == NULL ||
983 pb->bf_getreadbuffer == NULL ||
984 pb->bf_getsegcount == NULL) {
985 *errmsg = "string or read-only buffer";
986 return -1;
987 }
988 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
989 *errmsg = "string or single-segment read-only buffer";
990 return -1;
991 }
992 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
993 *errmsg = "(unspecified)";
994 }
995 return count;
996}
Guido van Rossumaa354651996-08-19 19:32:04 +0000997
998/* Support for keyword arguments donated by
999 Geoff Philbrick <philbric@delphi.hks.com> */
1000
Tim Petersf8cd3e82001-10-27 04:26:57 +00001001/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001002int
1003PyArg_ParseTupleAndKeywords(PyObject *args,
1004 PyObject *keywords,
1005 char *format,
1006 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001007{
1008 int retval;
1009 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001010
1011 if ((args == NULL || !PyTuple_Check(args)) ||
1012 (keywords != NULL && !PyDict_Check(keywords)) ||
1013 format == NULL ||
1014 kwlist == NULL)
1015 {
1016 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001017 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001018 }
1019
Guido van Rossumaa354651996-08-19 19:32:04 +00001020 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001021 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
1022 va_end(va);
1023 return retval;
1024}
1025
1026
1027static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001028vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
1029 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001030{
Tim Petersdc5eff92001-10-27 06:53:00 +00001031 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001032 int levels[32];
Tim Petersf8cd3e82001-10-27 04:26:57 +00001033 char *fname, *message;
1034 int min, max;
Tim Peters6fb26352001-10-27 04:38:11 +00001035 char *formatsave;
Tim Petersb639d492001-10-27 07:00:56 +00001036 int i, len, nargs, nkeywords;
Tim Petersc2f01122001-10-27 07:25:06 +00001037 char *msg, **p;
Tim Petersf4331c12001-10-27 00:17:34 +00001038
Tim Peters45772cd2001-10-27 03:58:40 +00001039 assert(args != NULL && PyTuple_Check(args));
1040 assert(keywords == NULL || PyDict_Check(keywords));
1041 assert(format != NULL);
1042 assert(kwlist != NULL);
1043 assert(p_va != NULL);
1044
Tim Petersf8cd3e82001-10-27 04:26:57 +00001045 /* Search the format:
1046 message <- error msg, if any (else NULL).
1047 name <- routine name, if any (else NULL).
1048 min <- # of required arguments, or -1 if all are required.
1049 max <- most arguments (required + optional).
Tim Peters62d48e12001-10-27 06:42:16 +00001050 Check that kwlist has a non-NULL entry for each arg.
Tim Petersf8cd3e82001-10-27 04:26:57 +00001051 Raise error if a tuple arg spec is found.
1052 */
1053 fname = message = NULL;
Tim Peters6fb26352001-10-27 04:38:11 +00001054 formatsave = format;
Tim Peters62d48e12001-10-27 06:42:16 +00001055 p = kwlist;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001056 min = -1;
1057 max = 0;
1058 while ((i = *format++) != '\0') {
Tim Peters62d48e12001-10-27 06:42:16 +00001059 if (isalpha(i) && i != 'e') {
Tim Petersf8cd3e82001-10-27 04:26:57 +00001060 max++;
Tim Peters62d48e12001-10-27 06:42:16 +00001061 if (*p == NULL) {
1062 /* kwlist is too short */
1063 PyErr_BadInternalCall();
1064 return 0;
1065 }
1066 p++;
1067 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001068 else if (i == '|')
1069 min = max;
1070 else if (i == ':') {
1071 fname = format;
1072 break;
1073 }
1074 else if (i == ';') {
1075 message = format;
1076 break;
1077 }
1078 else if (i == '(') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001079 PyErr_SetString(PyExc_SystemError,
1080 "tuple found in format when using keyword arguments");
1081 return 0;
1082 }
Tim Peters62d48e12001-10-27 06:42:16 +00001083 }
1084 format = formatsave;
1085 if (*p != NULL) {
1086 /* kwlist is too long */
1087 PyErr_BadInternalCall();
1088 return 0;
1089 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001090 if (min < 0) {
1091 /* All arguments are required. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001092 min = max;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001093 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001094
Tim Peters6fb26352001-10-27 04:38:11 +00001095 nargs = PyTuple_GET_SIZE(args);
Tim Petersb0872fc2001-10-27 04:45:34 +00001096 nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001097
Guido van Rossumaa354651996-08-19 19:32:04 +00001098 /* make sure there are no duplicate values for an argument;
1099 its not clear when to use the term "keyword argument vs.
1100 keyword parameter in messages */
Tim Petersb054be42001-10-27 05:07:41 +00001101 if (nkeywords > 0) {
Tim Peters6fb26352001-10-27 04:38:11 +00001102 for (i = 0; i < nargs; i++) {
Tim Petersa9f47392001-10-27 00:46:09 +00001103 char *thiskw = kwlist[i];
1104 if (thiskw == NULL)
1105 break;
Tim Peters077f5742001-10-27 05:50:39 +00001106 if (PyDict_GetItemString(keywords, thiskw)) {
Tim Petersb054be42001-10-27 05:07:41 +00001107 PyErr_Format(PyExc_TypeError,
1108 "keyword parameter '%s' was given "
1109 "by position and by name",
Tim Petersa9f47392001-10-27 00:46:09 +00001110 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001111 return 0;
1112 }
Tim Peters0af49162001-10-27 06:14:32 +00001113 else if (PyErr_Occurred())
1114 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001115 }
1116 }
Tim Peters61dde632001-10-27 05:30:17 +00001117
Guido van Rossumaa354651996-08-19 19:32:04 +00001118 /* required arguments missing from args can be supplied by keyword
Tim Peters62d48e12001-10-27 06:42:16 +00001119 arguments; set len to the number of posiitional arguments, and,
1120 if that's less than the minimum required, add in the number of
1121 required arguments that are supplied by keywords */
Tim Peters6fb26352001-10-27 04:38:11 +00001122 len = nargs;
Tim Peters62d48e12001-10-27 06:42:16 +00001123 if (nkeywords > 0 && nargs < min) {
Tim Peters6fb26352001-10-27 04:38:11 +00001124 for (i = nargs; i < min; i++) {
Tim Peters077f5742001-10-27 05:50:39 +00001125 if (PyDict_GetItemString(keywords, kwlist[i]))
Guido van Rossumaa354651996-08-19 19:32:04 +00001126 len++;
Tim Peters0af49162001-10-27 06:14:32 +00001127 else if (PyErr_Occurred())
1128 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001129 }
1130 }
Tim Peters0af49162001-10-27 06:14:32 +00001131
Guido van Rossumaa354651996-08-19 19:32:04 +00001132 /* make sure we got an acceptable number of arguments; the message
1133 is a little confusing with keywords since keyword arguments
1134 which are supplied, but don't match the required arguments
1135 are not included in the "%d given" part of the message */
Guido van Rossumaa354651996-08-19 19:32:04 +00001136 if (len < min || max < len) {
1137 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +00001138 PyOS_snprintf(msgbuf, sizeof(msgbuf),
1139 "%.200s%s takes %s %d argument%s "
1140 "(%d given)",
1141 fname==NULL ? "function" : fname,
1142 fname==NULL ? "" : "()",
1143 min==max ? "exactly"
1144 : len < min ? "at least" : "at most",
1145 len < min ? min : max,
1146 (len < min ? min : max) == 1 ? "" : "s",
1147 len);
Guido van Rossumaa354651996-08-19 19:32:04 +00001148 message = msgbuf;
1149 }
1150 PyErr_SetString(PyExc_TypeError, message);
1151 return 0;
1152 }
Tim Petersc2f01122001-10-27 07:25:06 +00001153
1154 /* convert the positional arguments */
Tim Peters6fb26352001-10-27 04:38:11 +00001155 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001156 if (*format == '|')
1157 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001158 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001159 levels, msgbuf);
1160 if (msg) {
1161 seterror(i+1, msg, levels, fname, message);
1162 return 0;
1163 }
1164 }
1165
Tim Petersc2f01122001-10-27 07:25:06 +00001166 /* handle no keyword parameters in call */
Tim Petersb054be42001-10-27 05:07:41 +00001167 if (nkeywords == 0)
Tim Peters28bf7a92001-10-27 04:33:41 +00001168 return 1;
Tim Petersb054be42001-10-27 05:07:41 +00001169
Guido van Rossumaa354651996-08-19 19:32:04 +00001170 /* convert the keyword arguments; this uses the format
1171 string where it was left after processing args */
Tim Petersb639d492001-10-27 07:00:56 +00001172 for (i = nargs; i < max; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001173 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001174 if (*format == '|')
1175 format++;
Tim Peters077f5742001-10-27 05:50:39 +00001176 item = PyDict_GetItemString(keywords, kwlist[i]);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001177 if (item != NULL) {
Tim Peters077f5742001-10-27 05:50:39 +00001178 Py_INCREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001179 msg = convertitem(item, &format, p_va, levels, msgbuf);
Tim Peters077f5742001-10-27 05:50:39 +00001180 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001181 if (msg) {
1182 seterror(i+1, msg, levels, fname, message);
1183 return 0;
1184 }
Tim Petersc2f01122001-10-27 07:25:06 +00001185 --nkeywords;
1186 if (nkeywords == 0)
1187 break;
Guido van Rossumaa354651996-08-19 19:32:04 +00001188 }
Tim Peters0af49162001-10-27 06:14:32 +00001189 else if (PyErr_Occurred())
1190 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001191 else {
Guido van Rossumaa354651996-08-19 19:32:04 +00001192 msg = skipitem(&format, p_va);
1193 if (msg) {
1194 seterror(i+1, msg, levels, fname, message);
1195 return 0;
1196 }
1197 }
1198 }
Tim Petersb054be42001-10-27 05:07:41 +00001199
Guido van Rossumaa354651996-08-19 19:32:04 +00001200 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001201 if (nkeywords > 0) {
1202 PyObject *key, *value;
1203 int pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001204 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001205 int match = 0;
1206 char *ks = PyString_AsString(key);
Tim Petersb639d492001-10-27 07:00:56 +00001207 for (i = 0; i < max; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001208 if (!strcmp(ks, kwlist[i])) {
1209 match = 1;
1210 break;
1211 }
1212 }
1213 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001214 PyErr_Format(PyExc_TypeError,
1215 "'%s' is an invalid keyword "
1216 "argument for this function",
1217 ks);
Guido van Rossumaa354651996-08-19 19:32:04 +00001218 return 0;
1219 }
1220 }
1221 }
Tim Petersc2f01122001-10-27 07:25:06 +00001222
Guido van Rossumaa354651996-08-19 19:32:04 +00001223 return 1;
1224}
1225
1226
1227static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001228skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001229{
1230 char *format = *p_format;
1231 char c = *format++;
1232
1233 switch (c) {
1234
1235 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001236 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001237 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001238 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001239 break;
1240 }
1241
1242 case 'h': /* short int */
1243 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001244 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001245 break;
1246 }
1247
Jack Jansencc22fbe2000-08-05 21:29:58 +00001248 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001249 {
1250 (void) va_arg(*p_va, unsigned short *);
1251 break;
1252 }
1253
Guido van Rossumaa354651996-08-19 19:32:04 +00001254 case 'i': /* int */
1255 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001256 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001257 break;
1258 }
1259
1260 case 'l': /* long int */
1261 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001262 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001263 break;
1264 }
1265
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001266#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001267 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001268 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001269 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001270 break;
1271 }
1272#endif
1273
Guido van Rossumaa354651996-08-19 19:32:04 +00001274 case 'f': /* float */
1275 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001276 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001277 break;
1278 }
1279
1280 case 'd': /* double */
1281 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001282 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001283 break;
1284 }
1285
1286#ifndef WITHOUT_COMPLEX
1287 case 'D': /* complex double */
1288 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001289 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001290 break;
1291 }
1292#endif /* WITHOUT_COMPLEX */
1293
1294 case 'c': /* char */
1295 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001296 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001297 break;
1298 }
1299
1300 case 's': /* string */
1301 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001302 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001303 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001304 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001305 format++;
1306 }
1307 break;
1308 }
1309
1310 case 'z': /* string */
1311 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001312 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001313 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001314 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001315 format++;
1316 }
1317 break;
1318 }
1319
1320 case 'S': /* string object */
1321 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001322 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001323 break;
1324 }
1325
1326 case 'O': /* object */
1327 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001328 if (*format == '!') {
1329 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001330 (void) va_arg(*p_va, PyTypeObject*);
1331 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001332 }
1333#if 0
1334/* I don't know what this is for */
1335 else if (*format == '?') {
1336 inquiry pred = va_arg(*p_va, inquiry);
1337 format++;
1338 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001340 }
1341 }
1342#endif
1343 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001344 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001345 (void) va_arg(*p_va, converter);
1346 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001347 format++;
1348 }
1349 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001350 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001351 }
1352 break;
1353 }
1354
1355 default:
1356 return "impossible<bad format char>";
1357
1358 }
1359
1360 *p_format = format;
1361 return NULL;
1362}
Fred Drakee4616e62001-10-23 21:09:29 +00001363
1364
1365int
1366PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
1367{
1368 int i, l;
1369 PyObject **o;
1370 va_list vargs;
1371
1372#ifdef HAVE_STDARG_PROTOTYPES
1373 va_start(vargs, max);
1374#else
1375 va_start(vargs);
1376#endif
1377
1378 assert(min >= 0);
1379 assert(min <= max);
1380 if (!PyTuple_Check(args)) {
1381 PyErr_SetString(PyExc_SystemError,
1382 "PyArg_UnpackTuple() argument list is not a tuple");
1383 return 0;
1384 }
1385 l = PyTuple_GET_SIZE(args);
1386 if (l < min) {
1387 if (name != NULL)
1388 PyErr_Format(
1389 PyExc_TypeError,
1390 "%s expected %s%d arguments, got %d",
1391 name, (min == max ? "" : "at least "), min, l);
1392 else
1393 PyErr_Format(
1394 PyExc_TypeError,
1395 "unpacked tuple should have %s%d elements,"
1396 " but has %d",
1397 (min == max ? "" : "at least "), min, l);
1398 va_end(vargs);
1399 return 0;
1400 }
1401 if (l > max) {
1402 if (name != NULL)
1403 PyErr_Format(
1404 PyExc_TypeError,
1405 "%s expected %s%d arguments, got %d",
1406 name, (min == max ? "" : "at most "), max, l);
1407 else
1408 PyErr_Format(
1409 PyExc_TypeError,
1410 "unpacked tuple should have %s%d elements,"
1411 " but has %d",
1412 (min == max ? "" : "at most "), max, l);
1413 va_end(vargs);
1414 return 0;
1415 }
1416 for (i = 0; i < l; i++) {
1417 o = va_arg(vargs, PyObject **);
1418 *o = PyTuple_GET_ITEM(args, i);
1419 }
1420 va_end(vargs);
1421 return 1;
1422}