blob: 0acb23a38ebaa8a052131e014a35c42196b8d672 [file] [log] [blame]
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001
2/* New getargs implementation */
3
4/* XXX There are several unchecked sprintf or strcat calls in this file.
5 XXX The only way these can become a danger is if some C code in the
6 XXX Python source (or in an extension) uses ridiculously long names
Thomas Wouters7e474022000-07-16 12:04:32 +00007 XXX or ridiculously deep nesting in format strings. */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00008
Guido van Rossum79f25d91997-04-29 20:08:16 +00009#include "Python.h"
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000010
Guido van Rossumc1d50531996-08-21 23:38:24 +000011#include <ctype.h>
12
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000013
Tim Petersdbd9ba62000-07-09 03:09:57 +000014int PyArg_Parse(PyObject *, char *, ...);
15int PyArg_ParseTuple(PyObject *, char *, ...);
16int PyArg_VaParse(PyObject *, char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000017
Tim Petersdbd9ba62000-07-09 03:09:57 +000018int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
19 char *, char **, ...);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000020
21/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000022static int vgetargs1(PyObject *, char *, va_list *, int);
23static void seterror(int, char *, int *, char *, char *);
24static char *convertitem(PyObject *, char **, va_list *, int *, char *);
25static char *converttuple(PyObject *, char **, va_list *,
26 int *, char *, int);
27static char *convertsimple(PyObject *, char **, va_list *, char *);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +000028static int convertbuffer(PyObject *, void **p, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000029
Tim Petersdbd9ba62000-07-09 03:09:57 +000030static int vgetargskeywords(PyObject *, PyObject *,
31 char *, char **, va_list *);
32static char *skipitem(char **, va_list *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000033
Guido van Rossum79f25d91997-04-29 20:08:16 +000034int PyArg_Parse(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000035{
36 int retval;
37 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000038
39 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000040 retval = vgetargs1(args, format, &va, 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000041 va_end(va);
42 return retval;
43}
44
45
Guido van Rossum79f25d91997-04-29 20:08:16 +000046int PyArg_ParseTuple(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000047{
48 int retval;
49 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000050
51 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000052 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000053 va_end(va);
54 return retval;
55}
56
57
58int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000059PyArg_VaParse(PyObject *args, char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000060{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000061 va_list lva;
62
63#ifdef VA_LIST_IS_ARRAY
64 memcpy(lva, va, sizeof(va_list));
65#else
66 lva = va;
67#endif
68
69 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000070}
71
72
73static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000074vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000075{
76 char msgbuf[256];
77 int levels[32];
78 char *fname = NULL;
79 char *message = NULL;
80 int min = -1;
81 int max = 0;
82 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000083 int endfmt = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000084 char *formatsave = format;
85 int i, len;
86 char *msg;
87
Tim Peters5c4d5bf2001-02-12 22:13:26 +000088 assert(compat || (args != (PyObject*)NULL));
89
Jeremy Hylton25916bd2001-05-29 17:46:19 +000090 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000091 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000092 switch (c) {
93 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000094 if (level == 0)
95 max++;
96 level++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000097 break;
98 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000099 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000100 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000101 else
102 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000103 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000104 case '\0':
105 endfmt = 1;
106 break;
107 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000108 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000109 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000110 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000111 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000112 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000113 endfmt = 1;
114 break;
115 default:
116 if (level == 0) {
117 if (c == 'O')
118 max++;
119 else if (isalpha(c)) {
120 if (c != 'e') /* skip encoded */
121 max++;
122 } else if (c == '|')
123 min = max;
124 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000125 break;
126 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000127 }
128
129 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000130 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000131
132 if (min < 0)
133 min = max;
134
135 format = formatsave;
136
137 if (compat) {
138 if (max == 0) {
139 if (args == NULL)
140 return 1;
Ka-Ping Yee20579702001-01-15 22:14:16 +0000141 sprintf(msgbuf, "%s%s takes no arguments",
142 fname==NULL ? "function" : fname,
143 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000145 return 0;
146 }
147 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000148 if (args == NULL) {
149 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000150 "%s%s takes at least one argument",
151 fname==NULL ? "function" : fname,
152 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000153 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000154 return 0;
155 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000156 msg = convertitem(args, &format, p_va, levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000157 if (msg == NULL)
158 return 1;
159 seterror(levels[0], msg, levels+1, fname, message);
160 return 0;
161 }
162 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000164 "old style getargs format uses new features");
165 return 0;
166 }
167 }
168
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 if (!PyTuple_Check(args)) {
170 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000171 "new style getargs format but argument is not a tuple");
172 return 0;
173 }
174
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000175 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000176
177 if (len < min || max < len) {
178 if (message == NULL) {
179 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000180 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000181 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000182 fname==NULL ? "" : "()",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000183 min==max ? "exactly"
184 : len < min ? "at least" : "at most",
185 len < min ? min : max,
186 (len < min ? min : max) == 1 ? "" : "s",
187 len);
188 message = msgbuf;
189 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000190 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000191 return 0;
192 }
193
194 for (i = 0; i < len; i++) {
195 if (*format == '|')
196 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000197 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
198 levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000199 if (msg) {
200 seterror(i+1, msg, levels, fname, message);
201 return 0;
202 }
203 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000204
Guido van Rossum730806d1998-04-10 22:27:42 +0000205 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000206 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000207 *format != '|' && *format != ':' && *format != ';') {
208 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000209 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000210 return 0;
211 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000212
213 return 1;
214}
215
216
217
218static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000220{
221 char buf[256];
222 int i;
223 char *p = buf;
224
Guido van Rossum79f25d91997-04-29 20:08:16 +0000225 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000226 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000227 else if (message == NULL) {
228 if (fname != NULL) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000229 sprintf(p, "%s() ", 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);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000236 while (levels[i] > 0) {
237 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 }
246 sprintf(p, " %s", 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;
300 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000301 toplevel ? "expected %d arguments, not %s" :
302 "must be %d-item sequence, not %s",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000303 n, arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000304 return msgbuf;
305 }
306
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000307 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000308 levels[0] = 0;
309 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000310 toplevel ? "expected %d arguments, not %d" :
311 "must be sequence of length %d, not %d",
312 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000313 return msgbuf;
314 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000315
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000316 format = *p_format;
317 for (i = 0; i < n; i++) {
318 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000319 PyObject *item;
320 item = PySequence_GetItem(arg, i);
321 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
322 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
323 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000324 if (msg != NULL) {
325 levels[0] = i+1;
326 return msg;
327 }
328 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000329
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000330 *p_format = format;
331 return NULL;
332}
333
334
335/* Convert a single item. */
336
337static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000338convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
339 char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000340{
341 char *msg;
342 char *format = *p_format;
343
344 if (*format == '(' /* ')' */) {
345 format++;
346 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
347 if (msg == NULL)
348 format++;
349 }
350 else {
351 msg = convertsimple(arg, &format, p_va, msgbuf);
352 if (msg != NULL)
353 levels[0] = 0;
354 }
355 if (msg == NULL)
356 *p_format = format;
357 return msg;
358}
359
360
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000361
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000362#define UNICODE_DEFAULT_ENCODING(arg) \
363 _PyUnicode_AsDefaultEncodedString(arg, NULL)
364
365/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000366
367static char *
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000368converterr(char *expected, PyObject *arg, char *msgbuf)
369{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000370 assert(expected != NULL);
371 assert(arg != NULL);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000372 sprintf(msgbuf, "must be %.50s, not %.50s", expected,
373 arg == Py_None ? "None" : arg->ob_type->tp_name);
374 return msgbuf;
375}
376
377#define CONV_UNICODE "(unicode conversion error)"
378
379/* Convert a non-tuple argument. Return NULL if conversion went OK,
380 or a string with a message describing the failure. The message is
381 formatted as "must be <desired type>, not <actual type>".
382 When failing, an exception may or may not have been raised.
383 Don't call if a tuple is expected.
384*/
385
386static char *
387convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000388{
389 char *format = *p_format;
390 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000391 PyObject *uarg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000392
393 switch (c) {
394
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000395 case 'b': { /* unsigned byte -- very short int */
396 char *p = va_arg(*p_va, char *);
397 long ival = PyInt_AsLong(arg);
398 if (ival == -1 && PyErr_Occurred())
399 return converterr("integer<b>", arg, msgbuf);
400 else if (ival < 0) {
401 PyErr_SetString(PyExc_OverflowError,
402 "unsigned byte integer is less than minimum");
403 return converterr("integer<b>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000404 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000405 else if (ival > UCHAR_MAX) {
406 PyErr_SetString(PyExc_OverflowError,
407 "unsigned byte integer is greater than maximum");
408 return converterr("integer<b>", arg, msgbuf);
409 }
410 else
411 *p = (unsigned char) ival;
412 break;
413 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000414
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000415 case 'B': {/* byte sized bitfield - both signed and unsigned
416 values allowed */
417 char *p = va_arg(*p_va, char *);
418 long ival = PyInt_AsLong(arg);
419 if (ival == -1 && PyErr_Occurred())
420 return converterr("integer<b>", arg, msgbuf);
421 else if (ival < SCHAR_MIN) {
422 PyErr_SetString(PyExc_OverflowError,
423 "byte-sized integer bitfield is less than minimum");
424 return converterr("integer<B>", arg, msgbuf);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000425 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000426 else if (ival > (int)UCHAR_MAX) {
427 PyErr_SetString(PyExc_OverflowError,
428 "byte-sized integer bitfield is greater than maximum");
429 return converterr("integer<B>", arg, msgbuf);
430 }
431 else
432 *p = (unsigned char) ival;
433 break;
434 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000435
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000436 case 'h': {/* signed short int */
437 short *p = va_arg(*p_va, short *);
438 long ival = PyInt_AsLong(arg);
439 if (ival == -1 && PyErr_Occurred())
440 return converterr("integer<h>", arg, msgbuf);
441 else if (ival < SHRT_MIN) {
442 PyErr_SetString(PyExc_OverflowError,
443 "signed short integer is less than minimum");
444 return converterr("integer<h>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000445 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000446 else if (ival > SHRT_MAX) {
447 PyErr_SetString(PyExc_OverflowError,
448 "signed short integer is greater than maximum");
449 return converterr("integer<h>", arg, msgbuf);
450 }
451 else
452 *p = (short) ival;
453 break;
454 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000455
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000456 case 'H': { /* short int sized bitfield, both signed and
457 unsigned allowed */
458 unsigned short *p = va_arg(*p_va, unsigned short *);
459 long ival = PyInt_AsLong(arg);
460 if (ival == -1 && PyErr_Occurred())
461 return converterr("integer<H>", arg, msgbuf);
462 else if (ival < SHRT_MIN) {
463 PyErr_SetString(PyExc_OverflowError,
464 "short integer bitfield is less than minimum");
465 return converterr("integer<H>", arg, msgbuf);
Jack Jansend50338f2000-07-06 12:22:00 +0000466 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000467 else if (ival > USHRT_MAX) {
468 PyErr_SetString(PyExc_OverflowError,
469 "short integer bitfield is greater than maximum");
470 return converterr("integer<H>", arg, msgbuf);
471 }
472 else
473 *p = (unsigned short) ival;
474 break;
475 }
Jack Jansend50338f2000-07-06 12:22:00 +0000476
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000477 case 'i': {/* signed int */
478 int *p = va_arg(*p_va, int *);
479 long ival = PyInt_AsLong(arg);
480 if (ival == -1 && PyErr_Occurred())
481 return converterr("integer<i>", arg, msgbuf);
482 else if (ival > INT_MAX) {
483 PyErr_SetString(PyExc_OverflowError,
484 "signed integer is greater than maximum");
485 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000486 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000487 else if (ival < INT_MIN) {
488 PyErr_SetString(PyExc_OverflowError,
489 "signed integer is less than minimum");
490 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000491 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000492 else
493 *p = ival;
494 break;
495 }
496
497 case 'l': {/* long int */
498 long *p = va_arg(*p_va, long *);
499 long ival = PyInt_AsLong(arg);
500 if (ival == -1 && PyErr_Occurred())
501 return converterr("integer<l>", arg, msgbuf);
502 else
503 *p = ival;
504 break;
505 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000506
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000507#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000508 case 'L': {/* LONG_LONG */
509 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
510 LONG_LONG ival = PyLong_AsLongLong( arg );
511 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
512 return converterr("long<L>", arg, msgbuf);
513 } else {
514 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000515 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000516 break;
517 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000518#endif
519
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000520 case 'f': {/* float */
521 float *p = va_arg(*p_va, float *);
522 double dval = PyFloat_AsDouble(arg);
523 if (PyErr_Occurred())
524 return converterr("float<f>", arg, msgbuf);
525 else
526 *p = (float) dval;
527 break;
528 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000529
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000530 case 'd': {/* double */
531 double *p = va_arg(*p_va, double *);
532 double dval = PyFloat_AsDouble(arg);
533 if (PyErr_Occurred())
534 return converterr("float<d>", arg, msgbuf);
535 else
536 *p = dval;
537 break;
538 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000539
Guido van Rossum530956d1996-07-21 02:27:43 +0000540#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000541 case 'D': {/* complex double */
542 Py_complex *p = va_arg(*p_va, Py_complex *);
543 Py_complex cval;
544 cval = PyComplex_AsCComplex(arg);
545 if (PyErr_Occurred())
546 return converterr("complex<D>", arg, msgbuf);
547 else
548 *p = cval;
549 break;
550 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000551#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000552
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000553 case 'c': {/* char */
554 char *p = va_arg(*p_va, char *);
555 if (PyString_Check(arg) && PyString_Size(arg) == 1)
Jeremy Hylton0407aea2001-10-10 02:51:57 +0000556 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000557 else
558 return converterr("char", arg, msgbuf);
559 break;
560 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000561
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000562 case 's': {/* string */
563 if (*format == '#') {
564 void **p = (void **)va_arg(*p_va, char **);
565 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000566
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000567 if (PyString_Check(arg)) {
568 *p = PyString_AS_STRING(arg);
569 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000570 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000571#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000572 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000573 uarg = UNICODE_DEFAULT_ENCODING(arg);
574 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000575 return converterr(CONV_UNICODE,
576 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000577 *p = PyString_AS_STRING(uarg);
578 *q = PyString_GET_SIZE(uarg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000579 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000580#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000581 else { /* any buffer-like object */
582 char *buf;
583 int count = convertbuffer(arg, p, &buf);
584 if (count < 0)
585 return converterr(buf, arg, msgbuf);
586 *q = count;
587 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000588 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000589 } else {
590 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000591
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000592 if (PyString_Check(arg))
593 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000594#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000595 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000596 uarg = UNICODE_DEFAULT_ENCODING(arg);
597 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000598 return converterr(CONV_UNICODE,
599 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000600 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000601 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000602#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000603 else
604 return converterr("string", arg, msgbuf);
605 if ((int)strlen(*p) != PyString_Size(arg))
606 return converterr("string without null bytes",
607 arg, msgbuf);
608 }
609 break;
610 }
611
612 case 'z': {/* string, may be NULL (None) */
613 if (*format == '#') { /* any buffer-like object */
614 void **p = (void **)va_arg(*p_va, char **);
615 int *q = va_arg(*p_va, int *);
616
617 if (arg == Py_None) {
618 *p = 0;
619 *q = 0;
620 }
621 else if (PyString_Check(arg)) {
622 *p = PyString_AS_STRING(arg);
623 *q = PyString_GET_SIZE(arg);
624 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000625#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000626 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000627 uarg = UNICODE_DEFAULT_ENCODING(arg);
628 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000629 return converterr(CONV_UNICODE,
630 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000631 *p = PyString_AS_STRING(uarg);
632 *q = PyString_GET_SIZE(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000633 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000634#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000635 else { /* any buffer-like object */
636 char *buf;
637 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000638 if (count < 0)
639 return converterr(buf, arg, msgbuf);
640 *q = count;
641 }
642 format++;
643 } else {
644 char **p = va_arg(*p_va, char **);
645
646 if (arg == Py_None)
647 *p = 0;
648 else if (PyString_Check(arg))
Jeremy Hyltona4c8cd72001-10-10 02:51:08 +0000649 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000650#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000651 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000652 uarg = UNICODE_DEFAULT_ENCODING(arg);
653 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000654 return converterr(CONV_UNICODE,
655 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000656 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000657 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000658#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000659 else
660 return converterr("string or None",
661 arg, msgbuf);
662 if (*format == '#') {
663 int *q = va_arg(*p_va, int *);
664 if (arg == Py_None)
665 *q = 0;
666 else
667 *q = PyString_Size(arg);
668 format++;
669 }
670 else if (*p != NULL &&
671 (int)strlen(*p) != PyString_Size(arg))
672 return converterr(
673 "string without null bytes or None",
674 arg, msgbuf);
675 }
676 break;
677 }
678
679 case 'e': {/* encoded string */
680 char **buffer;
681 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000682 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000683 int size, recode_strings;
684
685 /* Get 'e' parameter: the encoding name */
686 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000687#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000688 if (encoding == NULL)
689 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000690#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000691
692 /* Get output buffer parameter:
693 's' (recode all objects via Unicode) or
694 't' (only recode non-string objects)
695 */
696 if (*format == 's')
697 recode_strings = 1;
698 else if (*format == 't')
699 recode_strings = 0;
700 else
701 return converterr(
702 "(unknown parser marker combination)",
703 arg, msgbuf);
704 buffer = (char **)va_arg(*p_va, char **);
705 format++;
706 if (buffer == NULL)
707 return converterr("(buffer is NULL)",
708 arg, msgbuf);
709
710 /* Encode object */
711 if (!recode_strings && PyString_Check(arg)) {
712 s = arg;
713 Py_INCREF(s);
714 }
715 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000716#ifdef Py_USING_UNICODE
717 PyObject *u;
718
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000719 /* Convert object to Unicode */
720 u = PyUnicode_FromObject(arg);
721 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000722 return converterr(
723 "string or unicode or text buffer",
724 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000725
726 /* Encode object; use default error handling */
727 s = PyUnicode_AsEncodedString(u,
728 encoding,
729 NULL);
730 Py_DECREF(u);
731 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000732 return converterr("(encoding failed)",
733 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000734 if (!PyString_Check(s)) {
735 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000736 return converterr(
737 "(encoder failed to return a string)",
738 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000739 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000740#else
741 return converterr("string<e>", arg, msgbuf);
742#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000743 }
744 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000745
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000746 /* Write output; output is guaranteed to be 0-terminated */
747 if (*format == '#') {
748 /* Using buffer length parameter '#':
749
750 - if *buffer is NULL, a new buffer of the
751 needed size is allocated and the data
752 copied into it; *buffer is updated to point
753 to the new buffer; the caller is
754 responsible for PyMem_Free()ing it after
755 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000756
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000757 - if *buffer is not NULL, the data is
758 copied to *buffer; *buffer_len has to be
759 set to the size of the buffer on input;
760 buffer overflow is signalled with an error;
761 buffer has to provide enough room for the
762 encoded string plus the trailing 0-byte
763
764 - in both cases, *buffer_len is updated to
765 the size of the buffer /excluding/ the
766 trailing 0-byte
767
768 */
769 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000770
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000771 format++;
772 if (buffer_len == NULL)
773 return converterr(
774 "(buffer_len is NULL)",
775 arg, msgbuf);
776 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000777 *buffer = PyMem_NEW(char, size + 1);
778 if (*buffer == NULL) {
779 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000780 return converterr(
781 "(memory error)",
782 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000783 }
Fred Drake25871c02000-05-03 15:17:02 +0000784 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000785 if (size + 1 > *buffer_len) {
786 Py_DECREF(s);
787 return converterr(
788 "(buffer overflow)",
789 arg, msgbuf);
790 }
Fred Drake25871c02000-05-03 15:17:02 +0000791 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000792 memcpy(*buffer,
793 PyString_AS_STRING(s),
794 size + 1);
795 *buffer_len = size;
796 } else {
797 /* Using a 0-terminated buffer:
798
799 - the encoded string has to be 0-terminated
800 for this variant to work; if it is not, an
801 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000802
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000803 - a new buffer of the needed size is
804 allocated and the data copied into it;
805 *buffer is updated to point to the new
806 buffer; the caller is responsible for
807 PyMem_Free()ing it after usage
808
809 */
810 if ((int)strlen(PyString_AS_STRING(s)) != size)
811 return converterr(
812 "(encoded string without NULL bytes)",
813 arg, msgbuf);
814 *buffer = PyMem_NEW(char, size + 1);
815 if (*buffer == NULL) {
816 Py_DECREF(s);
817 return converterr("(memory error)",
818 arg, msgbuf);
819 }
820 memcpy(*buffer,
821 PyString_AS_STRING(s),
822 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000823 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000824 Py_DECREF(s);
825 break;
826 }
827
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000828#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000829 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
830 if (*format == '#') { /* any buffer-like object */
831 void **p = (void **)va_arg(*p_va, char **);
832 int *q = va_arg(*p_va, int *);
833 char *buf;
834 int count = convertbuffer(arg, p, &buf);
835
836 if (count < 0)
837 return converterr(buf, arg, msgbuf);
838 *q = count/(sizeof(Py_UNICODE));
839 format++;
840 } else {
841 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
842
Guido van Rossume826ef02000-03-10 23:02:17 +0000843 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000844 *p = PyUnicode_AS_UNICODE(arg);
845 else
846 return converterr("unicode", arg, msgbuf);
847 }
848 break;
849 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000850#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000851
852 case 'S': { /* string object */
853 PyObject **p = va_arg(*p_va, PyObject **);
854 if (PyString_Check(arg))
855 *p = arg;
856 else
857 return converterr("string", arg, msgbuf);
858 break;
859 }
860
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000861#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000862 case 'U': { /* Unicode object */
863 PyObject **p = va_arg(*p_va, PyObject **);
864 if (PyUnicode_Check(arg))
865 *p = arg;
866 else
867 return converterr("unicode", arg, msgbuf);
868 break;
869 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000870#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000871
872 case 'O': { /* object */
873 PyTypeObject *type;
874 PyObject **p;
875 if (*format == '!') {
876 type = va_arg(*p_va, PyTypeObject*);
877 p = va_arg(*p_va, PyObject **);
878 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +0000879 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +0000880 *p = arg;
881 else
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000882 return converterr(type->tp_name, arg, msgbuf);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000883
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000884 }
885 else if (*format == '?') {
886 inquiry pred = va_arg(*p_va, inquiry);
887 p = va_arg(*p_va, PyObject **);
888 format++;
889 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000890 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000891 else
892 return converterr("(unspecified)",
893 arg, msgbuf);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000894
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000895 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000896 else if (*format == '&') {
897 typedef int (*converter)(PyObject *, void *);
898 converter convert = va_arg(*p_va, converter);
899 void *addr = va_arg(*p_va, void *);
900 format++;
901 if (! (*convert)(arg, addr))
902 return converterr("(unspecified)",
903 arg, msgbuf);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000904 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000905 else {
906 p = va_arg(*p_va, PyObject **);
907 *p = arg;
908 }
909 break;
910 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000911
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000912
913 case 'w': { /* memory buffer, read-write access */
914 void **p = va_arg(*p_va, void **);
915 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
916 int count;
917
918 if (pb == NULL ||
919 pb->bf_getwritebuffer == NULL ||
920 pb->bf_getsegcount == NULL)
921 return converterr("read-write buffer", arg, msgbuf);
922 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
923 return converterr("single-segment read-write buffer",
924 arg, msgbuf);
925 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
926 return converterr("(unspecified)", arg, msgbuf);
927 if (*format == '#') {
928 int *q = va_arg(*p_va, int *);
929
930 *q = count;
931 format++;
932 }
933 break;
934 }
935
936 case 't': { /* 8-bit character buffer, read-only access */
937 const char **p = va_arg(*p_va, const char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +0000938 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000939 int count;
940
941 if (*format++ != '#')
942 return converterr(
943 "invalid use of 't' format character",
944 arg, msgbuf);
945 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +0000946 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
947 pb == NULL || pb->bf_getcharbuffer == NULL ||
948 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000949 return converterr(
950 "string or read-only character buffer",
951 arg, msgbuf);
952
Jeremy Hylton4819e972001-10-11 14:40:37 +0000953 if (pb->bf_getsegcount(arg, NULL) != 1)
954 return converterr(
955 "string or single-segment read-only buffer",
956 arg, msgbuf);
957
958 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000959 if (count < 0)
Jeremy Hylton4819e972001-10-11 14:40:37 +0000960 return converterr("(unspecified)", arg, msgbuf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000961 *va_arg(*p_va, int *) = count;
962 break;
963 }
964
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000965 default:
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000966 return converterr("impossible<bad format char>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000967
968 }
969
970 *p_format = format;
971 return NULL;
972}
Guido van Rossumaa354651996-08-19 19:32:04 +0000973
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000974int convertbuffer(PyObject *arg, void **p, char **errmsg)
975{
976 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
977 int count;
978 if (pb == NULL ||
979 pb->bf_getreadbuffer == NULL ||
980 pb->bf_getsegcount == NULL) {
981 *errmsg = "string or read-only buffer";
982 return -1;
983 }
984 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
985 *errmsg = "string or single-segment read-only buffer";
986 return -1;
987 }
988 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
989 *errmsg = "(unspecified)";
990 }
991 return count;
992}
Guido van Rossumaa354651996-08-19 19:32:04 +0000993
994/* Support for keyword arguments donated by
995 Geoff Philbrick <philbric@delphi.hks.com> */
996
Guido van Rossum79f25d91997-04-29 20:08:16 +0000997int PyArg_ParseTupleAndKeywords(PyObject *args,
998 PyObject *keywords,
Guido van Rossumaa354651996-08-19 19:32:04 +0000999 char *format,
1000 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001001{
1002 int retval;
1003 va_list va;
Guido van Rossumaa354651996-08-19 19:32:04 +00001004
1005 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001006 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
1007 va_end(va);
1008 return retval;
1009}
1010
1011
1012static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001013vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
1014 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001015{
1016 char msgbuf[256];
1017 int levels[32];
1018 char *fname = NULL;
1019 char *message = NULL;
1020 int min = -1;
1021 int max = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001022 char *formatsave = format;
1023 int i, len, tplen, kwlen;
1024 char *msg, *ks, **p;
1025 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001026 PyObject *key, *value;
Guido van Rossumaa354651996-08-19 19:32:04 +00001027
1028 /* nested tuples cannot be parsed when using keyword arguments */
1029
1030 for (;;) {
1031 int c = *format++;
1032 if (c == '(') {
1033 PyErr_SetString(PyExc_SystemError,
1034 "tuple found in format when using keyword arguments");
1035 return 0;
1036 }
1037 else if (c == '\0')
1038 break;
1039 else if (c == ':') {
1040 fname = format;
1041 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001042 } else if (c == ';') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001043 message = format;
1044 break;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001045 } else if (c == 'e')
Marc-André Lemburgbbcf2a72000-09-08 11:49:37 +00001046 ; /* Pass */
Guido van Rossumaa354651996-08-19 19:32:04 +00001047 else if (isalpha(c))
1048 max++;
1049 else if (c == '|')
1050 min = max;
1051 }
1052
1053 if (min < 0)
1054 min = max;
1055
1056 format = formatsave;
1057
1058 if (!PyTuple_Check(args)) {
1059 PyErr_SetString(PyExc_SystemError,
1060 "new style getargs format but argument is not a tuple");
1061 return 0;
1062 }
1063
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001064 tplen = PyTuple_GET_SIZE(args);
Guido van Rossumaa354651996-08-19 19:32:04 +00001065
1066 /* do a cursory check of the keywords just to see how many we got */
1067
1068 if (keywords) {
1069 if (!PyDict_Check(keywords)) {
Jeremy Hyltona0ac40c2001-01-25 20:13:10 +00001070 if (keywords == NULL)
1071 PyErr_SetString(PyExc_SystemError,
1072 "NULL received when keyword dictionary expected");
1073 else
1074 PyErr_Format(PyExc_SystemError,
1075 "%s received when keyword dictionary expected",
1076 keywords->ob_type->tp_name);
Guido van Rossumaa354651996-08-19 19:32:04 +00001077 return 0;
1078 }
1079 kwlen = PyDict_Size(keywords);
1080 }
1081 else {
1082 kwlen = 0;
1083 }
1084
1085 /* make sure there are no duplicate values for an argument;
1086 its not clear when to use the term "keyword argument vs.
1087 keyword parameter in messages */
1088
1089 if (keywords) {
1090 for (i = 0; i < tplen; i++) {
1091 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1092 sprintf(msgbuf,
1093 "keyword parameter %s redefined",
1094 kwlist[i]);
1095 PyErr_SetString(PyExc_TypeError, msgbuf);
1096 return 0;
1097 }
1098 }
1099 }
1100 PyErr_Clear(); /* I'm not which Py functions set the error string */
1101
1102 /* required arguments missing from args can be supplied by keyword
1103 arguments */
1104
1105 len = tplen;
1106 if (keywords && tplen < min) {
1107 for (i = tplen; i < min; i++) {
1108 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1109 len++;
1110 }
1111 }
1112 }
1113 PyErr_Clear();
1114
1115 /* make sure we got an acceptable number of arguments; the message
1116 is a little confusing with keywords since keyword arguments
1117 which are supplied, but don't match the required arguments
1118 are not included in the "%d given" part of the message */
1119
1120 if (len < min || max < len) {
1121 if (message == NULL) {
1122 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001123 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001124 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001125 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001126 min==max ? "exactly"
1127 : len < min ? "at least" : "at most",
1128 len < min ? min : max,
1129 (len < min ? min : max) == 1 ? "" : "s",
1130 len);
1131 message = msgbuf;
1132 }
1133 PyErr_SetString(PyExc_TypeError, message);
1134 return 0;
1135 }
1136
1137 for (i = 0; i < tplen; i++) {
1138 if (*format == '|')
1139 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001140 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001141 levels, msgbuf);
1142 if (msg) {
1143 seterror(i+1, msg, levels, fname, message);
1144 return 0;
1145 }
1146 }
1147
1148 /* handle no keyword parameters in call */
1149
1150 if (!keywords) return 1;
1151
1152 /* make sure the number of keywords in the keyword list matches the
1153 number of items in the format string */
1154
1155 nkwds = 0;
1156 p = kwlist;
1157 for (;;) {
1158 if (!*(p++)) break;
1159 nkwds++;
1160 }
1161
1162 if (nkwds != max) {
1163 PyErr_SetString(PyExc_SystemError,
1164 "number of items in format string and keyword list do not match");
1165 return 0;
1166 }
1167
1168 /* convert the keyword arguments; this uses the format
1169 string where it was left after processing args */
1170
1171 converted = 0;
1172 for (i = tplen; i < nkwds; 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++;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001176 item = PyMapping_GetItemString(keywords, kwlist[i]);
1177 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001178 msg = convertitem(item, &format, p_va, levels, msgbuf);
1179 if (msg) {
1180 seterror(i+1, msg, levels, fname, message);
1181 return 0;
1182 }
1183 converted++;
Barry Warsaw07050282000-12-11 20:01:55 +00001184 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001185 }
1186 else {
1187 PyErr_Clear();
1188 msg = skipitem(&format, p_va);
1189 if (msg) {
1190 seterror(i+1, msg, levels, fname, message);
1191 return 0;
1192 }
1193 }
1194 }
1195
1196 /* make sure there are no extraneous keyword arguments */
1197
1198 pos = 0;
1199 if (converted < kwlen) {
1200 while (PyDict_Next(keywords, &pos, &key, &value)) {
1201 match = 0;
1202 ks = PyString_AsString(key);
1203 for (i = 0; i < nkwds; i++) {
1204 if (!strcmp(ks, kwlist[i])) {
1205 match = 1;
1206 break;
1207 }
1208 }
1209 if (!match) {
1210 sprintf(msgbuf,
Guido van Rossum80dc16b2000-05-08 14:02:41 +00001211 "%s is an invalid keyword argument for this function",
Guido van Rossumaa354651996-08-19 19:32:04 +00001212 ks);
1213 PyErr_SetString(PyExc_TypeError, msgbuf);
1214 return 0;
1215 }
1216 }
1217 }
1218
1219 return 1;
1220}
1221
1222
1223static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001224skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001225{
1226 char *format = *p_format;
1227 char c = *format++;
1228
1229 switch (c) {
1230
1231 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001232 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001233 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001234 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001235 break;
1236 }
1237
1238 case 'h': /* short int */
1239 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001240 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001241 break;
1242 }
1243
Jack Jansencc22fbe2000-08-05 21:29:58 +00001244 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001245 {
1246 (void) va_arg(*p_va, unsigned short *);
1247 break;
1248 }
1249
Guido van Rossumaa354651996-08-19 19:32:04 +00001250 case 'i': /* int */
1251 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001252 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001253 break;
1254 }
1255
1256 case 'l': /* long int */
1257 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001258 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001259 break;
1260 }
1261
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001262#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001263 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001264 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001265 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001266 break;
1267 }
1268#endif
1269
Guido van Rossumaa354651996-08-19 19:32:04 +00001270 case 'f': /* float */
1271 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001272 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001273 break;
1274 }
1275
1276 case 'd': /* double */
1277 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001278 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001279 break;
1280 }
1281
1282#ifndef WITHOUT_COMPLEX
1283 case 'D': /* complex double */
1284 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001285 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001286 break;
1287 }
1288#endif /* WITHOUT_COMPLEX */
1289
1290 case 'c': /* char */
1291 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001292 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001293 break;
1294 }
1295
1296 case 's': /* string */
1297 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001298 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001299 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001300 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001301 format++;
1302 }
1303 break;
1304 }
1305
1306 case 'z': /* string */
1307 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001308 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001309 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001310 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001311 format++;
1312 }
1313 break;
1314 }
1315
1316 case 'S': /* string object */
1317 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001318 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001319 break;
1320 }
1321
1322 case 'O': /* object */
1323 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001324 if (*format == '!') {
1325 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001326 (void) va_arg(*p_va, PyTypeObject*);
1327 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001328 }
1329#if 0
1330/* I don't know what this is for */
1331 else if (*format == '?') {
1332 inquiry pred = va_arg(*p_va, inquiry);
1333 format++;
1334 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001335 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001336 }
1337 }
1338#endif
1339 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001340 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001341 (void) va_arg(*p_va, converter);
1342 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001343 format++;
1344 }
1345 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001346 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001347 }
1348 break;
1349 }
1350
1351 default:
1352 return "impossible<bad format char>";
1353
1354 }
1355
1356 *p_format = format;
1357 return NULL;
1358}