blob: 248def36cab44b8948fd31a3b0e8ac89026b0be8 [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{
Tim Petersdc5eff92001-10-27 06:53:00 +00001030 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001031 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;
Tim Petersb639d492001-10-27 07:00:56 +00001035 int i, len, nargs, nkeywords;
Tim Petersc2f01122001-10-27 07:25:06 +00001036 char *msg, **p;
Tim Petersf4331c12001-10-27 00:17:34 +00001037
Tim Peters45772cd2001-10-27 03:58:40 +00001038 assert(args != NULL && PyTuple_Check(args));
1039 assert(keywords == NULL || PyDict_Check(keywords));
1040 assert(format != NULL);
1041 assert(kwlist != NULL);
1042 assert(p_va != NULL);
1043
Tim Petersf8cd3e82001-10-27 04:26:57 +00001044 /* Search the format:
1045 message <- error msg, if any (else NULL).
1046 name <- routine name, if any (else NULL).
1047 min <- # of required arguments, or -1 if all are required.
1048 max <- most arguments (required + optional).
Tim Peters62d48e12001-10-27 06:42:16 +00001049 Check that kwlist has a non-NULL entry for each arg.
Tim Petersf8cd3e82001-10-27 04:26:57 +00001050 Raise error if a tuple arg spec is found.
1051 */
1052 fname = message = NULL;
Tim Peters6fb26352001-10-27 04:38:11 +00001053 formatsave = format;
Tim Peters62d48e12001-10-27 06:42:16 +00001054 p = kwlist;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001055 min = -1;
1056 max = 0;
1057 while ((i = *format++) != '\0') {
Tim Peters62d48e12001-10-27 06:42:16 +00001058 if (isalpha(i) && i != 'e') {
Tim Petersf8cd3e82001-10-27 04:26:57 +00001059 max++;
Tim Peters62d48e12001-10-27 06:42:16 +00001060 if (*p == NULL) {
1061 /* kwlist is too short */
1062 PyErr_BadInternalCall();
1063 return 0;
1064 }
1065 p++;
1066 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001067 else if (i == '|')
1068 min = max;
1069 else if (i == ':') {
1070 fname = format;
1071 break;
1072 }
1073 else if (i == ';') {
1074 message = format;
1075 break;
1076 }
1077 else if (i == '(') {
Guido van Rossumaa354651996-08-19 19:32:04 +00001078 PyErr_SetString(PyExc_SystemError,
1079 "tuple found in format when using keyword arguments");
1080 return 0;
1081 }
Tim Peters62d48e12001-10-27 06:42:16 +00001082 }
1083 format = formatsave;
1084 if (*p != NULL) {
1085 /* kwlist is too long */
1086 PyErr_BadInternalCall();
1087 return 0;
1088 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001089 if (min < 0) {
1090 /* All arguments are required. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001091 min = max;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001092 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001093
Tim Peters6fb26352001-10-27 04:38:11 +00001094 nargs = PyTuple_GET_SIZE(args);
Tim Petersb0872fc2001-10-27 04:45:34 +00001095 nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001096
Guido van Rossumaa354651996-08-19 19:32:04 +00001097 /* 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 Petersb054be42001-10-27 05:07:41 +00001100 if (nkeywords > 0) {
Tim Peters6fb26352001-10-27 04:38:11 +00001101 for (i = 0; i < nargs; i++) {
Tim Petersa9f47392001-10-27 00:46:09 +00001102 char *thiskw = kwlist[i];
1103 if (thiskw == NULL)
1104 break;
Tim Peters077f5742001-10-27 05:50:39 +00001105 if (PyDict_GetItemString(keywords, thiskw)) {
Tim Petersb054be42001-10-27 05:07:41 +00001106 PyErr_Format(PyExc_TypeError,
1107 "keyword parameter '%s' was given "
1108 "by position and by name",
Tim Petersa9f47392001-10-27 00:46:09 +00001109 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001110 return 0;
1111 }
Tim Peters0af49162001-10-27 06:14:32 +00001112 else if (PyErr_Occurred())
1113 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001114 }
1115 }
Tim Peters61dde632001-10-27 05:30:17 +00001116
Guido van Rossumaa354651996-08-19 19:32:04 +00001117 /* required arguments missing from args can be supplied by keyword
Tim Peters62d48e12001-10-27 06:42:16 +00001118 arguments; set len to the number of posiitional arguments, and,
1119 if that's less than the minimum required, add in the number of
1120 required arguments that are supplied by keywords */
Tim Peters6fb26352001-10-27 04:38:11 +00001121 len = nargs;
Tim Peters62d48e12001-10-27 06:42:16 +00001122 if (nkeywords > 0 && nargs < min) {
Tim Peters6fb26352001-10-27 04:38:11 +00001123 for (i = nargs; i < min; i++) {
Tim Peters077f5742001-10-27 05:50:39 +00001124 if (PyDict_GetItemString(keywords, kwlist[i]))
Guido van Rossumaa354651996-08-19 19:32:04 +00001125 len++;
Tim Peters0af49162001-10-27 06:14:32 +00001126 else if (PyErr_Occurred())
1127 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001128 }
1129 }
Tim Peters0af49162001-10-27 06:14:32 +00001130
Guido van Rossumaa354651996-08-19 19:32:04 +00001131 /* make sure we got an acceptable number of arguments; the message
1132 is a little confusing with keywords since keyword arguments
1133 which are supplied, but don't match the required arguments
1134 are not included in the "%d given" part of the message */
Guido van Rossumaa354651996-08-19 19:32:04 +00001135 if (len < min || max < len) {
1136 if (message == NULL) {
1137 sprintf(msgbuf,
Tim Petersdc5eff92001-10-27 06:53:00 +00001138 "%.200s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001139 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001140 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001141 min==max ? "exactly"
1142 : len < min ? "at least" : "at most",
1143 len < min ? min : max,
1144 (len < min ? min : max) == 1 ? "" : "s",
1145 len);
1146 message = msgbuf;
1147 }
1148 PyErr_SetString(PyExc_TypeError, message);
1149 return 0;
1150 }
Tim Petersc2f01122001-10-27 07:25:06 +00001151
1152 /* convert the positional arguments */
Tim Peters6fb26352001-10-27 04:38:11 +00001153 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001154 if (*format == '|')
1155 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001156 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001157 levels, msgbuf);
1158 if (msg) {
1159 seterror(i+1, msg, levels, fname, message);
1160 return 0;
1161 }
1162 }
1163
Tim Petersc2f01122001-10-27 07:25:06 +00001164 /* handle no keyword parameters in call */
Tim Petersb054be42001-10-27 05:07:41 +00001165 if (nkeywords == 0)
Tim Peters28bf7a92001-10-27 04:33:41 +00001166 return 1;
Tim Petersb054be42001-10-27 05:07:41 +00001167
Guido van Rossumaa354651996-08-19 19:32:04 +00001168 /* convert the keyword arguments; this uses the format
1169 string where it was left after processing args */
Tim Petersb639d492001-10-27 07:00:56 +00001170 for (i = nargs; i < max; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001171 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001172 if (*format == '|')
1173 format++;
Tim Peters077f5742001-10-27 05:50:39 +00001174 item = PyDict_GetItemString(keywords, kwlist[i]);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001175 if (item != NULL) {
Tim Peters077f5742001-10-27 05:50:39 +00001176 Py_INCREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001177 msg = convertitem(item, &format, p_va, levels, msgbuf);
Tim Peters077f5742001-10-27 05:50:39 +00001178 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001179 if (msg) {
1180 seterror(i+1, msg, levels, fname, message);
1181 return 0;
1182 }
Tim Petersc2f01122001-10-27 07:25:06 +00001183 --nkeywords;
1184 if (nkeywords == 0)
1185 break;
Guido van Rossumaa354651996-08-19 19:32:04 +00001186 }
Tim Peters0af49162001-10-27 06:14:32 +00001187 else if (PyErr_Occurred())
1188 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001189 else {
Guido van Rossumaa354651996-08-19 19:32:04 +00001190 msg = skipitem(&format, p_va);
1191 if (msg) {
1192 seterror(i+1, msg, levels, fname, message);
1193 return 0;
1194 }
1195 }
1196 }
Tim Petersb054be42001-10-27 05:07:41 +00001197
Guido van Rossumaa354651996-08-19 19:32:04 +00001198 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001199 if (nkeywords > 0) {
1200 PyObject *key, *value;
1201 int pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001202 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001203 int match = 0;
1204 char *ks = PyString_AsString(key);
Tim Petersb639d492001-10-27 07:00:56 +00001205 for (i = 0; i < max; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001206 if (!strcmp(ks, kwlist[i])) {
1207 match = 1;
1208 break;
1209 }
1210 }
1211 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001212 PyErr_Format(PyExc_TypeError,
1213 "'%s' is an invalid keyword "
1214 "argument for this function",
1215 ks);
Guido van Rossumaa354651996-08-19 19:32:04 +00001216 return 0;
1217 }
1218 }
1219 }
Tim Petersc2f01122001-10-27 07:25:06 +00001220
Guido van Rossumaa354651996-08-19 19:32:04 +00001221 return 1;
1222}
1223
1224
1225static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001226skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001227{
1228 char *format = *p_format;
1229 char c = *format++;
1230
1231 switch (c) {
1232
1233 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001234 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001235 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001236 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001237 break;
1238 }
1239
1240 case 'h': /* short int */
1241 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001242 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001243 break;
1244 }
1245
Jack Jansencc22fbe2000-08-05 21:29:58 +00001246 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001247 {
1248 (void) va_arg(*p_va, unsigned short *);
1249 break;
1250 }
1251
Guido van Rossumaa354651996-08-19 19:32:04 +00001252 case 'i': /* int */
1253 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001254 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001255 break;
1256 }
1257
1258 case 'l': /* long int */
1259 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001260 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001261 break;
1262 }
1263
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001264#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001265 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001266 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001267 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001268 break;
1269 }
1270#endif
1271
Guido van Rossumaa354651996-08-19 19:32:04 +00001272 case 'f': /* float */
1273 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001274 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001275 break;
1276 }
1277
1278 case 'd': /* double */
1279 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001280 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001281 break;
1282 }
1283
1284#ifndef WITHOUT_COMPLEX
1285 case 'D': /* complex double */
1286 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001287 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001288 break;
1289 }
1290#endif /* WITHOUT_COMPLEX */
1291
1292 case 'c': /* char */
1293 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001294 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001295 break;
1296 }
1297
1298 case 's': /* string */
1299 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001300 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001301 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001302 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001303 format++;
1304 }
1305 break;
1306 }
1307
1308 case 'z': /* string */
1309 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001310 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001311 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001312 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001313 format++;
1314 }
1315 break;
1316 }
1317
1318 case 'S': /* string object */
1319 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001320 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001321 break;
1322 }
1323
1324 case 'O': /* object */
1325 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001326 if (*format == '!') {
1327 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001328 (void) va_arg(*p_va, PyTypeObject*);
1329 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001330 }
1331#if 0
1332/* I don't know what this is for */
1333 else if (*format == '?') {
1334 inquiry pred = va_arg(*p_va, inquiry);
1335 format++;
1336 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001337 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001338 }
1339 }
1340#endif
1341 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001342 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001343 (void) va_arg(*p_va, converter);
1344 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001345 format++;
1346 }
1347 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001348 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001349 }
1350 break;
1351 }
1352
1353 default:
1354 return "impossible<bad format char>";
1355
1356 }
1357
1358 *p_format = format;
1359 return NULL;
1360}
Fred Drakee4616e62001-10-23 21:09:29 +00001361
1362
1363int
1364PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
1365{
1366 int i, l;
1367 PyObject **o;
1368 va_list vargs;
1369
1370#ifdef HAVE_STDARG_PROTOTYPES
1371 va_start(vargs, max);
1372#else
1373 va_start(vargs);
1374#endif
1375
1376 assert(min >= 0);
1377 assert(min <= max);
1378 if (!PyTuple_Check(args)) {
1379 PyErr_SetString(PyExc_SystemError,
1380 "PyArg_UnpackTuple() argument list is not a tuple");
1381 return 0;
1382 }
1383 l = PyTuple_GET_SIZE(args);
1384 if (l < min) {
1385 if (name != NULL)
1386 PyErr_Format(
1387 PyExc_TypeError,
1388 "%s expected %s%d arguments, got %d",
1389 name, (min == max ? "" : "at least "), min, l);
1390 else
1391 PyErr_Format(
1392 PyExc_TypeError,
1393 "unpacked tuple should have %s%d elements,"
1394 " but has %d",
1395 (min == max ? "" : "at least "), min, l);
1396 va_end(vargs);
1397 return 0;
1398 }
1399 if (l > max) {
1400 if (name != NULL)
1401 PyErr_Format(
1402 PyExc_TypeError,
1403 "%s expected %s%d arguments, got %d",
1404 name, (min == max ? "" : "at most "), max, l);
1405 else
1406 PyErr_Format(
1407 PyExc_TypeError,
1408 "unpacked tuple should have %s%d elements,"
1409 " but has %d",
1410 (min == max ? "" : "at most "), max, l);
1411 va_end(vargs);
1412 return 0;
1413 }
1414 for (i = 0; i < l; i++) {
1415 o = va_arg(vargs, PyObject **);
1416 *o = PyTuple_GET_ITEM(args, i);
1417 }
1418 va_end(vargs);
1419 return 1;
1420}