blob: 9796b5e8393a4c0246c72104c96cbd09dfe330b4 [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;
Tim Petersb0872fc2001-10-27 04:45:34 +00001037 int nkwlist, 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 Petersb0872fc2001-10-27 04:45:34 +00001083 nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001084
Guido van Rossumaa354651996-08-19 19:32:04 +00001085 /* make sure there are no duplicate values for an argument;
1086 its not clear when to use the term "keyword argument vs.
1087 keyword parameter in messages */
Tim Petersb054be42001-10-27 05:07:41 +00001088 if (nkeywords > 0) {
Tim Peters6fb26352001-10-27 04:38:11 +00001089 for (i = 0; i < nargs; i++) {
Tim Petersa9f47392001-10-27 00:46:09 +00001090 char *thiskw = kwlist[i];
1091 if (thiskw == NULL)
1092 break;
1093 if (PyMapping_HasKeyString(keywords, thiskw)) {
Tim Petersb054be42001-10-27 05:07:41 +00001094 PyErr_Format(PyExc_TypeError,
1095 "keyword parameter '%s' was given "
1096 "by position and by name",
Tim Petersa9f47392001-10-27 00:46:09 +00001097 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001098 return 0;
1099 }
1100 }
1101 }
Tim Petersa9f47392001-10-27 00:46:09 +00001102 /* XXX The loop just above didn't used to break when hitting the
1103 end of kwlist, so could pass NULL on to PyMapping_HasKeyString,
1104 which sets a "NULL argument to internal routine" error then.
1105 However, the comment below doesn't give any clues about which
1106 'error string' it's talking about, so darned hard to say whether
1107 the PyErr_Clear() still serves a purpose.
1108 */
Guido van Rossumaa354651996-08-19 19:32:04 +00001109 PyErr_Clear(); /* I'm not which Py functions set the error string */
1110
1111 /* required arguments missing from args can be supplied by keyword
1112 arguments */
1113
Tim Peters6fb26352001-10-27 04:38:11 +00001114 len = nargs;
1115 if (keywords && nargs < min) {
1116 for (i = nargs; i < min; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001117 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1118 len++;
1119 }
1120 }
1121 }
1122 PyErr_Clear();
1123
1124 /* make sure we got an acceptable number of arguments; the message
1125 is a little confusing with keywords since keyword arguments
1126 which are supplied, but don't match the required arguments
1127 are not included in the "%d given" part of the message */
1128
1129 if (len < min || max < len) {
1130 if (message == NULL) {
1131 sprintf(msgbuf,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001132 "%s%s takes %s %d argument%s (%d given)",
Guido van Rossumaa354651996-08-19 19:32:04 +00001133 fname==NULL ? "function" : fname,
Ka-Ping Yee20579702001-01-15 22:14:16 +00001134 fname==NULL ? "" : "()",
Guido van Rossumaa354651996-08-19 19:32:04 +00001135 min==max ? "exactly"
1136 : len < min ? "at least" : "at most",
1137 len < min ? min : max,
1138 (len < min ? min : max) == 1 ? "" : "s",
1139 len);
1140 message = msgbuf;
1141 }
1142 PyErr_SetString(PyExc_TypeError, message);
1143 return 0;
1144 }
1145
Tim Peters6fb26352001-10-27 04:38:11 +00001146 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001147 if (*format == '|')
1148 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001149 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossumaa354651996-08-19 19:32:04 +00001150 levels, msgbuf);
1151 if (msg) {
1152 seterror(i+1, msg, levels, fname, message);
1153 return 0;
1154 }
1155 }
1156
1157 /* handle no keyword parameters in call */
Tim Petersb054be42001-10-27 05:07:41 +00001158 if (nkeywords == 0)
Tim Peters28bf7a92001-10-27 04:33:41 +00001159 return 1;
Tim Petersb054be42001-10-27 05:07:41 +00001160
Guido van Rossumaa354651996-08-19 19:32:04 +00001161 /* make sure the number of keywords in the keyword list matches the
1162 number of items in the format string */
Tim Petersb0872fc2001-10-27 04:45:34 +00001163 nkwlist = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001164 p = kwlist;
Tim Petersb054be42001-10-27 05:07:41 +00001165 while (*p++)
Tim Petersb0872fc2001-10-27 04:45:34 +00001166 nkwlist++;
Tim Petersb0872fc2001-10-27 04:45:34 +00001167 if (nkwlist != max) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001168 PyErr_SetString(PyExc_SystemError,
1169 "number of items in format string and keyword list do not match");
1170 return 0;
1171 }
Tim Petersb054be42001-10-27 05:07:41 +00001172
Guido van Rossumaa354651996-08-19 19:32:04 +00001173 /* convert the keyword arguments; this uses the format
1174 string where it was left after processing args */
Guido van Rossumaa354651996-08-19 19:32:04 +00001175 converted = 0;
Tim Petersb0872fc2001-10-27 04:45:34 +00001176 for (i = nargs; i < nkwlist; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001178 if (*format == '|')
1179 format++;
Guido van Rossum80bb9651996-12-05 23:27:02 +00001180 item = PyMapping_GetItemString(keywords, kwlist[i]);
1181 if (item != NULL) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001182 msg = convertitem(item, &format, p_va, levels, msgbuf);
1183 if (msg) {
1184 seterror(i+1, msg, levels, fname, message);
1185 return 0;
1186 }
1187 converted++;
Barry Warsaw07050282000-12-11 20:01:55 +00001188 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001189 }
1190 else {
1191 PyErr_Clear();
1192 msg = skipitem(&format, p_va);
1193 if (msg) {
1194 seterror(i+1, msg, levels, fname, message);
1195 return 0;
1196 }
1197 }
1198 }
Tim Petersb054be42001-10-27 05:07:41 +00001199
Guido van Rossumaa354651996-08-19 19:32:04 +00001200 /* make sure there are no extraneous keyword arguments */
Guido van Rossumaa354651996-08-19 19:32:04 +00001201 pos = 0;
Tim Peters6fb26352001-10-27 04:38:11 +00001202 if (converted < nkeywords) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001203 while (PyDict_Next(keywords, &pos, &key, &value)) {
1204 match = 0;
1205 ks = PyString_AsString(key);
Tim Petersb0872fc2001-10-27 04:45:34 +00001206 for (i = 0; i < nkwlist; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001207 if (!strcmp(ks, kwlist[i])) {
1208 match = 1;
1209 break;
1210 }
1211 }
1212 if (!match) {
1213 sprintf(msgbuf,
Guido van Rossum80dc16b2000-05-08 14:02:41 +00001214 "%s is an invalid keyword argument for this function",
Guido van Rossumaa354651996-08-19 19:32:04 +00001215 ks);
1216 PyErr_SetString(PyExc_TypeError, msgbuf);
1217 return 0;
1218 }
1219 }
1220 }
1221
1222 return 1;
1223}
1224
1225
1226static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001227skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001228{
1229 char *format = *p_format;
1230 char c = *format++;
1231
1232 switch (c) {
1233
1234 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001235 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001236 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001237 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001238 break;
1239 }
1240
1241 case 'h': /* short int */
1242 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001243 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001244 break;
1245 }
1246
Jack Jansencc22fbe2000-08-05 21:29:58 +00001247 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001248 {
1249 (void) va_arg(*p_va, unsigned short *);
1250 break;
1251 }
1252
Guido van Rossumaa354651996-08-19 19:32:04 +00001253 case 'i': /* int */
1254 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001255 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001256 break;
1257 }
1258
1259 case 'l': /* long int */
1260 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001261 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001262 break;
1263 }
1264
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001265#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001266 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001267 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001268 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001269 break;
1270 }
1271#endif
1272
Guido van Rossumaa354651996-08-19 19:32:04 +00001273 case 'f': /* float */
1274 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001275 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001276 break;
1277 }
1278
1279 case 'd': /* double */
1280 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001281 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001282 break;
1283 }
1284
1285#ifndef WITHOUT_COMPLEX
1286 case 'D': /* complex double */
1287 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001288 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001289 break;
1290 }
1291#endif /* WITHOUT_COMPLEX */
1292
1293 case 'c': /* char */
1294 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001295 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001296 break;
1297 }
1298
1299 case 's': /* string */
1300 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001301 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001302 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001303 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001304 format++;
1305 }
1306 break;
1307 }
1308
1309 case 'z': /* string */
1310 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001311 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001312 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001313 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001314 format++;
1315 }
1316 break;
1317 }
1318
1319 case 'S': /* string object */
1320 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001321 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001322 break;
1323 }
1324
1325 case 'O': /* object */
1326 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001327 if (*format == '!') {
1328 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001329 (void) va_arg(*p_va, PyTypeObject*);
1330 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001331 }
1332#if 0
1333/* I don't know what this is for */
1334 else if (*format == '?') {
1335 inquiry pred = va_arg(*p_va, inquiry);
1336 format++;
1337 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001338 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001339 }
1340 }
1341#endif
1342 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001343 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001344 (void) va_arg(*p_va, converter);
1345 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001346 format++;
1347 }
1348 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001349 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001350 }
1351 break;
1352 }
1353
1354 default:
1355 return "impossible<bad format char>";
1356
1357 }
1358
1359 *p_format = format;
1360 return NULL;
1361}
Fred Drakee4616e62001-10-23 21:09:29 +00001362
1363
1364int
1365PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
1366{
1367 int i, l;
1368 PyObject **o;
1369 va_list vargs;
1370
1371#ifdef HAVE_STDARG_PROTOTYPES
1372 va_start(vargs, max);
1373#else
1374 va_start(vargs);
1375#endif
1376
1377 assert(min >= 0);
1378 assert(min <= max);
1379 if (!PyTuple_Check(args)) {
1380 PyErr_SetString(PyExc_SystemError,
1381 "PyArg_UnpackTuple() argument list is not a tuple");
1382 return 0;
1383 }
1384 l = PyTuple_GET_SIZE(args);
1385 if (l < min) {
1386 if (name != NULL)
1387 PyErr_Format(
1388 PyExc_TypeError,
1389 "%s expected %s%d arguments, got %d",
1390 name, (min == max ? "" : "at least "), min, l);
1391 else
1392 PyErr_Format(
1393 PyExc_TypeError,
1394 "unpacked tuple should have %s%d elements,"
1395 " but has %d",
1396 (min == max ? "" : "at least "), min, l);
1397 va_end(vargs);
1398 return 0;
1399 }
1400 if (l > max) {
1401 if (name != NULL)
1402 PyErr_Format(
1403 PyExc_TypeError,
1404 "%s expected %s%d arguments, got %d",
1405 name, (min == max ? "" : "at most "), max, l);
1406 else
1407 PyErr_Format(
1408 PyExc_TypeError,
1409 "unpacked tuple should have %s%d elements,"
1410 " but has %d",
1411 (min == max ? "" : "at most "), max, l);
1412 va_end(vargs);
1413 return 0;
1414 }
1415 for (i = 0; i < l; i++) {
1416 o = va_arg(vargs, PyObject **);
1417 *o = PyTuple_GET_ITEM(args, i);
1418 }
1419 va_end(vargs);
1420 return 1;
1421}