blob: 2f876ff6455dc1870da15ce36f22941217496afb [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
Fred Drake563dfc22001-10-23 14:41:08 +000034int
35PyArg_Parse(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000036{
37 int retval;
38 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000039
40 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000041 retval = vgetargs1(args, format, &va, 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000042 va_end(va);
43 return retval;
44}
45
46
Fred Drake563dfc22001-10-23 14:41:08 +000047int
48PyArg_ParseTuple(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000049{
50 int retval;
51 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000052
53 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000054 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000055 va_end(va);
56 return retval;
57}
58
59
60int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000061PyArg_VaParse(PyObject *args, char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000062{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000063 va_list lva;
64
65#ifdef VA_LIST_IS_ARRAY
66 memcpy(lva, va, sizeof(va_list));
67#else
68 lva = va;
69#endif
70
71 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000072}
73
74
75static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000076vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000077{
78 char msgbuf[256];
79 int levels[32];
80 char *fname = NULL;
81 char *message = NULL;
82 int min = -1;
83 int max = 0;
84 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000085 int endfmt = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000086 char *formatsave = format;
87 int i, len;
88 char *msg;
89
Tim Peters5c4d5bf2001-02-12 22:13:26 +000090 assert(compat || (args != (PyObject*)NULL));
91
Jeremy Hylton25916bd2001-05-29 17:46:19 +000092 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000093 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000094 switch (c) {
95 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000096 if (level == 0)
97 max++;
98 level++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000099 break;
100 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000101 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000102 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000103 else
104 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000105 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000106 case '\0':
107 endfmt = 1;
108 break;
109 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000110 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000111 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000112 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000113 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000114 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000115 endfmt = 1;
116 break;
117 default:
118 if (level == 0) {
119 if (c == 'O')
120 max++;
121 else if (isalpha(c)) {
122 if (c != 'e') /* skip encoded */
123 max++;
124 } else if (c == '|')
125 min = max;
126 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000127 break;
128 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000129 }
130
131 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000132 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000133
134 if (min < 0)
135 min = max;
136
137 format = formatsave;
138
139 if (compat) {
140 if (max == 0) {
141 if (args == NULL)
142 return 1;
Ka-Ping Yee20579702001-01-15 22:14:16 +0000143 sprintf(msgbuf, "%s%s takes no arguments",
144 fname==NULL ? "function" : fname,
145 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000146 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000147 return 0;
148 }
149 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000150 if (args == NULL) {
151 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000152 "%s%s takes at least one argument",
153 fname==NULL ? "function" : fname,
154 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000155 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000156 return 0;
157 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000158 msg = convertitem(args, &format, p_va, levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000159 if (msg == NULL)
160 return 1;
161 seterror(levels[0], msg, levels+1, fname, message);
162 return 0;
163 }
164 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000165 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000166 "old style getargs format uses new features");
167 return 0;
168 }
169 }
170
Guido van Rossum79f25d91997-04-29 20:08:16 +0000171 if (!PyTuple_Check(args)) {
172 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000173 "new style getargs format but argument is not a tuple");
174 return 0;
175 }
176
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000177 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000178
179 if (len < min || max < len) {
180 if (message == NULL) {
181 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000182 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000183 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000184 fname==NULL ? "" : "()",
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000185 min==max ? "exactly"
186 : len < min ? "at least" : "at most",
187 len < min ? min : max,
188 (len < min ? min : max) == 1 ? "" : "s",
189 len);
190 message = msgbuf;
191 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000192 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000193 return 0;
194 }
195
196 for (i = 0; i < len; i++) {
197 if (*format == '|')
198 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000199 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
200 levels, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000201 if (msg) {
202 seterror(i+1, msg, levels, fname, message);
203 return 0;
204 }
205 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000206
Guido van Rossum730806d1998-04-10 22:27:42 +0000207 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000208 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000209 *format != '|' && *format != ':' && *format != ';') {
210 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000211 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000212 return 0;
213 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000214
215 return 1;
216}
217
218
219
220static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000221seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000222{
223 char buf[256];
224 int i;
225 char *p = buf;
226
Guido van Rossum79f25d91997-04-29 20:08:16 +0000227 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000228 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000229 else if (message == NULL) {
230 if (fname != NULL) {
Ka-Ping Yee20579702001-01-15 22:14:16 +0000231 sprintf(p, "%s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000232 p += strlen(p);
233 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000234 if (iarg != 0) {
235 sprintf(p, "argument %d", iarg);
236 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000237 p += strlen(p);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000238 while (levels[i] > 0) {
239 sprintf(p, ", item %d", levels[i]-1);
240 p += strlen(p);
241 i++;
242 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000243 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000244 else {
245 sprintf(p, "argument");
246 p += strlen(p);
247 }
248 sprintf(p, " %s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000249 message = buf;
250 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000252}
253
254
255/* Convert a tuple argument.
256 On entry, *p_format points to the character _after_ the opening '('.
257 On successful exit, *p_format points to the closing ')'.
258 If successful:
259 *p_format and *p_va are updated,
260 *levels and *msgbuf are untouched,
261 and NULL is returned.
262 If the argument is invalid:
263 *p_format is unchanged,
264 *p_va is undefined,
265 *levels is a 0-terminated list of item numbers,
266 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000267 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000268 <typename1> is the name of the expected type, and
269 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000270 and msgbuf is returned.
271*/
272
273static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000274converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
275 char *msgbuf, int toplevel)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000276{
277 int level = 0;
278 int n = 0;
279 char *format = *p_format;
280 int i;
281
282 for (;;) {
283 int c = *format++;
284 if (c == '(') {
285 if (level == 0)
286 n++;
287 level++;
288 }
289 else if (c == ')') {
290 if (level == 0)
291 break;
292 level--;
293 }
294 else if (c == ':' || c == ';' || c == '\0')
295 break;
296 else if (level == 0 && isalpha(c))
297 n++;
298 }
299
Ka-Ping Yee20579702001-01-15 22:14:16 +0000300 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000301 levels[0] = 0;
302 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000303 toplevel ? "expected %d arguments, not %s" :
304 "must be %d-item sequence, not %s",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000305 n, arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000306 return msgbuf;
307 }
308
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000309 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000310 levels[0] = 0;
311 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +0000312 toplevel ? "expected %d arguments, not %d" :
313 "must be sequence of length %d, not %d",
314 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000315 return msgbuf;
316 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000317
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000318 format = *p_format;
319 for (i = 0; i < n; i++) {
320 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000321 PyObject *item;
322 item = PySequence_GetItem(arg, i);
323 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
324 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
325 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000326 if (msg != NULL) {
327 levels[0] = i+1;
328 return msg;
329 }
330 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000331
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000332 *p_format = format;
333 return NULL;
334}
335
336
337/* Convert a single item. */
338
339static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
341 char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000342{
343 char *msg;
344 char *format = *p_format;
345
346 if (*format == '(' /* ')' */) {
347 format++;
348 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
349 if (msg == NULL)
350 format++;
351 }
352 else {
353 msg = convertsimple(arg, &format, p_va, msgbuf);
354 if (msg != NULL)
355 levels[0] = 0;
356 }
357 if (msg == NULL)
358 *p_format = format;
359 return msg;
360}
361
362
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000363
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000364#define UNICODE_DEFAULT_ENCODING(arg) \
365 _PyUnicode_AsDefaultEncodedString(arg, NULL)
366
367/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000368
369static char *
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000370converterr(char *expected, PyObject *arg, char *msgbuf)
371{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000372 assert(expected != NULL);
373 assert(arg != NULL);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000374 sprintf(msgbuf, "must be %.50s, not %.50s", expected,
375 arg == Py_None ? "None" : arg->ob_type->tp_name);
376 return msgbuf;
377}
378
379#define CONV_UNICODE "(unicode conversion error)"
380
381/* Convert a non-tuple argument. Return NULL if conversion went OK,
382 or a string with a message describing the failure. The message is
383 formatted as "must be <desired type>, not <actual type>".
384 When failing, an exception may or may not have been raised.
385 Don't call if a tuple is expected.
386*/
387
388static char *
389convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000390{
391 char *format = *p_format;
392 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000393 PyObject *uarg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000394
395 switch (c) {
396
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000397 case 'b': { /* unsigned byte -- very short int */
398 char *p = va_arg(*p_va, char *);
399 long ival = PyInt_AsLong(arg);
400 if (ival == -1 && PyErr_Occurred())
401 return converterr("integer<b>", arg, msgbuf);
402 else if (ival < 0) {
403 PyErr_SetString(PyExc_OverflowError,
404 "unsigned byte integer is less than minimum");
405 return converterr("integer<b>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000406 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000407 else if (ival > UCHAR_MAX) {
408 PyErr_SetString(PyExc_OverflowError,
409 "unsigned byte integer is greater than maximum");
410 return converterr("integer<b>", arg, msgbuf);
411 }
412 else
413 *p = (unsigned char) ival;
414 break;
415 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000416
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000417 case 'B': {/* byte sized bitfield - both signed and unsigned
418 values allowed */
419 char *p = va_arg(*p_va, char *);
420 long ival = PyInt_AsLong(arg);
421 if (ival == -1 && PyErr_Occurred())
422 return converterr("integer<b>", arg, msgbuf);
423 else if (ival < SCHAR_MIN) {
424 PyErr_SetString(PyExc_OverflowError,
425 "byte-sized integer bitfield is less than minimum");
426 return converterr("integer<B>", arg, msgbuf);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000427 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000428 else if (ival > (int)UCHAR_MAX) {
429 PyErr_SetString(PyExc_OverflowError,
430 "byte-sized integer bitfield is greater than maximum");
431 return converterr("integer<B>", arg, msgbuf);
432 }
433 else
434 *p = (unsigned char) ival;
435 break;
436 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000437
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000438 case 'h': {/* signed short int */
439 short *p = va_arg(*p_va, short *);
440 long ival = PyInt_AsLong(arg);
441 if (ival == -1 && PyErr_Occurred())
442 return converterr("integer<h>", arg, msgbuf);
443 else if (ival < SHRT_MIN) {
444 PyErr_SetString(PyExc_OverflowError,
445 "signed short integer is less than minimum");
446 return converterr("integer<h>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000447 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000448 else if (ival > SHRT_MAX) {
449 PyErr_SetString(PyExc_OverflowError,
450 "signed short integer is greater than maximum");
451 return converterr("integer<h>", arg, msgbuf);
452 }
453 else
454 *p = (short) ival;
455 break;
456 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000457
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000458 case 'H': { /* short int sized bitfield, both signed and
459 unsigned allowed */
460 unsigned short *p = va_arg(*p_va, unsigned short *);
461 long ival = PyInt_AsLong(arg);
462 if (ival == -1 && PyErr_Occurred())
463 return converterr("integer<H>", arg, msgbuf);
464 else if (ival < SHRT_MIN) {
465 PyErr_SetString(PyExc_OverflowError,
466 "short integer bitfield is less than minimum");
467 return converterr("integer<H>", arg, msgbuf);
Jack Jansend50338f2000-07-06 12:22:00 +0000468 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000469 else if (ival > USHRT_MAX) {
470 PyErr_SetString(PyExc_OverflowError,
471 "short integer bitfield is greater than maximum");
472 return converterr("integer<H>", arg, msgbuf);
473 }
474 else
475 *p = (unsigned short) ival;
476 break;
477 }
Jack Jansend50338f2000-07-06 12:22:00 +0000478
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000479 case 'i': {/* signed int */
480 int *p = va_arg(*p_va, int *);
481 long ival = PyInt_AsLong(arg);
482 if (ival == -1 && PyErr_Occurred())
483 return converterr("integer<i>", arg, msgbuf);
484 else if (ival > INT_MAX) {
485 PyErr_SetString(PyExc_OverflowError,
486 "signed integer is greater than maximum");
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 if (ival < INT_MIN) {
490 PyErr_SetString(PyExc_OverflowError,
491 "signed integer is less than minimum");
492 return converterr("integer<i>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000493 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000494 else
495 *p = ival;
496 break;
497 }
498
499 case 'l': {/* long int */
500 long *p = va_arg(*p_va, long *);
501 long ival = PyInt_AsLong(arg);
502 if (ival == -1 && PyErr_Occurred())
503 return converterr("integer<l>", arg, msgbuf);
504 else
505 *p = ival;
506 break;
507 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000508
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000509#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000510 case 'L': {/* LONG_LONG */
511 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
512 LONG_LONG ival = PyLong_AsLongLong( arg );
513 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
514 return converterr("long<L>", arg, msgbuf);
515 } else {
516 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000517 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000518 break;
519 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000520#endif
521
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000522 case 'f': {/* float */
523 float *p = va_arg(*p_va, float *);
524 double dval = PyFloat_AsDouble(arg);
525 if (PyErr_Occurred())
526 return converterr("float<f>", arg, msgbuf);
527 else
528 *p = (float) dval;
529 break;
530 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000531
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000532 case 'd': {/* double */
533 double *p = va_arg(*p_va, double *);
534 double dval = PyFloat_AsDouble(arg);
535 if (PyErr_Occurred())
536 return converterr("float<d>", arg, msgbuf);
537 else
538 *p = dval;
539 break;
540 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000541
Guido van Rossum530956d1996-07-21 02:27:43 +0000542#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000543 case 'D': {/* complex double */
544 Py_complex *p = va_arg(*p_va, Py_complex *);
545 Py_complex cval;
546 cval = PyComplex_AsCComplex(arg);
547 if (PyErr_Occurred())
548 return converterr("complex<D>", arg, msgbuf);
549 else
550 *p = cval;
551 break;
552 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000553#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000554
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000555 case 'c': {/* char */
556 char *p = va_arg(*p_va, char *);
557 if (PyString_Check(arg) && PyString_Size(arg) == 1)
Jeremy Hylton0407aea2001-10-10 02:51:57 +0000558 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000559 else
560 return converterr("char", arg, msgbuf);
561 break;
562 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000563
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000564 case 's': {/* string */
565 if (*format == '#') {
566 void **p = (void **)va_arg(*p_va, char **);
567 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000568
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000569 if (PyString_Check(arg)) {
570 *p = PyString_AS_STRING(arg);
571 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000572 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000573#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000574 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000575 uarg = UNICODE_DEFAULT_ENCODING(arg);
576 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000577 return converterr(CONV_UNICODE,
578 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000579 *p = PyString_AS_STRING(uarg);
580 *q = PyString_GET_SIZE(uarg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000581 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000582#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000583 else { /* any buffer-like object */
584 char *buf;
585 int count = convertbuffer(arg, p, &buf);
586 if (count < 0)
587 return converterr(buf, arg, msgbuf);
588 *q = count;
589 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000590 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000591 } else {
592 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000593
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000594 if (PyString_Check(arg))
595 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000596#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000597 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000598 uarg = UNICODE_DEFAULT_ENCODING(arg);
599 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000600 return converterr(CONV_UNICODE,
601 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000602 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000603 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000604#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000605 else
606 return converterr("string", arg, msgbuf);
607 if ((int)strlen(*p) != PyString_Size(arg))
608 return converterr("string without null bytes",
609 arg, msgbuf);
610 }
611 break;
612 }
613
614 case 'z': {/* string, may be NULL (None) */
615 if (*format == '#') { /* any buffer-like object */
616 void **p = (void **)va_arg(*p_va, char **);
617 int *q = va_arg(*p_va, int *);
618
619 if (arg == Py_None) {
620 *p = 0;
621 *q = 0;
622 }
623 else if (PyString_Check(arg)) {
624 *p = PyString_AS_STRING(arg);
625 *q = PyString_GET_SIZE(arg);
626 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000627#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000628 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000629 uarg = UNICODE_DEFAULT_ENCODING(arg);
630 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000631 return converterr(CONV_UNICODE,
632 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000633 *p = PyString_AS_STRING(uarg);
634 *q = PyString_GET_SIZE(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000635 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000636#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000637 else { /* any buffer-like object */
638 char *buf;
639 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000640 if (count < 0)
641 return converterr(buf, arg, msgbuf);
642 *q = count;
643 }
644 format++;
645 } else {
646 char **p = va_arg(*p_va, char **);
647
648 if (arg == Py_None)
649 *p = 0;
650 else if (PyString_Check(arg))
Jeremy Hyltona4c8cd72001-10-10 02:51:08 +0000651 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000652#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000653 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000654 uarg = UNICODE_DEFAULT_ENCODING(arg);
655 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000656 return converterr(CONV_UNICODE,
657 arg, msgbuf);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000658 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000659 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000660#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000661 else
662 return converterr("string or None",
663 arg, msgbuf);
664 if (*format == '#') {
665 int *q = va_arg(*p_va, int *);
666 if (arg == Py_None)
667 *q = 0;
668 else
669 *q = PyString_Size(arg);
670 format++;
671 }
672 else if (*p != NULL &&
673 (int)strlen(*p) != PyString_Size(arg))
674 return converterr(
675 "string without null bytes or None",
676 arg, msgbuf);
677 }
678 break;
679 }
680
681 case 'e': {/* encoded string */
682 char **buffer;
683 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000684 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000685 int size, recode_strings;
686
687 /* Get 'e' parameter: the encoding name */
688 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000689#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000690 if (encoding == NULL)
691 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000692#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000693
694 /* Get output buffer parameter:
695 's' (recode all objects via Unicode) or
696 't' (only recode non-string objects)
697 */
698 if (*format == 's')
699 recode_strings = 1;
700 else if (*format == 't')
701 recode_strings = 0;
702 else
703 return converterr(
704 "(unknown parser marker combination)",
705 arg, msgbuf);
706 buffer = (char **)va_arg(*p_va, char **);
707 format++;
708 if (buffer == NULL)
709 return converterr("(buffer is NULL)",
710 arg, msgbuf);
711
712 /* Encode object */
713 if (!recode_strings && PyString_Check(arg)) {
714 s = arg;
715 Py_INCREF(s);
716 }
717 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000718#ifdef Py_USING_UNICODE
719 PyObject *u;
720
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000721 /* Convert object to Unicode */
722 u = PyUnicode_FromObject(arg);
723 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000724 return converterr(
725 "string or unicode or text buffer",
726 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000727
728 /* Encode object; use default error handling */
729 s = PyUnicode_AsEncodedString(u,
730 encoding,
731 NULL);
732 Py_DECREF(u);
733 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000734 return converterr("(encoding failed)",
735 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000736 if (!PyString_Check(s)) {
737 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000738 return converterr(
739 "(encoder failed to return a string)",
740 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000741 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000742#else
743 return converterr("string<e>", arg, msgbuf);
744#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000745 }
746 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000747
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000748 /* Write output; output is guaranteed to be 0-terminated */
749 if (*format == '#') {
750 /* Using buffer length parameter '#':
751
752 - if *buffer is NULL, a new buffer of the
753 needed size is allocated and the data
754 copied into it; *buffer is updated to point
755 to the new buffer; the caller is
756 responsible for PyMem_Free()ing it after
757 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000758
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000759 - if *buffer is not NULL, the data is
760 copied to *buffer; *buffer_len has to be
761 set to the size of the buffer on input;
762 buffer overflow is signalled with an error;
763 buffer has to provide enough room for the
764 encoded string plus the trailing 0-byte
765
766 - in both cases, *buffer_len is updated to
767 the size of the buffer /excluding/ the
768 trailing 0-byte
769
770 */
771 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000772
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000773 format++;
774 if (buffer_len == NULL)
775 return converterr(
776 "(buffer_len is NULL)",
777 arg, msgbuf);
778 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000779 *buffer = PyMem_NEW(char, size + 1);
780 if (*buffer == NULL) {
781 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000782 return converterr(
783 "(memory error)",
784 arg, msgbuf);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000785 }
Fred Drake25871c02000-05-03 15:17:02 +0000786 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000787 if (size + 1 > *buffer_len) {
788 Py_DECREF(s);
789 return converterr(
790 "(buffer overflow)",
791 arg, msgbuf);
792 }
Fred Drake25871c02000-05-03 15:17:02 +0000793 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000794 memcpy(*buffer,
795 PyString_AS_STRING(s),
796 size + 1);
797 *buffer_len = size;
798 } else {
799 /* Using a 0-terminated buffer:
800
801 - the encoded string has to be 0-terminated
802 for this variant to work; if it is not, an
803 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000804
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000805 - a new buffer of the needed size is
806 allocated and the data copied into it;
807 *buffer is updated to point to the new
808 buffer; the caller is responsible for
809 PyMem_Free()ing it after usage
810
811 */
812 if ((int)strlen(PyString_AS_STRING(s)) != size)
813 return converterr(
814 "(encoded string without NULL bytes)",
815 arg, msgbuf);
816 *buffer = PyMem_NEW(char, size + 1);
817 if (*buffer == NULL) {
818 Py_DECREF(s);
819 return converterr("(memory error)",
820 arg, msgbuf);
821 }
822 memcpy(*buffer,
823 PyString_AS_STRING(s),
824 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000825 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000826 Py_DECREF(s);
827 break;
828 }
829
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000830#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000831 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
832 if (*format == '#') { /* any buffer-like object */
833 void **p = (void **)va_arg(*p_va, char **);
834 int *q = va_arg(*p_va, int *);
835 char *buf;
836 int count = convertbuffer(arg, p, &buf);
837
838 if (count < 0)
839 return converterr(buf, arg, msgbuf);
840 *q = count/(sizeof(Py_UNICODE));
841 format++;
842 } else {
843 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
844
Guido van Rossume826ef02000-03-10 23:02:17 +0000845 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000846 *p = PyUnicode_AS_UNICODE(arg);
847 else
848 return converterr("unicode", arg, msgbuf);
849 }
850 break;
851 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000852#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000853
854 case 'S': { /* string object */
855 PyObject **p = va_arg(*p_va, PyObject **);
856 if (PyString_Check(arg))
857 *p = arg;
858 else
859 return converterr("string", arg, msgbuf);
860 break;
861 }
862
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000863#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000864 case 'U': { /* Unicode object */
865 PyObject **p = va_arg(*p_va, PyObject **);
866 if (PyUnicode_Check(arg))
867 *p = arg;
868 else
869 return converterr("unicode", arg, msgbuf);
870 break;
871 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000872#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000873
874 case 'O': { /* object */
875 PyTypeObject *type;
876 PyObject **p;
877 if (*format == '!') {
878 type = va_arg(*p_va, PyTypeObject*);
879 p = va_arg(*p_va, PyObject **);
880 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +0000881 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +0000882 *p = arg;
883 else
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000884 return converterr(type->tp_name, arg, msgbuf);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000885
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000886 }
887 else if (*format == '?') {
888 inquiry pred = va_arg(*p_va, inquiry);
889 p = va_arg(*p_va, PyObject **);
890 format++;
891 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000892 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000893 else
894 return converterr("(unspecified)",
895 arg, msgbuf);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000896
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000897 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000898 else if (*format == '&') {
899 typedef int (*converter)(PyObject *, void *);
900 converter convert = va_arg(*p_va, converter);
901 void *addr = va_arg(*p_va, void *);
902 format++;
903 if (! (*convert)(arg, addr))
904 return converterr("(unspecified)",
905 arg, msgbuf);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000906 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000907 else {
908 p = va_arg(*p_va, PyObject **);
909 *p = arg;
910 }
911 break;
912 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000913
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000914
915 case 'w': { /* memory buffer, read-write access */
916 void **p = va_arg(*p_va, void **);
917 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
918 int count;
919
920 if (pb == NULL ||
921 pb->bf_getwritebuffer == NULL ||
922 pb->bf_getsegcount == NULL)
923 return converterr("read-write buffer", arg, msgbuf);
924 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
925 return converterr("single-segment read-write buffer",
926 arg, msgbuf);
927 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
928 return converterr("(unspecified)", arg, msgbuf);
929 if (*format == '#') {
930 int *q = va_arg(*p_va, int *);
931
932 *q = count;
933 format++;
934 }
935 break;
936 }
937
938 case 't': { /* 8-bit character buffer, read-only access */
939 const char **p = va_arg(*p_va, const char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +0000940 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000941 int count;
942
943 if (*format++ != '#')
944 return converterr(
945 "invalid use of 't' format character",
946 arg, msgbuf);
947 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +0000948 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
949 pb == NULL || pb->bf_getcharbuffer == NULL ||
950 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000951 return converterr(
952 "string or read-only character buffer",
953 arg, msgbuf);
954
Jeremy Hylton4819e972001-10-11 14:40:37 +0000955 if (pb->bf_getsegcount(arg, NULL) != 1)
956 return converterr(
957 "string or single-segment read-only buffer",
958 arg, msgbuf);
959
960 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000961 if (count < 0)
Jeremy Hylton4819e972001-10-11 14:40:37 +0000962 return converterr("(unspecified)", arg, msgbuf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000963 *va_arg(*p_va, int *) = count;
964 break;
965 }
966
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000967 default:
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000968 return converterr("impossible<bad format char>", arg, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000969
970 }
971
972 *p_format = format;
973 return NULL;
974}
Guido van Rossumaa354651996-08-19 19:32:04 +0000975
Fred Drake563dfc22001-10-23 14:41:08 +0000976static int
977convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000978{
979 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
980 int count;
981 if (pb == NULL ||
982 pb->bf_getreadbuffer == NULL ||
983 pb->bf_getsegcount == NULL) {
984 *errmsg = "string or read-only buffer";
985 return -1;
986 }
987 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
988 *errmsg = "string or single-segment read-only buffer";
989 return -1;
990 }
991 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
992 *errmsg = "(unspecified)";
993 }
994 return count;
995}
Guido van Rossumaa354651996-08-19 19:32:04 +0000996
997/* Support for keyword arguments donated by
998 Geoff Philbrick <philbric@delphi.hks.com> */
999
Tim Petersf8cd3e82001-10-27 04:26:57 +00001000/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001001int
1002PyArg_ParseTupleAndKeywords(PyObject *args,
1003 PyObject *keywords,
1004 char *format,
1005 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001006{
1007 int retval;
1008 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001009
1010 if ((args == NULL || !PyTuple_Check(args)) ||
1011 (keywords != NULL && !PyDict_Check(keywords)) ||
1012 format == NULL ||
1013 kwlist == NULL)
1014 {
1015 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001016 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001017 }
1018
Guido van Rossumaa354651996-08-19 19:32:04 +00001019 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001020 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
1021 va_end(va);
1022 return retval;
1023}
1024
1025
1026static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001027vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
1028 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001029{
1030 char msgbuf[256];
1031 int levels[32];
Tim Petersf8cd3e82001-10-27 04:26:57 +00001032 char *fname, *message;
1033 int min, max;
Tim Peters6fb26352001-10-27 04:38:11 +00001034 char *formatsave;
1035 int i, len, nargs, nkeywords;
Guido van Rossumaa354651996-08-19 19:32:04 +00001036 char *msg, *ks, **p;
1037 int nkwds, pos, match, converted;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001038 PyObject *key, *value;
Tim Petersf4331c12001-10-27 00:17:34 +00001039
Tim Peters45772cd2001-10-27 03:58:40 +00001040 assert(args != NULL && PyTuple_Check(args));
1041 assert(keywords == NULL || PyDict_Check(keywords));
1042 assert(format != NULL);
1043 assert(kwlist != NULL);
1044 assert(p_va != NULL);
1045
Tim Petersf8cd3e82001-10-27 04:26:57 +00001046 /* Search the format:
1047 message <- error msg, if any (else NULL).
1048 name <- routine name, if any (else NULL).
1049 min <- # of required arguments, or -1 if all are required.
1050 max <- most arguments (required + optional).
1051 Raise error if a tuple arg spec is found.
1052 */
1053 fname = message = NULL;
Tim Peters6fb26352001-10-27 04:38:11 +00001054 formatsave = format;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001055 min = -1;
1056 max = 0;
1057 while ((i = *format++) != '\0') {
1058 if (isalpha(i) && i != 'e')
1059 max++;
1060 else if (i == '|')
1061 min = max;
1062 else if (i == ':') {
1063 fname = format;
1064 break;
1065 }
1066 else if (i == ';') {
1067 message = format;
1068 break;
1069 }
1070 else if (i == '(') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001071 PyErr_SetString(PyExc_SystemError,
1072 "tuple found in format when using keyword arguments");
1073 return 0;
1074 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001075 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001076 if (min < 0) {
1077 /* All arguments are required. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001078 min = max;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001079 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001080 format = formatsave;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001081
Tim Peters6fb26352001-10-27 04:38:11 +00001082 nargs = PyTuple_GET_SIZE(args);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001083
Guido van Rossumaa354651996-08-19 19:32:04 +00001084 /* do a cursory check of the keywords just to see how many we got */
Tim Petersf4331c12001-10-27 00:17:34 +00001085
Tim Peters6fb26352001-10-27 04:38:11 +00001086 nkeywords = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001087 if (keywords) {
1088 if (!PyDict_Check(keywords)) {
Tim Petersf4331c12001-10-27 00:17:34 +00001089 PyErr_Format(PyExc_SystemError,
1090 "%s received when keyword dictionary expected",
1091 keywords->ob_type->tp_name);
Guido van Rossumaa354651996-08-19 19:32:04 +00001092 return 0;
1093 }
Tim Peters6fb26352001-10-27 04:38:11 +00001094 nkeywords = PyDict_Size(keywords);
Guido van Rossumaa354651996-08-19 19:32:04 +00001095 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001096
1097 /* make sure there are no duplicate values for an argument;
1098 its not clear when to use the term "keyword argument vs.
1099 keyword parameter in messages */
Tim Petersa9f47392001-10-27 00:46:09 +00001100
Guido van Rossumaa354651996-08-19 19:32:04 +00001101 if (keywords) {
Tim Peters6fb26352001-10-27 04:38:11 +00001102 for (i = 0; i < nargs; i++) {
Tim Petersa9f47392001-10-27 00:46:09 +00001103 char *thiskw = kwlist[i];
1104 if (thiskw == NULL)
1105 break;
1106 if (PyMapping_HasKeyString(keywords, thiskw)) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001107 sprintf(msgbuf,
1108 "keyword parameter %s redefined",
Tim Petersa9f47392001-10-27 00:46:09 +00001109 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001110 PyErr_SetString(PyExc_TypeError, msgbuf);
1111 return 0;
1112 }
1113 }
1114 }
Tim Petersa9f47392001-10-27 00:46:09 +00001115 /* XXX The loop just above didn't used to break when hitting the
1116 end of kwlist, so could pass NULL on to PyMapping_HasKeyString,
1117 which sets a "NULL argument to internal routine" error then.
1118 However, the comment below doesn't give any clues about which
1119 'error string' it's talking about, so darned hard to say whether
1120 the PyErr_Clear() still serves a purpose.
1121 */
Guido van Rossumaa354651996-08-19 19:32:04 +00001122 PyErr_Clear(); /* I'm not which Py functions set the error string */
1123
1124 /* required arguments missing from args can be supplied by keyword
1125 arguments */
1126
Tim Peters6fb26352001-10-27 04:38:11 +00001127 len = nargs;
1128 if (keywords && nargs < min) {
1129 for (i = nargs; i < min; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001130 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1131 len++;
1132 }
1133 }
1134 }
1135 PyErr_Clear();
1136
1137 /* make sure we got an acceptable number of arguments; the message
1138 is a little confusing with keywords since keyword arguments
1139 which are supplied, but don't match the required arguments
1140 are not included in the "%d given" part of the message */
1141
1142 if (len < min || max < len) {
1143 if (message == NULL) {
1144 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001145 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001146 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001147 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001148 min==max ? "exactly"
1149 : len < min ? "at least" : "at most",
1150 len < min ? min : max,
1151 (len < min ? min : max) == 1 ? "" : "s",
1152 len);
1153 message = msgbuf;
1154 }
1155 PyErr_SetString(PyExc_TypeError, message);
1156 return 0;
1157 }
1158
Tim Peters6fb26352001-10-27 04:38:11 +00001159 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001160 if (*format == '|')
1161 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001162 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001163 levels, msgbuf);
1164 if (msg) {
1165 seterror(i+1, msg, levels, fname, message);
1166 return 0;
1167 }
1168 }
1169
1170 /* handle no keyword parameters in call */
1171
Tim Peters28bf7a92001-10-27 04:33:41 +00001172 if (!keywords)
1173 return 1;
Guido van Rossumaa354651996-08-19 19:32:04 +00001174
1175 /* make sure the number of keywords in the keyword list matches the
1176 number of items in the format string */
1177
1178 nkwds = 0;
1179 p = kwlist;
1180 for (;;) {
1181 if (!*(p++)) break;
1182 nkwds++;
1183 }
1184
1185 if (nkwds != max) {
1186 PyErr_SetString(PyExc_SystemError,
1187 "number of items in format string and keyword list do not match");
1188 return 0;
1189 }
1190
1191 /* convert the keyword arguments; this uses the format
1192 string where it was left after processing args */
1193
1194 converted = 0;
Tim Peters6fb26352001-10-27 04:38:11 +00001195 for (i = nargs; i < nkwds; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001196 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001197 if (*format == '|')
1198 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001199 item = PyMapping_GetItemString(keywords, kwlist[i]);
1200 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001201 msg = convertitem(item, &format, p_va, levels, msgbuf);
1202 if (msg) {
1203 seterror(i+1, msg, levels, fname, message);
1204 return 0;
1205 }
1206 converted++;
Barry Warsaw07050282000-12-11 20:01:55 +00001207 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001208 }
1209 else {
1210 PyErr_Clear();
1211 msg = skipitem(&format, p_va);
1212 if (msg) {
1213 seterror(i+1, msg, levels, fname, message);
1214 return 0;
1215 }
1216 }
1217 }
1218
1219 /* make sure there are no extraneous keyword arguments */
1220
1221 pos = 0;
Tim Peters6fb26352001-10-27 04:38:11 +00001222 if (converted < nkeywords) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001223 while (PyDict_Next(keywords, &pos, &key, &value)) {
1224 match = 0;
1225 ks = PyString_AsString(key);
1226 for (i = 0; i < nkwds; i++) {
1227 if (!strcmp(ks, kwlist[i])) {
1228 match = 1;
1229 break;
1230 }
1231 }
1232 if (!match) {
1233 sprintf(msgbuf,
Guido van Rossum80dc16b2000-05-08 14:02:41 +00001234 "%s is an invalid keyword argument for this function",
Guido van Rossumaa354651996-08-19 19:32:04 +00001235 ks);
1236 PyErr_SetString(PyExc_TypeError, msgbuf);
1237 return 0;
1238 }
1239 }
1240 }
1241
1242 return 1;
1243}
1244
1245
1246static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001247skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001248{
1249 char *format = *p_format;
1250 char c = *format++;
1251
1252 switch (c) {
1253
1254 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001255 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001256 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001257 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001258 break;
1259 }
1260
1261 case 'h': /* short int */
1262 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001263 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001264 break;
1265 }
1266
Jack Jansencc22fbe2000-08-05 21:29:58 +00001267 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001268 {
1269 (void) va_arg(*p_va, unsigned short *);
1270 break;
1271 }
1272
Guido van Rossumaa354651996-08-19 19:32:04 +00001273 case 'i': /* int */
1274 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001275 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001276 break;
1277 }
1278
1279 case 'l': /* long int */
1280 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001281 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001282 break;
1283 }
1284
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001285#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001286 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001287 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001288 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001289 break;
1290 }
1291#endif
1292
Guido van Rossumaa354651996-08-19 19:32:04 +00001293 case 'f': /* float */
1294 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001295 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001296 break;
1297 }
1298
1299 case 'd': /* double */
1300 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001301 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001302 break;
1303 }
1304
1305#ifndef WITHOUT_COMPLEX
1306 case 'D': /* complex double */
1307 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001308 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001309 break;
1310 }
1311#endif /* WITHOUT_COMPLEX */
1312
1313 case 'c': /* char */
1314 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001315 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001316 break;
1317 }
1318
1319 case 's': /* string */
1320 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001321 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001322 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001323 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001324 format++;
1325 }
1326 break;
1327 }
1328
1329 case 'z': /* string */
1330 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001331 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001332 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001333 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001334 format++;
1335 }
1336 break;
1337 }
1338
1339 case 'S': /* string object */
1340 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001341 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001342 break;
1343 }
1344
1345 case 'O': /* object */
1346 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001347 if (*format == '!') {
1348 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001349 (void) va_arg(*p_va, PyTypeObject*);
1350 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001351 }
1352#if 0
1353/* I don't know what this is for */
1354 else if (*format == '?') {
1355 inquiry pred = va_arg(*p_va, inquiry);
1356 format++;
1357 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001358 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001359 }
1360 }
1361#endif
1362 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001363 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001364 (void) va_arg(*p_va, converter);
1365 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001366 format++;
1367 }
1368 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001369 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001370 }
1371 break;
1372 }
1373
1374 default:
1375 return "impossible<bad format char>";
1376
1377 }
1378
1379 *p_format = format;
1380 return NULL;
1381}
Fred Drakee4616e62001-10-23 21:09:29 +00001382
1383
1384int
1385PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
1386{
1387 int i, l;
1388 PyObject **o;
1389 va_list vargs;
1390
1391#ifdef HAVE_STDARG_PROTOTYPES
1392 va_start(vargs, max);
1393#else
1394 va_start(vargs);
1395#endif
1396
1397 assert(min >= 0);
1398 assert(min <= max);
1399 if (!PyTuple_Check(args)) {
1400 PyErr_SetString(PyExc_SystemError,
1401 "PyArg_UnpackTuple() argument list is not a tuple");
1402 return 0;
1403 }
1404 l = PyTuple_GET_SIZE(args);
1405 if (l < min) {
1406 if (name != NULL)
1407 PyErr_Format(
1408 PyExc_TypeError,
1409 "%s expected %s%d arguments, got %d",
1410 name, (min == max ? "" : "at least "), min, l);
1411 else
1412 PyErr_Format(
1413 PyExc_TypeError,
1414 "unpacked tuple should have %s%d elements,"
1415 " but has %d",
1416 (min == max ? "" : "at least "), min, l);
1417 va_end(vargs);
1418 return 0;
1419 }
1420 if (l > max) {
1421 if (name != NULL)
1422 PyErr_Format(
1423 PyExc_TypeError,
1424 "%s expected %s%d arguments, got %d",
1425 name, (min == max ? "" : "at most "), max, l);
1426 else
1427 PyErr_Format(
1428 PyExc_TypeError,
1429 "unpacked tuple should have %s%d elements,"
1430 " but has %d",
1431 (min == max ? "" : "at most "), max, l);
1432 va_end(vargs);
1433 return 0;
1434 }
1435 for (i = 0; i < l; i++) {
1436 o = va_arg(vargs, PyObject **);
1437 *o = PyTuple_GET_ITEM(args, i);
1438 }
1439 va_end(vargs);
1440 return 1;
1441}