blob: c80ca58fddba4509c72c8880f12743e4dfd9b2bf [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;
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000138 sprintf(msgbuf, "%.200s%s takes no arguments",
Ka-Ping Yee20579702001-01-15 22:14:16 +0000139 fname==NULL ? "function" : fname,
140 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000141 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000142 return 0;
143 }
144 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000145 if (args == NULL) {
146 sprintf(msgbuf,
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000147 "%.200s%s takes at least one argument",
Ka-Ping Yee20579702001-01-15 22:14:16 +0000148 fname==NULL ? "function" : fname,
149 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000150 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000151 return 0;
152 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000153 msg = convertitem(args, &format, p_va, levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000154 if (msg == NULL)
155 return 1;
156 seterror(levels[0], msg, levels+1, fname, message);
157 return 0;
158 }
159 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000160 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000161 "old style getargs format uses new features");
162 return 0;
163 }
164 }
165
Guido van Rossum79f25d91997-04-29 20:08:16 +0000166 if (!PyTuple_Check(args)) {
167 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000168 "new style getargs format but argument is not a tuple");
169 return 0;
170 }
171
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000172 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000173
174 if (len < min || max < len) {
175 if (message == NULL) {
176 sprintf(msgbuf,
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000177 "%.150s%s takes %s %d argument%s (%d given)",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000178 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000179 fname==NULL ? "" : "()",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000180 min==max ? "exactly"
181 : len < min ? "at least" : "at most",
182 len < min ? min : max,
183 (len < min ? min : max) == 1 ? "" : "s",
184 len);
185 message = msgbuf;
186 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000187 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000188 return 0;
189 }
190
191 for (i = 0; i < len; i++) {
192 if (*format == '|')
193 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000194 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
195 levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000196 if (msg) {
197 seterror(i+1, msg, levels, fname, message);
198 return 0;
199 }
200 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000201
Guido van Rossum730806d1998-04-10 22:27:42 +0000202 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000203 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000204 *format != '|' && *format != ':' && *format != ';') {
205 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000206 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000207 return 0;
208 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000209
210 return 1;
211}
212
213
214
215static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000216seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000217{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000218 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000219 int i;
220 char *p = buf;
221
Guido van Rossum79f25d91997-04-29 20:08:16 +0000222 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000223 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000224 else if (message == NULL) {
225 if (fname != NULL) {
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000226 sprintf(p, "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000227 p += strlen(p);
228 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000229 if (iarg != 0) {
230 sprintf(p, "argument %d", iarg);
231 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000232 p += strlen(p);
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000233 while (levels[i] > 0 && (int)(p-buf) < 220) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000234 sprintf(p, ", item %d", levels[i]-1);
235 p += strlen(p);
236 i++;
237 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000238 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000239 else {
240 sprintf(p, "argument");
241 p += strlen(p);
242 }
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000243 sprintf(p, " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000244 message = buf;
245 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000247}
248
249
250/* Convert a tuple argument.
251 On entry, *p_format points to the character _after_ the opening '('.
252 On successful exit, *p_format points to the closing ')'.
253 If successful:
254 *p_format and *p_va are updated,
255 *levels and *msgbuf are untouched,
256 and NULL is returned.
257 If the argument is invalid:
258 *p_format is unchanged,
259 *p_va is undefined,
260 *levels is a 0-terminated list of item numbers,
261 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000262 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000263 <typename1> is the name of the expected type, and
264 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000265 and msgbuf is returned.
266*/
267
268static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
270 char *msgbuf, int toplevel)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000271{
272 int level = 0;
273 int n = 0;
274 char *format = *p_format;
275 int i;
276
277 for (;;) {
278 int c = *format++;
279 if (c == '(') {
280 if (level == 0)
281 n++;
282 level++;
283 }
284 else if (c == ')') {
285 if (level == 0)
286 break;
287 level--;
288 }
289 else if (c == ':' || c == ';' || c == '\0')
290 break;
291 else if (level == 0 && isalpha(c))
292 n++;
293 }
294
Ka-Ping Yee20579702001-01-15 22:14:16 +0000295 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000296 levels[0] = 0;
297 sprintf(msgbuf,
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000298 toplevel ? "expected %d arguments, not %.50s" :
299 "must be %d-item sequence, not %.50s",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000300 n, arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000301 return msgbuf;
302 }
303
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000304 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000305 levels[0] = 0;
306 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000307 toplevel ? "expected %d arguments, not %d" :
308 "must be sequence of length %d, not %d",
309 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000310 return msgbuf;
311 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000312
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000313 format = *p_format;
314 for (i = 0; i < n; i++) {
315 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000316 PyObject *item;
317 item = PySequence_GetItem(arg, i);
318 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
319 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
320 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000321 if (msg != NULL) {
322 levels[0] = i+1;
323 return msg;
324 }
325 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000326
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000327 *p_format = format;
328 return NULL;
329}
330
331
332/* Convert a single item. */
333
334static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
336 char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000337{
338 char *msg;
339 char *format = *p_format;
340
341 if (*format == '(' /* ')' */) {
342 format++;
343 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
344 if (msg == NULL)
345 format++;
346 }
347 else {
348 msg = convertsimple(arg, &format, p_va, msgbuf);
349 if (msg != NULL)
350 levels[0] = 0;
351 }
352 if (msg == NULL)
353 *p_format = format;
354 return msg;
355}
356
357
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000358
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000359#define UNICODE_DEFAULT_ENCODING(arg) \
360 _PyUnicode_AsDefaultEncodedString(arg, NULL)
361
362/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000363
364static char *
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000365converterr(char *expected, PyObject *arg, char *msgbuf)
366{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000367 assert(expected != NULL);
368 assert(arg != NULL);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000369 sprintf(msgbuf, "must be %.50s, not %.50s", expected,
370 arg == Py_None ? "None" : arg->ob_type->tp_name);
371 return msgbuf;
372}
373
374#define CONV_UNICODE "(unicode conversion error)"
375
376/* Convert a non-tuple argument. Return NULL if conversion went OK,
377 or a string with a message describing the failure. The message is
378 formatted as "must be <desired type>, not <actual type>".
379 When failing, an exception may or may not have been raised.
380 Don't call if a tuple is expected.
381*/
382
383static char *
384convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000385{
386 char *format = *p_format;
387 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000388 PyObject *uarg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000389
390 switch (c) {
391
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000392 case 'b': { /* unsigned byte -- very short int */
393 char *p = va_arg(*p_va, char *);
394 long ival = PyInt_AsLong(arg);
395 if (ival == -1 && PyErr_Occurred())
396 return converterr("integer<b>", arg, msgbuf);
397 else if (ival < 0) {
398 PyErr_SetString(PyExc_OverflowError,
399 "unsigned byte integer is less than minimum");
400 return converterr("integer<b>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000401 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000402 else if (ival > UCHAR_MAX) {
403 PyErr_SetString(PyExc_OverflowError,
404 "unsigned byte integer is greater than maximum");
405 return converterr("integer<b>", arg, msgbuf);
406 }
407 else
408 *p = (unsigned char) ival;
409 break;
410 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000411
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000412 case 'B': {/* byte sized bitfield - both signed and unsigned
413 values allowed */
414 char *p = va_arg(*p_va, char *);
415 long ival = PyInt_AsLong(arg);
416 if (ival == -1 && PyErr_Occurred())
417 return converterr("integer<b>", arg, msgbuf);
418 else if (ival < SCHAR_MIN) {
419 PyErr_SetString(PyExc_OverflowError,
420 "byte-sized integer bitfield is less than minimum");
421 return converterr("integer<B>", arg, msgbuf);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000422 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000423 else if (ival > (int)UCHAR_MAX) {
424 PyErr_SetString(PyExc_OverflowError,
425 "byte-sized integer bitfield is greater than maximum");
426 return converterr("integer<B>", arg, msgbuf);
427 }
428 else
429 *p = (unsigned char) ival;
430 break;
431 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000432
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000433 case 'h': {/* signed short int */
434 short *p = va_arg(*p_va, short *);
435 long ival = PyInt_AsLong(arg);
436 if (ival == -1 && PyErr_Occurred())
437 return converterr("integer<h>", arg, msgbuf);
438 else if (ival < SHRT_MIN) {
439 PyErr_SetString(PyExc_OverflowError,
440 "signed short integer is less than minimum");
441 return converterr("integer<h>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000442 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000443 else if (ival > SHRT_MAX) {
444 PyErr_SetString(PyExc_OverflowError,
445 "signed short integer is greater than maximum");
446 return converterr("integer<h>", arg, msgbuf);
447 }
448 else
449 *p = (short) ival;
450 break;
451 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000452
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000453 case 'H': { /* short int sized bitfield, both signed and
454 unsigned allowed */
455 unsigned short *p = va_arg(*p_va, unsigned short *);
456 long ival = PyInt_AsLong(arg);
457 if (ival == -1 && PyErr_Occurred())
458 return converterr("integer<H>", arg, msgbuf);
459 else if (ival < SHRT_MIN) {
460 PyErr_SetString(PyExc_OverflowError,
461 "short integer bitfield is less than minimum");
462 return converterr("integer<H>", arg, msgbuf);
Jack Jansend50338f2000-07-06 12:22:00 +0000463 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000464 else if (ival > USHRT_MAX) {
465 PyErr_SetString(PyExc_OverflowError,
466 "short integer bitfield is greater than maximum");
467 return converterr("integer<H>", arg, msgbuf);
468 }
469 else
470 *p = (unsigned short) ival;
471 break;
472 }
Jack Jansend50338f2000-07-06 12:22:00 +0000473
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000474 case 'i': {/* signed int */
475 int *p = va_arg(*p_va, int *);
476 long ival = PyInt_AsLong(arg);
477 if (ival == -1 && PyErr_Occurred())
478 return converterr("integer<i>", arg, msgbuf);
479 else if (ival > INT_MAX) {
480 PyErr_SetString(PyExc_OverflowError,
481 "signed integer is greater than maximum");
482 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000483 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000484 else if (ival < INT_MIN) {
485 PyErr_SetString(PyExc_OverflowError,
486 "signed integer is less than minimum");
487 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000488 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000489 else
490 *p = ival;
491 break;
492 }
493
494 case 'l': {/* long int */
495 long *p = va_arg(*p_va, long *);
496 long ival = PyInt_AsLong(arg);
497 if (ival == -1 && PyErr_Occurred())
498 return converterr("integer<l>", arg, msgbuf);
499 else
500 *p = ival;
501 break;
502 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000503
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000504#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000505 case 'L': {/* LONG_LONG */
506 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
507 LONG_LONG ival = PyLong_AsLongLong( arg );
508 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
509 return converterr("long<L>", arg, msgbuf);
510 } else {
511 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000512 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000513 break;
514 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000515#endif
516
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000517 case 'f': {/* float */
518 float *p = va_arg(*p_va, float *);
519 double dval = PyFloat_AsDouble(arg);
520 if (PyErr_Occurred())
521 return converterr("float<f>", arg, msgbuf);
522 else
523 *p = (float) dval;
524 break;
525 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000526
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000527 case 'd': {/* double */
528 double *p = va_arg(*p_va, double *);
529 double dval = PyFloat_AsDouble(arg);
530 if (PyErr_Occurred())
531 return converterr("float<d>", arg, msgbuf);
532 else
533 *p = dval;
534 break;
535 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000536
Guido van Rossum530956d1996-07-21 02:27:43 +0000537#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000538 case 'D': {/* complex double */
539 Py_complex *p = va_arg(*p_va, Py_complex *);
540 Py_complex cval;
541 cval = PyComplex_AsCComplex(arg);
542 if (PyErr_Occurred())
543 return converterr("complex<D>", arg, msgbuf);
544 else
545 *p = cval;
546 break;
547 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000548#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000549
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000550 case 'c': {/* char */
551 char *p = va_arg(*p_va, char *);
552 if (PyString_Check(arg) && PyString_Size(arg) == 1)
Jeremy Hylton0407aea2001-10-10 02:51:57 +0000553 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000554 else
555 return converterr("char", arg, msgbuf);
556 break;
557 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000558
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000559 case 's': {/* string */
560 if (*format == '#') {
561 void **p = (void **)va_arg(*p_va, char **);
562 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000563
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000564 if (PyString_Check(arg)) {
565 *p = PyString_AS_STRING(arg);
566 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000567 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000568#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000569 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000570 uarg = UNICODE_DEFAULT_ENCODING(arg);
571 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000572 return converterr(CONV_UNICODE,
573 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000574 *p = PyString_AS_STRING(uarg);
575 *q = PyString_GET_SIZE(uarg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000576 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000577#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000578 else { /* any buffer-like object */
579 char *buf;
580 int count = convertbuffer(arg, p, &buf);
581 if (count < 0)
582 return converterr(buf, arg, msgbuf);
583 *q = count;
584 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000585 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000586 } else {
587 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000588
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000589 if (PyString_Check(arg))
590 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000591#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000592 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000593 uarg = UNICODE_DEFAULT_ENCODING(arg);
594 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000595 return converterr(CONV_UNICODE,
596 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000597 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000598 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000599#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000600 else
601 return converterr("string", arg, msgbuf);
602 if ((int)strlen(*p) != PyString_Size(arg))
603 return converterr("string without null bytes",
604 arg, msgbuf);
605 }
606 break;
607 }
608
609 case 'z': {/* string, may be NULL (None) */
610 if (*format == '#') { /* any buffer-like object */
611 void **p = (void **)va_arg(*p_va, char **);
612 int *q = va_arg(*p_va, int *);
613
614 if (arg == Py_None) {
615 *p = 0;
616 *q = 0;
617 }
618 else if (PyString_Check(arg)) {
619 *p = PyString_AS_STRING(arg);
620 *q = PyString_GET_SIZE(arg);
621 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000622#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000623 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000624 uarg = UNICODE_DEFAULT_ENCODING(arg);
625 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000626 return converterr(CONV_UNICODE,
627 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000628 *p = PyString_AS_STRING(uarg);
629 *q = PyString_GET_SIZE(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000630 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000631#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000632 else { /* any buffer-like object */
633 char *buf;
634 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000635 if (count < 0)
636 return converterr(buf, arg, msgbuf);
637 *q = count;
638 }
639 format++;
640 } else {
641 char **p = va_arg(*p_va, char **);
642
643 if (arg == Py_None)
644 *p = 0;
645 else if (PyString_Check(arg))
Jeremy Hyltona4c8cd72001-10-10 02:51:08 +0000646 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000647#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000648 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000649 uarg = UNICODE_DEFAULT_ENCODING(arg);
650 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000651 return converterr(CONV_UNICODE,
652 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000653 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000654 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000655#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000656 else
657 return converterr("string or None",
658 arg, msgbuf);
659 if (*format == '#') {
660 int *q = va_arg(*p_va, int *);
661 if (arg == Py_None)
662 *q = 0;
663 else
664 *q = PyString_Size(arg);
665 format++;
666 }
667 else if (*p != NULL &&
668 (int)strlen(*p) != PyString_Size(arg))
669 return converterr(
670 "string without null bytes or None",
671 arg, msgbuf);
672 }
673 break;
674 }
675
676 case 'e': {/* encoded string */
677 char **buffer;
678 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000679 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000680 int size, recode_strings;
681
682 /* Get 'e' parameter: the encoding name */
683 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000684#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000685 if (encoding == NULL)
686 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000687#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000688
689 /* Get output buffer parameter:
690 's' (recode all objects via Unicode) or
691 't' (only recode non-string objects)
692 */
693 if (*format == 's')
694 recode_strings = 1;
695 else if (*format == 't')
696 recode_strings = 0;
697 else
698 return converterr(
699 "(unknown parser marker combination)",
700 arg, msgbuf);
701 buffer = (char **)va_arg(*p_va, char **);
702 format++;
703 if (buffer == NULL)
704 return converterr("(buffer is NULL)",
705 arg, msgbuf);
706
707 /* Encode object */
708 if (!recode_strings && PyString_Check(arg)) {
709 s = arg;
710 Py_INCREF(s);
711 }
712 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000713#ifdef Py_USING_UNICODE
714 PyObject *u;
715
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000716 /* Convert object to Unicode */
717 u = PyUnicode_FromObject(arg);
718 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000719 return converterr(
720 "string or unicode or text buffer",
721 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000722
723 /* Encode object; use default error handling */
724 s = PyUnicode_AsEncodedString(u,
725 encoding,
726 NULL);
727 Py_DECREF(u);
728 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000729 return converterr("(encoding failed)",
730 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000731 if (!PyString_Check(s)) {
732 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000733 return converterr(
734 "(encoder failed to return a string)",
735 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000736 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000737#else
738 return converterr("string<e>", arg, msgbuf);
739#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000740 }
741 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000742
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000743 /* Write output; output is guaranteed to be 0-terminated */
744 if (*format == '#') {
745 /* Using buffer length parameter '#':
746
747 - if *buffer is NULL, a new buffer of the
748 needed size is allocated and the data
749 copied into it; *buffer is updated to point
750 to the new buffer; the caller is
751 responsible for PyMem_Free()ing it after
752 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000753
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000754 - if *buffer is not NULL, the data is
755 copied to *buffer; *buffer_len has to be
756 set to the size of the buffer on input;
757 buffer overflow is signalled with an error;
758 buffer has to provide enough room for the
759 encoded string plus the trailing 0-byte
760
761 - in both cases, *buffer_len is updated to
762 the size of the buffer /excluding/ the
763 trailing 0-byte
764
765 */
766 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000767
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000768 format++;
769 if (buffer_len == NULL)
770 return converterr(
771 "(buffer_len is NULL)",
772 arg, msgbuf);
773 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000774 *buffer = PyMem_NEW(char, size + 1);
775 if (*buffer == NULL) {
776 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000777 return converterr(
778 "(memory error)",
779 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000780 }
Fred Drake25871c02000-05-03 15:17:02 +0000781 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000782 if (size + 1 > *buffer_len) {
783 Py_DECREF(s);
784 return converterr(
785 "(buffer overflow)",
786 arg, msgbuf);
787 }
Fred Drake25871c02000-05-03 15:17:02 +0000788 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000789 memcpy(*buffer,
790 PyString_AS_STRING(s),
791 size + 1);
792 *buffer_len = size;
793 } else {
794 /* Using a 0-terminated buffer:
795
796 - the encoded string has to be 0-terminated
797 for this variant to work; if it is not, an
798 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000799
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000800 - a new buffer of the needed size is
801 allocated and the data copied into it;
802 *buffer is updated to point to the new
803 buffer; the caller is responsible for
804 PyMem_Free()ing it after usage
805
806 */
807 if ((int)strlen(PyString_AS_STRING(s)) != size)
808 return converterr(
809 "(encoded string without NULL bytes)",
810 arg, msgbuf);
811 *buffer = PyMem_NEW(char, size + 1);
812 if (*buffer == NULL) {
813 Py_DECREF(s);
814 return converterr("(memory error)",
815 arg, msgbuf);
816 }
817 memcpy(*buffer,
818 PyString_AS_STRING(s),
819 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000820 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000821 Py_DECREF(s);
822 break;
823 }
824
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000825#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000826 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
827 if (*format == '#') { /* any buffer-like object */
828 void **p = (void **)va_arg(*p_va, char **);
829 int *q = va_arg(*p_va, int *);
830 char *buf;
831 int count = convertbuffer(arg, p, &buf);
832
833 if (count < 0)
834 return converterr(buf, arg, msgbuf);
835 *q = count/(sizeof(Py_UNICODE));
836 format++;
837 } else {
838 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
839
Guido van Rossume826ef02000-03-10 23:02:17 +0000840 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000841 *p = PyUnicode_AS_UNICODE(arg);
842 else
843 return converterr("unicode", arg, msgbuf);
844 }
845 break;
846 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000847#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000848
849 case 'S': { /* string object */
850 PyObject **p = va_arg(*p_va, PyObject **);
851 if (PyString_Check(arg))
852 *p = arg;
853 else
854 return converterr("string", arg, msgbuf);
855 break;
856 }
857
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000858#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000859 case 'U': { /* Unicode object */
860 PyObject **p = va_arg(*p_va, PyObject **);
861 if (PyUnicode_Check(arg))
862 *p = arg;
863 else
864 return converterr("unicode", arg, msgbuf);
865 break;
866 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000867#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000868
869 case 'O': { /* object */
870 PyTypeObject *type;
871 PyObject **p;
872 if (*format == '!') {
873 type = va_arg(*p_va, PyTypeObject*);
874 p = va_arg(*p_va, PyObject **);
875 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +0000876 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +0000877 *p = arg;
878 else
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000879 return converterr(type->tp_name, arg, msgbuf);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000880
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000881 }
882 else if (*format == '?') {
883 inquiry pred = va_arg(*p_va, inquiry);
884 p = va_arg(*p_va, PyObject **);
885 format++;
886 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000887 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000888 else
889 return converterr("(unspecified)",
890 arg, msgbuf);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000891
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000892 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000893 else if (*format == '&') {
894 typedef int (*converter)(PyObject *, void *);
895 converter convert = va_arg(*p_va, converter);
896 void *addr = va_arg(*p_va, void *);
897 format++;
898 if (! (*convert)(arg, addr))
899 return converterr("(unspecified)",
900 arg, msgbuf);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000901 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000902 else {
903 p = va_arg(*p_va, PyObject **);
904 *p = arg;
905 }
906 break;
907 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000908
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000909
910 case 'w': { /* memory buffer, read-write access */
911 void **p = va_arg(*p_va, void **);
912 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
913 int count;
914
915 if (pb == NULL ||
916 pb->bf_getwritebuffer == NULL ||
917 pb->bf_getsegcount == NULL)
918 return converterr("read-write buffer", arg, msgbuf);
919 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
920 return converterr("single-segment read-write buffer",
921 arg, msgbuf);
922 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
923 return converterr("(unspecified)", arg, msgbuf);
924 if (*format == '#') {
925 int *q = va_arg(*p_va, int *);
926
927 *q = count;
928 format++;
929 }
930 break;
931 }
932
933 case 't': { /* 8-bit character buffer, read-only access */
934 const char **p = va_arg(*p_va, const char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +0000935 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000936 int count;
937
938 if (*format++ != '#')
939 return converterr(
940 "invalid use of 't' format character",
941 arg, msgbuf);
942 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +0000943 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
944 pb == NULL || pb->bf_getcharbuffer == NULL ||
945 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000946 return converterr(
947 "string or read-only character buffer",
948 arg, msgbuf);
949
Jeremy Hylton4819e972001-10-11 14:40:37 +0000950 if (pb->bf_getsegcount(arg, NULL) != 1)
951 return converterr(
952 "string or single-segment read-only buffer",
953 arg, msgbuf);
954
955 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000956 if (count < 0)
Jeremy Hylton4819e972001-10-11 14:40:37 +0000957 return converterr("(unspecified)", arg, msgbuf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000958 *va_arg(*p_va, int *) = count;
959 break;
960 }
961
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000962 default:
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000963 return converterr("impossible<bad format char>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000964
965 }
966
967 *p_format = format;
968 return NULL;
969}
Guido van Rossumaa354651996-08-19 19:32:04 +0000970
Fred Drake563dfc22001-10-23 14:41:08 +0000971static int
972convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000973{
974 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
975 int count;
976 if (pb == NULL ||
977 pb->bf_getreadbuffer == NULL ||
978 pb->bf_getsegcount == NULL) {
979 *errmsg = "string or read-only buffer";
980 return -1;
981 }
982 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
983 *errmsg = "string or single-segment read-only buffer";
984 return -1;
985 }
986 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
987 *errmsg = "(unspecified)";
988 }
989 return count;
990}
Guido van Rossumaa354651996-08-19 19:32:04 +0000991
992/* Support for keyword arguments donated by
993 Geoff Philbrick <philbric@delphi.hks.com> */
994
Tim Petersf8cd3e82001-10-27 04:26:57 +0000995/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +0000996int
997PyArg_ParseTupleAndKeywords(PyObject *args,
998 PyObject *keywords,
999 char *format,
1000 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001001{
1002 int retval;
1003 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001004
1005 if ((args == NULL || !PyTuple_Check(args)) ||
1006 (keywords != NULL && !PyDict_Check(keywords)) ||
1007 format == NULL ||
1008 kwlist == NULL)
1009 {
1010 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001011 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001012 }
1013
Guido van Rossumaa354651996-08-19 19:32:04 +00001014 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001015 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
1016 va_end(va);
1017 return retval;
1018}
1019
1020
1021static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001022vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
1023 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001024{
Tim Petersdc5eff92001-10-27 06:53:00 +00001025 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001026 int levels[32];
Tim Petersf8cd3e82001-10-27 04:26:57 +00001027 char *fname, *message;
1028 int min, max;
Tim Peters6fb26352001-10-27 04:38:11 +00001029 char *formatsave;
Tim Petersb639d492001-10-27 07:00:56 +00001030 int i, len, nargs, nkeywords;
Tim Petersc2f01122001-10-27 07:25:06 +00001031 char *msg, **p;
Tim Petersf4331c12001-10-27 00:17:34 +00001032
Tim Peters45772cd2001-10-27 03:58:40 +00001033 assert(args != NULL && PyTuple_Check(args));
1034 assert(keywords == NULL || PyDict_Check(keywords));
1035 assert(format != NULL);
1036 assert(kwlist != NULL);
1037 assert(p_va != NULL);
1038
Tim Petersf8cd3e82001-10-27 04:26:57 +00001039 /* Search the format:
1040 message <- error msg, if any (else NULL).
1041 name <- routine name, if any (else NULL).
1042 min <- # of required arguments, or -1 if all are required.
1043 max <- most arguments (required + optional).
Tim Peters62d48e12001-10-27 06:42:16 +00001044 Check that kwlist has a non-NULL entry for each arg.
Tim Petersf8cd3e82001-10-27 04:26:57 +00001045 Raise error if a tuple arg spec is found.
1046 */
1047 fname = message = NULL;
Tim Peters6fb26352001-10-27 04:38:11 +00001048 formatsave = format;
Tim Peters62d48e12001-10-27 06:42:16 +00001049 p = kwlist;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001050 min = -1;
1051 max = 0;
1052 while ((i = *format++) != '\0') {
Tim Peters62d48e12001-10-27 06:42:16 +00001053 if (isalpha(i) && i != 'e') {
Tim Petersf8cd3e82001-10-27 04:26:57 +00001054 max++;
Tim Peters62d48e12001-10-27 06:42:16 +00001055 if (*p == NULL) {
1056 /* kwlist is too short */
1057 PyErr_BadInternalCall();
1058 return 0;
1059 }
1060 p++;
1061 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001062 else if (i == '|')
1063 min = max;
1064 else if (i == ':') {
1065 fname = format;
1066 break;
1067 }
1068 else if (i == ';') {
1069 message = format;
1070 break;
1071 }
1072 else if (i == '(') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001073 PyErr_SetString(PyExc_SystemError,
1074 "tuple found in format when using keyword arguments");
1075 return 0;
1076 }
Tim Peters62d48e12001-10-27 06:42:16 +00001077 }
1078 format = formatsave;
1079 if (*p != NULL) {
1080 /* kwlist is too long */
1081 PyErr_BadInternalCall();
1082 return 0;
1083 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001084 if (min < 0) {
1085 /* All arguments are required. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001086 min = max;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001087 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001088
Tim Peters6fb26352001-10-27 04:38:11 +00001089 nargs = PyTuple_GET_SIZE(args);
Tim Petersb0872fc2001-10-27 04:45:34 +00001090 nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001091
Guido van Rossumaa354651996-08-19 19:32:04 +00001092 /* make sure there are no duplicate values for an argument;
1093 its not clear when to use the term "keyword argument vs.
1094 keyword parameter in messages */
Tim Petersb054be42001-10-27 05:07:41 +00001095 if (nkeywords > 0) {
Tim Peters6fb26352001-10-27 04:38:11 +00001096 for (i = 0; i < nargs; i++) {
Tim Petersa9f47392001-10-27 00:46:09 +00001097 char *thiskw = kwlist[i];
1098 if (thiskw == NULL)
1099 break;
Tim Peters077f5742001-10-27 05:50:39 +00001100 if (PyDict_GetItemString(keywords, thiskw)) {
Tim Petersb054be42001-10-27 05:07:41 +00001101 PyErr_Format(PyExc_TypeError,
1102 "keyword parameter '%s' was given "
1103 "by position and by name",
Tim Petersa9f47392001-10-27 00:46:09 +00001104 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001105 return 0;
1106 }
Tim Peters0af49162001-10-27 06:14:32 +00001107 else if (PyErr_Occurred())
1108 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001109 }
1110 }
Tim Peters61dde632001-10-27 05:30:17 +00001111
Guido van Rossumaa354651996-08-19 19:32:04 +00001112 /* required arguments missing from args can be supplied by keyword
Tim Peters62d48e12001-10-27 06:42:16 +00001113 arguments; set len to the number of posiitional arguments, and,
1114 if that's less than the minimum required, add in the number of
1115 required arguments that are supplied by keywords */
Tim Peters6fb26352001-10-27 04:38:11 +00001116 len = nargs;
Tim Peters62d48e12001-10-27 06:42:16 +00001117 if (nkeywords > 0 && nargs < min) {
Tim Peters6fb26352001-10-27 04:38:11 +00001118 for (i = nargs; i < min; i++) {
Tim Peters077f5742001-10-27 05:50:39 +00001119 if (PyDict_GetItemString(keywords, kwlist[i]))
Guido van Rossumaa354651996-08-19 19:32:04 +00001120 len++;
Tim Peters0af49162001-10-27 06:14:32 +00001121 else if (PyErr_Occurred())
1122 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001123 }
1124 }
Tim Peters0af49162001-10-27 06:14:32 +00001125
Guido van Rossumaa354651996-08-19 19:32:04 +00001126 /* make sure we got an acceptable number of arguments; the message
1127 is a little confusing with keywords since keyword arguments
1128 which are supplied, but don't match the required arguments
1129 are not included in the "%d given" part of the message */
Guido van Rossumaa354651996-08-19 19:32:04 +00001130 if (len < min || max < len) {
1131 if (message == NULL) {
1132 sprintf(msgbuf,
Tim Petersdc5eff92001-10-27 06:53:00 +00001133 "%.200s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001134 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001135 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001136 min==max ? "exactly"
1137 : len < min ? "at least" : "at most",
1138 len < min ? min : max,
1139 (len < min ? min : max) == 1 ? "" : "s",
1140 len);
1141 message = msgbuf;
1142 }
1143 PyErr_SetString(PyExc_TypeError, message);
1144 return 0;
1145 }
Tim Petersc2f01122001-10-27 07:25:06 +00001146
1147 /* convert the positional arguments */
Tim Peters6fb26352001-10-27 04:38:11 +00001148 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001149 if (*format == '|')
1150 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001151 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001152 levels, msgbuf);
1153 if (msg) {
1154 seterror(i+1, msg, levels, fname, message);
1155 return 0;
1156 }
1157 }
1158
Tim Petersc2f01122001-10-27 07:25:06 +00001159 /* handle no keyword parameters in call */
Tim Petersb054be42001-10-27 05:07:41 +00001160 if (nkeywords == 0)
Tim Peters28bf7a92001-10-27 04:33:41 +00001161 return 1;
Tim Petersb054be42001-10-27 05:07:41 +00001162
Guido van Rossumaa354651996-08-19 19:32:04 +00001163 /* convert the keyword arguments; this uses the format
1164 string where it was left after processing args */
Tim Petersb639d492001-10-27 07:00:56 +00001165 for (i = nargs; i < max; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001166 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001167 if (*format == '|')
1168 format++;
Tim Peters077f5742001-10-27 05:50:39 +00001169 item = PyDict_GetItemString(keywords, kwlist[i]);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001170 if (item != NULL) {
Tim Peters077f5742001-10-27 05:50:39 +00001171 Py_INCREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001172 msg = convertitem(item, &format, p_va, levels, msgbuf);
Tim Peters077f5742001-10-27 05:50:39 +00001173 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001174 if (msg) {
1175 seterror(i+1, msg, levels, fname, message);
1176 return 0;
1177 }
Tim Petersc2f01122001-10-27 07:25:06 +00001178 --nkeywords;
1179 if (nkeywords == 0)
1180 break;
Guido van Rossumaa354651996-08-19 19:32:04 +00001181 }
Tim Peters0af49162001-10-27 06:14:32 +00001182 else if (PyErr_Occurred())
1183 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001184 else {
Guido van Rossumaa354651996-08-19 19:32:04 +00001185 msg = skipitem(&format, p_va);
1186 if (msg) {
1187 seterror(i+1, msg, levels, fname, message);
1188 return 0;
1189 }
1190 }
1191 }
Tim Petersb054be42001-10-27 05:07:41 +00001192
Guido van Rossumaa354651996-08-19 19:32:04 +00001193 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001194 if (nkeywords > 0) {
1195 PyObject *key, *value;
1196 int pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001197 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001198 int match = 0;
1199 char *ks = PyString_AsString(key);
Tim Petersb639d492001-10-27 07:00:56 +00001200 for (i = 0; i < max; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001201 if (!strcmp(ks, kwlist[i])) {
1202 match = 1;
1203 break;
1204 }
1205 }
1206 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001207 PyErr_Format(PyExc_TypeError,
1208 "'%s' is an invalid keyword "
1209 "argument for this function",
1210 ks);
Guido van Rossumaa354651996-08-19 19:32:04 +00001211 return 0;
1212 }
1213 }
1214 }
Tim Petersc2f01122001-10-27 07:25:06 +00001215
Guido van Rossumaa354651996-08-19 19:32:04 +00001216 return 1;
1217}
1218
1219
1220static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001221skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001222{
1223 char *format = *p_format;
1224 char c = *format++;
1225
1226 switch (c) {
1227
1228 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001229 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001230 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001231 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001232 break;
1233 }
1234
1235 case 'h': /* short int */
1236 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001237 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001238 break;
1239 }
1240
Jack Jansencc22fbe2000-08-05 21:29:58 +00001241 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001242 {
1243 (void) va_arg(*p_va, unsigned short *);
1244 break;
1245 }
1246
Guido van Rossumaa354651996-08-19 19:32:04 +00001247 case 'i': /* int */
1248 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001249 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001250 break;
1251 }
1252
1253 case 'l': /* long int */
1254 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001255 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001256 break;
1257 }
1258
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001259#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001260 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001261 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001262 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001263 break;
1264 }
1265#endif
1266
Guido van Rossumaa354651996-08-19 19:32:04 +00001267 case 'f': /* float */
1268 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001269 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001270 break;
1271 }
1272
1273 case 'd': /* double */
1274 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001275 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001276 break;
1277 }
1278
1279#ifndef WITHOUT_COMPLEX
1280 case 'D': /* complex double */
1281 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001282 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001283 break;
1284 }
1285#endif /* WITHOUT_COMPLEX */
1286
1287 case 'c': /* char */
1288 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001289 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001290 break;
1291 }
1292
1293 case 's': /* string */
1294 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001295 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001296 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001297 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001298 format++;
1299 }
1300 break;
1301 }
1302
1303 case 'z': /* string */
1304 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001305 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001306 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001307 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001308 format++;
1309 }
1310 break;
1311 }
1312
1313 case 'S': /* string object */
1314 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001315 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001316 break;
1317 }
1318
1319 case 'O': /* object */
1320 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001321 if (*format == '!') {
1322 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001323 (void) va_arg(*p_va, PyTypeObject*);
1324 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001325 }
1326#if 0
1327/* I don't know what this is for */
1328 else if (*format == '?') {
1329 inquiry pred = va_arg(*p_va, inquiry);
1330 format++;
1331 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001332 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001333 }
1334 }
1335#endif
1336 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001337 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001338 (void) va_arg(*p_va, converter);
1339 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001340 format++;
1341 }
1342 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001343 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001344 }
1345 break;
1346 }
1347
1348 default:
1349 return "impossible<bad format char>";
1350
1351 }
1352
1353 *p_format = format;
1354 return NULL;
1355}
Fred Drakee4616e62001-10-23 21:09:29 +00001356
1357
1358int
1359PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
1360{
1361 int i, l;
1362 PyObject **o;
1363 va_list vargs;
1364
1365#ifdef HAVE_STDARG_PROTOTYPES
1366 va_start(vargs, max);
1367#else
1368 va_start(vargs);
1369#endif
1370
1371 assert(min >= 0);
1372 assert(min <= max);
1373 if (!PyTuple_Check(args)) {
1374 PyErr_SetString(PyExc_SystemError,
1375 "PyArg_UnpackTuple() argument list is not a tuple");
1376 return 0;
1377 }
1378 l = PyTuple_GET_SIZE(args);
1379 if (l < min) {
1380 if (name != NULL)
1381 PyErr_Format(
1382 PyExc_TypeError,
1383 "%s expected %s%d arguments, got %d",
1384 name, (min == max ? "" : "at least "), min, l);
1385 else
1386 PyErr_Format(
1387 PyExc_TypeError,
1388 "unpacked tuple should have %s%d elements,"
1389 " but has %d",
1390 (min == max ? "" : "at least "), min, l);
1391 va_end(vargs);
1392 return 0;
1393 }
1394 if (l > max) {
1395 if (name != NULL)
1396 PyErr_Format(
1397 PyExc_TypeError,
1398 "%s expected %s%d arguments, got %d",
1399 name, (min == max ? "" : "at most "), max, l);
1400 else
1401 PyErr_Format(
1402 PyExc_TypeError,
1403 "unpacked tuple should have %s%d elements,"
1404 " but has %d",
1405 (min == max ? "" : "at most "), max, l);
1406 va_end(vargs);
1407 return 0;
1408 }
1409 for (i = 0; i < l; i++) {
1410 o = va_arg(vargs, PyObject **);
1411 *o = PyTuple_GET_ITEM(args, i);
1412 }
1413 va_end(vargs);
1414 return 1;
1415}