blob: 72194a86a0e3cc148672df75f41ae3c3023707c5 [file] [log] [blame]
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001
2/* New getargs implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00005
Guido van Rossumc1d50531996-08-21 23:38:24 +00006#include <ctype.h>
7
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00008
Tim Petersdbd9ba62000-07-09 03:09:57 +00009int PyArg_Parse(PyObject *, char *, ...);
10int PyArg_ParseTuple(PyObject *, char *, ...);
11int PyArg_VaParse(PyObject *, char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000012
Tim Petersdbd9ba62000-07-09 03:09:57 +000013int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
14 char *, char **, ...);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000015
16/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000017static int vgetargs1(PyObject *, char *, va_list *, int);
18static void seterror(int, char *, int *, char *, char *);
Jeremy Hyltonb048b262001-11-28 22:14:37 +000019static char *convertitem(PyObject *, char **, va_list *, int *, char *,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000020 size_t, PyObject **);
Tim Petersdbd9ba62000-07-09 03:09:57 +000021static char *converttuple(PyObject *, char **, va_list *,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000022 int *, char *, size_t, int, PyObject **);
23static char *convertsimple(PyObject *, char **, va_list *, char *,
24 size_t, PyObject **);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +000025static int convertbuffer(PyObject *, void **p, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000026
Tim Petersdbd9ba62000-07-09 03:09:57 +000027static int vgetargskeywords(PyObject *, PyObject *,
28 char *, char **, va_list *);
29static char *skipitem(char **, va_list *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000030
Fred Drake563dfc22001-10-23 14:41:08 +000031int
32PyArg_Parse(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000033{
34 int retval;
35 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000036
37 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000038 retval = vgetargs1(args, format, &va, 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000039 va_end(va);
40 return retval;
41}
42
43
Fred Drake563dfc22001-10-23 14:41:08 +000044int
45PyArg_ParseTuple(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000046{
47 int retval;
48 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000049
50 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000051 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000052 va_end(va);
53 return retval;
54}
55
56
57int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000058PyArg_VaParse(PyObject *args, char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000059{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000060 va_list lva;
61
62#ifdef VA_LIST_IS_ARRAY
63 memcpy(lva, va, sizeof(va_list));
64#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +000065#ifdef __va_copy
66 __va_copy(lva, va);
67#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068 lva = va;
69#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +000070#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +000071
72 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000073}
74
75
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000076/* Handle cleanup of allocated memory in case of exception */
77
78static int
79addcleanup(void *ptr, PyObject **freelist)
80{
81 PyObject *cobj;
82 if (!*freelist) {
83 *freelist = PyList_New(0);
84 if (!*freelist) {
85 PyMem_FREE(ptr);
86 return -1;
87 }
88 }
89 cobj = PyCObject_FromVoidPtr(ptr, NULL);
90 if (!cobj) {
91 PyMem_FREE(ptr);
92 return -1;
93 }
94 if(PyList_Append(*freelist, cobj)) {
95 PyMem_FREE(ptr);
96 Py_DECREF(cobj);
97 return -1;
98 }
99 Py_DECREF(cobj);
100 return 0;
101}
102
103static int
104cleanreturn(int retval, PyObject *freelist)
105{
106 if(freelist) {
107 if((retval) == 0) {
108 int len = PyList_GET_SIZE(freelist), i;
109 for (i = 0; i < len; i++)
110 PyMem_FREE(PyCObject_AsVoidPtr(
111 PyList_GET_ITEM(freelist, i)));
112 }
113 Py_DECREF(freelist);
114 }
115 return retval;
116}
117
118
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000119static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000120vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000121{
122 char msgbuf[256];
123 int levels[32];
124 char *fname = NULL;
125 char *message = NULL;
126 int min = -1;
127 int max = 0;
128 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000129 int endfmt = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000130 char *formatsave = format;
131 int i, len;
132 char *msg;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000133 PyObject *freelist = NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000134
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000135 assert(compat || (args != (PyObject*)NULL));
136
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000137 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000138 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000139 switch (c) {
140 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000141 if (level == 0)
142 max++;
143 level++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000144 break;
145 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000146 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000147 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000148 else
149 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000150 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000151 case '\0':
152 endfmt = 1;
153 break;
154 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000155 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000156 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000157 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000158 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000159 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000160 endfmt = 1;
161 break;
162 default:
163 if (level == 0) {
164 if (c == 'O')
165 max++;
166 else if (isalpha(c)) {
167 if (c != 'e') /* skip encoded */
168 max++;
169 } else if (c == '|')
170 min = max;
171 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000172 break;
173 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000174 }
175
176 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000177 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000178
179 if (min < 0)
180 min = max;
181
182 format = formatsave;
183
184 if (compat) {
185 if (max == 0) {
186 if (args == NULL)
187 return 1;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000188 PyOS_snprintf(msgbuf, sizeof(msgbuf),
189 "%.200s%s takes no arguments",
190 fname==NULL ? "function" : fname,
191 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000192 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000193 return 0;
194 }
195 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000196 if (args == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000197 PyOS_snprintf(msgbuf, sizeof(msgbuf),
198 "%.200s%s takes at least one argument",
199 fname==NULL ? "function" : fname,
200 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000201 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000202 return 0;
203 }
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000204 msg = convertitem(args, &format, p_va, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000205 sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000206 if (msg == NULL)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000207 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000208 seterror(levels[0], msg, levels+1, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000209 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000210 }
211 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000212 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000213 "old style getargs format uses new features");
214 return 0;
215 }
216 }
217
Guido van Rossum79f25d91997-04-29 20:08:16 +0000218 if (!PyTuple_Check(args)) {
219 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000220 "new style getargs format but argument is not a tuple");
221 return 0;
222 }
223
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000224 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000225
226 if (len < min || max < len) {
227 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000228 PyOS_snprintf(msgbuf, sizeof(msgbuf),
229 "%.150s%s takes %s %d argument%s "
230 "(%d given)",
231 fname==NULL ? "function" : fname,
232 fname==NULL ? "" : "()",
233 min==max ? "exactly"
234 : len < min ? "at least" : "at most",
235 len < min ? min : max,
236 (len < min ? min : max) == 1 ? "" : "s",
237 len);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000238 message = msgbuf;
239 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000240 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000241 return 0;
242 }
243
244 for (i = 0; i < len; i++) {
245 if (*format == '|')
246 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000247 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000248 levels, msgbuf, sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000249 if (msg) {
250 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000251 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000252 }
253 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000254
Guido van Rossum730806d1998-04-10 22:27:42 +0000255 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000256 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000257 *format != '|' && *format != ':' && *format != ';') {
258 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000259 "bad format string: %.200s", formatsave);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000260 return cleanreturn(0, freelist);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000261 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000262
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000263 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000264}
265
266
267
268static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000270{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000271 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000272 int i;
273 char *p = buf;
274
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000276 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000277 else if (message == NULL) {
278 if (fname != NULL) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000279 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000280 p += strlen(p);
281 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000282 if (iarg != 0) {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000283 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000284 "argument %d", iarg);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000285 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000286 p += strlen(p);
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000287 while (levels[i] > 0 && (int)(p-buf) < 220) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000288 PyOS_snprintf(p, sizeof(buf) - (buf - p),
289 ", item %d", levels[i]-1);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000290 p += strlen(p);
291 i++;
292 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000293 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000294 else {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000295 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
Ka-Ping Yee20579702001-01-15 22:14:16 +0000296 p += strlen(p);
297 }
Tim Petersfaad5ad2001-12-03 00:43:33 +0000298 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000299 message = buf;
300 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000301 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000302}
303
304
305/* Convert a tuple argument.
306 On entry, *p_format points to the character _after_ the opening '('.
307 On successful exit, *p_format points to the closing ')'.
308 If successful:
309 *p_format and *p_va are updated,
310 *levels and *msgbuf are untouched,
311 and NULL is returned.
312 If the argument is invalid:
313 *p_format is unchanged,
314 *p_va is undefined,
315 *levels is a 0-terminated list of item numbers,
316 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000317 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000318 <typename1> is the name of the expected type, and
319 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000320 and msgbuf is returned.
321*/
322
323static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000324converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000325 char *msgbuf, size_t bufsize, int toplevel, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000326{
327 int level = 0;
328 int n = 0;
329 char *format = *p_format;
330 int i;
331
332 for (;;) {
333 int c = *format++;
334 if (c == '(') {
335 if (level == 0)
336 n++;
337 level++;
338 }
339 else if (c == ')') {
340 if (level == 0)
341 break;
342 level--;
343 }
344 else if (c == ':' || c == ';' || c == '\0')
345 break;
346 else if (level == 0 && isalpha(c))
347 n++;
348 }
349
Ka-Ping Yee20579702001-01-15 22:14:16 +0000350 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000351 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000352 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000353 toplevel ? "expected %d arguments, not %.50s" :
354 "must be %d-item sequence, not %.50s",
355 n,
356 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000357 return msgbuf;
358 }
359
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000360 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000361 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000362 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000363 toplevel ? "expected %d arguments, not %d" :
364 "must be sequence of length %d, not %d",
365 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000366 return msgbuf;
367 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000368
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000369 format = *p_format;
370 for (i = 0; i < n; i++) {
371 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000372 PyObject *item;
373 item = PySequence_GetItem(arg, i);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000374 msg = convertitem(item, &format, p_va, levels+1, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000375 bufsize, freelist);
Guido van Rossum66368cc1999-02-17 23:16:43 +0000376 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
377 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000378 if (msg != NULL) {
379 levels[0] = i+1;
380 return msg;
381 }
382 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000383
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000384 *p_format = format;
385 return NULL;
386}
387
388
389/* Convert a single item. */
390
391static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000392convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000393 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000394{
395 char *msg;
396 char *format = *p_format;
397
398 if (*format == '(' /* ')' */) {
399 format++;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000400 msg = converttuple(arg, &format, p_va, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000401 bufsize, 0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000402 if (msg == NULL)
403 format++;
404 }
405 else {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000406 msg = convertsimple(arg, &format, p_va, msgbuf, bufsize,
407 freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000408 if (msg != NULL)
409 levels[0] = 0;
410 }
411 if (msg == NULL)
412 *p_format = format;
413 return msg;
414}
415
416
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000417
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000418#define UNICODE_DEFAULT_ENCODING(arg) \
419 _PyUnicode_AsDefaultEncodedString(arg, NULL)
420
421/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000422
423static char *
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000424converterr(char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000425{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000426 assert(expected != NULL);
427 assert(arg != NULL);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000428 PyOS_snprintf(msgbuf, bufsize,
429 "must be %.50s, not %.50s", expected,
430 arg == Py_None ? "None" : arg->ob_type->tp_name);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000431 return msgbuf;
432}
433
434#define CONV_UNICODE "(unicode conversion error)"
435
Neil Schemenauer5042da62003-02-04 20:59:40 +0000436/* explicitly check for float arguments when integers are expected. For now
437 * signal a warning. Returns true if an exception was raised. */
438static int
439float_argument_error(PyObject *arg)
440{
441 if (PyFloat_Check(arg) &&
442 PyErr_Warn(PyExc_DeprecationWarning,
443 "integer argument expected, got float" ))
444 return 1;
445 else
446 return 0;
447}
448
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000449/* Convert a non-tuple argument. Return NULL if conversion went OK,
450 or a string with a message describing the failure. The message is
451 formatted as "must be <desired type>, not <actual type>".
452 When failing, an exception may or may not have been raised.
453 Don't call if a tuple is expected.
454*/
455
456static char *
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000457convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000458 size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000459{
460 char *format = *p_format;
461 char c = *format++;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000462#ifdef Py_USING_UNICODE
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000463 PyObject *uarg;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000464#endif
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000465
466 switch (c) {
467
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000468 case 'b': { /* unsigned byte -- very short int */
469 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000470 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000471 if (float_argument_error(arg))
472 return NULL;
Neil Schemenauerb808e992003-01-24 22:15:21 +0000473 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000474 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000475 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000476 else if (ival < 0) {
477 PyErr_SetString(PyExc_OverflowError,
478 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000479 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000480 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000481 else if (ival > UCHAR_MAX) {
482 PyErr_SetString(PyExc_OverflowError,
483 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000484 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000485 }
486 else
487 *p = (unsigned char) ival;
488 break;
489 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000490
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000491 case 'B': {/* byte sized bitfield - both signed and unsigned
492 values allowed */
493 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000494 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000495 if (float_argument_error(arg))
496 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000497 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000498 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000499 return converterr("integer<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000500 else
501 *p = (unsigned char) ival;
502 break;
503 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000504
Guido van Rossumfce26e72003-04-18 00:12:30 +0000505 case 'h': {/* signed short int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000506 short *p = va_arg(*p_va, short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000507 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000508 if (float_argument_error(arg))
509 return NULL;
Neil Schemenauerb808e992003-01-24 22:15:21 +0000510 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000511 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000512 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfce26e72003-04-18 00:12:30 +0000513 else if (ival < SHRT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000514 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000515 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000516 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000517 }
Guido van Rossumfce26e72003-04-18 00:12:30 +0000518 else if (ival > SHRT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000519 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000520 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000521 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000522 }
523 else
524 *p = (short) ival;
525 break;
526 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000527
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000528 case 'H': { /* short int sized bitfield, both signed and
529 unsigned allowed */
530 unsigned short *p = va_arg(*p_va, unsigned short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000531 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000532 if (float_argument_error(arg))
533 return NULL;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000534 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000535 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000536 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000537 else
538 *p = (unsigned short) ival;
539 break;
540 }
Jack Jansend50338f2000-07-06 12:22:00 +0000541
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000542 case 'i': {/* signed int */
543 int *p = va_arg(*p_va, int *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000544 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000545 if (float_argument_error(arg))
546 return NULL;
Neil Schemenauerb808e992003-01-24 22:15:21 +0000547 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000548 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000549 return converterr("integer<i>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000550 else if (ival > INT_MAX) {
551 PyErr_SetString(PyExc_OverflowError,
552 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000553 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000554 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000555 else if (ival < INT_MIN) {
556 PyErr_SetString(PyExc_OverflowError,
557 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000558 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000559 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000560 else
561 *p = ival;
562 break;
563 }
564
Thomas Hellera4ea6032003-04-17 18:55:45 +0000565 case 'I': { /* int sized bitfield, both signed and
566 unsigned allowed */
567 unsigned int *p = va_arg(*p_va, unsigned int *);
568 unsigned int ival;
569 if (float_argument_error(arg))
570 return NULL;
571 ival = PyInt_AsUnsignedLongMask(arg);
572 if (ival == -1 && PyErr_Occurred())
573 return converterr("integer<I>", arg, msgbuf, bufsize);
574 else
575 *p = ival;
576 break;
577 }
578
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000579 case 'l': {/* long int */
580 long *p = va_arg(*p_va, long *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000581 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000582 if (float_argument_error(arg))
583 return NULL;
Neil Schemenauerb808e992003-01-24 22:15:21 +0000584 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000585 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000586 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000587 else
588 *p = ival;
589 break;
590 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000591
592 case 'k': { /* long sized bitfield */
593 unsigned long *p = va_arg(*p_va, unsigned long *);
594 unsigned long ival;
595 if (PyInt_Check(arg))
596 ival = PyInt_AsUnsignedLongMask(arg);
597 else if (PyLong_Check(arg))
598 ival = PyLong_AsUnsignedLongMask(arg);
599 else
600 return converterr("integer<k>", arg, msgbuf, bufsize);
601 *p = ival;
602 break;
603 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000604
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000605#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000606 case 'L': {/* PY_LONG_LONG */
607 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
608 PY_LONG_LONG ival = PyLong_AsLongLong( arg );
609 if( ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000610 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000611 } else {
612 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000613 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000614 break;
615 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000616
617 case 'K': { /* long long sized bitfield */
618 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
619 unsigned PY_LONG_LONG ival;
620 if (float_argument_error(arg))
621 return NULL;
622 if (PyInt_Check(arg))
623 ival = PyInt_AsUnsignedLongMask(arg);
624 else if (PyLong_Check(arg))
625 ival = PyLong_AsUnsignedLongLongMask(arg);
626 else
627 return converterr("integer<K>", arg, msgbuf, bufsize);
628 *p = ival;
629 break;
630 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000631#endif
632
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000633 case 'f': {/* float */
634 float *p = va_arg(*p_va, float *);
635 double dval = PyFloat_AsDouble(arg);
636 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000637 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000638 else
639 *p = (float) dval;
640 break;
641 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000642
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000643 case 'd': {/* double */
644 double *p = va_arg(*p_va, double *);
645 double dval = PyFloat_AsDouble(arg);
646 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000647 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000648 else
649 *p = dval;
650 break;
651 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000652
Guido van Rossum530956d1996-07-21 02:27:43 +0000653#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000654 case 'D': {/* complex double */
655 Py_complex *p = va_arg(*p_va, Py_complex *);
656 Py_complex cval;
657 cval = PyComplex_AsCComplex(arg);
658 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000659 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000660 else
661 *p = cval;
662 break;
663 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000664#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000665
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000666 case 'c': {/* char */
667 char *p = va_arg(*p_va, char *);
668 if (PyString_Check(arg) && PyString_Size(arg) == 1)
Jeremy Hylton0407aea2001-10-10 02:51:57 +0000669 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000670 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000671 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000672 break;
673 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000674
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000675 case 's': {/* string */
676 if (*format == '#') {
677 void **p = (void **)va_arg(*p_va, char **);
678 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000679
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000680 if (PyString_Check(arg)) {
681 *p = PyString_AS_STRING(arg);
682 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000683 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000684#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000685 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000686 uarg = UNICODE_DEFAULT_ENCODING(arg);
687 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000688 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000689 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000690 *p = PyString_AS_STRING(uarg);
691 *q = PyString_GET_SIZE(uarg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000692 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000693#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000694 else { /* any buffer-like object */
695 char *buf;
696 int count = convertbuffer(arg, p, &buf);
697 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000698 return converterr(buf, arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000699 *q = count;
700 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000701 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000702 } else {
703 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000704
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000705 if (PyString_Check(arg))
706 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000707#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000708 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000709 uarg = UNICODE_DEFAULT_ENCODING(arg);
710 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000711 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000712 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000713 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000714 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000715#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000716 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000717 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000718 if ((int)strlen(*p) != PyString_Size(arg))
719 return converterr("string without null bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000720 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000721 }
722 break;
723 }
724
725 case 'z': {/* string, may be NULL (None) */
726 if (*format == '#') { /* any buffer-like object */
727 void **p = (void **)va_arg(*p_va, char **);
728 int *q = va_arg(*p_va, int *);
729
730 if (arg == Py_None) {
731 *p = 0;
732 *q = 0;
733 }
734 else if (PyString_Check(arg)) {
735 *p = PyString_AS_STRING(arg);
736 *q = PyString_GET_SIZE(arg);
737 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000738#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000739 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000740 uarg = UNICODE_DEFAULT_ENCODING(arg);
741 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000742 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000743 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000744 *p = PyString_AS_STRING(uarg);
745 *q = PyString_GET_SIZE(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000746 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000747#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000748 else { /* any buffer-like object */
749 char *buf;
750 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000751 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000752 return converterr(buf, arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000753 *q = count;
754 }
755 format++;
756 } else {
757 char **p = va_arg(*p_va, char **);
758
759 if (arg == Py_None)
760 *p = 0;
761 else if (PyString_Check(arg))
Jeremy Hyltona4c8cd72001-10-10 02:51:08 +0000762 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000763#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000764 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000765 uarg = UNICODE_DEFAULT_ENCODING(arg);
766 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000767 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000768 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000769 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000770 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000771#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000772 else
773 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000774 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000775 if (*format == '#') {
776 int *q = va_arg(*p_va, int *);
777 if (arg == Py_None)
778 *q = 0;
779 else
780 *q = PyString_Size(arg);
781 format++;
782 }
783 else if (*p != NULL &&
784 (int)strlen(*p) != PyString_Size(arg))
785 return converterr(
786 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000787 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000788 }
789 break;
790 }
791
792 case 'e': {/* encoded string */
793 char **buffer;
794 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000795 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000796 int size, recode_strings;
797
798 /* Get 'e' parameter: the encoding name */
799 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000800#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000801 if (encoding == NULL)
802 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000803#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000804
805 /* Get output buffer parameter:
806 's' (recode all objects via Unicode) or
807 't' (only recode non-string objects)
808 */
809 if (*format == 's')
810 recode_strings = 1;
811 else if (*format == 't')
812 recode_strings = 0;
813 else
814 return converterr(
815 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000816 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000817 buffer = (char **)va_arg(*p_va, char **);
818 format++;
819 if (buffer == NULL)
820 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000821 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000822
823 /* Encode object */
824 if (!recode_strings && PyString_Check(arg)) {
825 s = arg;
826 Py_INCREF(s);
827 }
828 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000829#ifdef Py_USING_UNICODE
830 PyObject *u;
831
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000832 /* Convert object to Unicode */
833 u = PyUnicode_FromObject(arg);
834 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000835 return converterr(
836 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000837 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000838
839 /* Encode object; use default error handling */
840 s = PyUnicode_AsEncodedString(u,
841 encoding,
842 NULL);
843 Py_DECREF(u);
844 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000845 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000846 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000847 if (!PyString_Check(s)) {
848 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000849 return converterr(
850 "(encoder failed to return a string)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000851 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000852 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000853#else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000854 return converterr("string<e>", arg, msgbuf, bufsize);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000855#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000856 }
857 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000858
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000859 /* Write output; output is guaranteed to be 0-terminated */
860 if (*format == '#') {
861 /* Using buffer length parameter '#':
862
863 - if *buffer is NULL, a new buffer of the
864 needed size is allocated and the data
865 copied into it; *buffer is updated to point
866 to the new buffer; the caller is
867 responsible for PyMem_Free()ing it after
868 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000869
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000870 - if *buffer is not NULL, the data is
871 copied to *buffer; *buffer_len has to be
872 set to the size of the buffer on input;
873 buffer overflow is signalled with an error;
874 buffer has to provide enough room for the
875 encoded string plus the trailing 0-byte
876
877 - in both cases, *buffer_len is updated to
878 the size of the buffer /excluding/ the
879 trailing 0-byte
880
881 */
882 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000883
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000884 format++;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000885 if (buffer_len == NULL) {
886 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000887 return converterr(
888 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000889 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000890 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000891 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000892 *buffer = PyMem_NEW(char, size + 1);
893 if (*buffer == NULL) {
894 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000895 return converterr(
896 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000897 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000898 }
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000899 if(addcleanup(*buffer, freelist)) {
900 Py_DECREF(s);
901 return converterr(
902 "(cleanup problem)",
903 arg, msgbuf, bufsize);
904 }
Fred Drake25871c02000-05-03 15:17:02 +0000905 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000906 if (size + 1 > *buffer_len) {
907 Py_DECREF(s);
908 return converterr(
909 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000910 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000911 }
Fred Drake25871c02000-05-03 15:17:02 +0000912 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000913 memcpy(*buffer,
914 PyString_AS_STRING(s),
915 size + 1);
916 *buffer_len = size;
917 } else {
918 /* Using a 0-terminated buffer:
919
920 - the encoded string has to be 0-terminated
921 for this variant to work; if it is not, an
922 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000923
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000924 - a new buffer of the needed size is
925 allocated and the data copied into it;
926 *buffer is updated to point to the new
927 buffer; the caller is responsible for
928 PyMem_Free()ing it after usage
929
930 */
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000931 if ((int)strlen(PyString_AS_STRING(s)) != size) {
932 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000933 return converterr(
934 "(encoded string without NULL bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000935 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000936 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000937 *buffer = PyMem_NEW(char, size + 1);
938 if (*buffer == NULL) {
939 Py_DECREF(s);
940 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000941 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000942 }
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000943 if(addcleanup(*buffer, freelist)) {
944 Py_DECREF(s);
945 return converterr("(cleanup problem)",
946 arg, msgbuf, bufsize);
947 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000948 memcpy(*buffer,
949 PyString_AS_STRING(s),
950 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000951 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000952 Py_DECREF(s);
953 break;
954 }
955
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000956#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000957 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
958 if (*format == '#') { /* any buffer-like object */
959 void **p = (void **)va_arg(*p_va, char **);
960 int *q = va_arg(*p_va, int *);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000961 if (PyUnicode_Check(arg)) {
962 *p = PyUnicode_AS_UNICODE(arg);
963 *q = PyUnicode_GET_SIZE(arg);
964 }
965 else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000966 char *buf;
967 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000968 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000969 return converterr(buf, arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000970 *q = count/(sizeof(Py_UNICODE));
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000971 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000972 format++;
973 } else {
974 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossume826ef02000-03-10 23:02:17 +0000975 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000976 *p = PyUnicode_AS_UNICODE(arg);
977 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000978 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000979 }
980 break;
981 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000982#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000983
984 case 'S': { /* string object */
985 PyObject **p = va_arg(*p_va, PyObject **);
986 if (PyString_Check(arg))
987 *p = arg;
988 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000989 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000990 break;
991 }
992
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000993#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000994 case 'U': { /* Unicode object */
995 PyObject **p = va_arg(*p_va, PyObject **);
996 if (PyUnicode_Check(arg))
997 *p = arg;
998 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000999 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001000 break;
1001 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001002#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001003
1004 case 'O': { /* object */
1005 PyTypeObject *type;
1006 PyObject **p;
1007 if (*format == '!') {
1008 type = va_arg(*p_va, PyTypeObject*);
1009 p = va_arg(*p_va, PyObject **);
1010 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +00001011 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +00001012 *p = arg;
1013 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001014 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +00001015
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001016 }
1017 else if (*format == '?') {
1018 inquiry pred = va_arg(*p_va, inquiry);
1019 p = va_arg(*p_va, PyObject **);
1020 format++;
1021 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001022 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001023 else
1024 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001025 arg, msgbuf, bufsize);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001026
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001027 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001028 else if (*format == '&') {
1029 typedef int (*converter)(PyObject *, void *);
1030 converter convert = va_arg(*p_va, converter);
1031 void *addr = va_arg(*p_va, void *);
1032 format++;
1033 if (! (*convert)(arg, addr))
1034 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001035 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001036 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001037 else {
1038 p = va_arg(*p_va, PyObject **);
1039 *p = arg;
1040 }
1041 break;
1042 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001043
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001044
1045 case 'w': { /* memory buffer, read-write access */
1046 void **p = va_arg(*p_va, void **);
1047 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1048 int count;
1049
1050 if (pb == NULL ||
1051 pb->bf_getwritebuffer == NULL ||
1052 pb->bf_getsegcount == NULL)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001053 return converterr("read-write buffer", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001054 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1055 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001056 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001057 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001058 return converterr("(unspecified)", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001059 if (*format == '#') {
1060 int *q = va_arg(*p_va, int *);
1061
1062 *q = count;
1063 format++;
1064 }
1065 break;
1066 }
1067
1068 case 't': { /* 8-bit character buffer, read-only access */
1069 const char **p = va_arg(*p_va, const char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001070 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001071 int count;
1072
1073 if (*format++ != '#')
1074 return converterr(
1075 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001076 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001077 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +00001078 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1079 pb == NULL || pb->bf_getcharbuffer == NULL ||
1080 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001081 return converterr(
1082 "string or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001083 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001084
Jeremy Hylton4819e972001-10-11 14:40:37 +00001085 if (pb->bf_getsegcount(arg, NULL) != 1)
1086 return converterr(
1087 "string or single-segment read-only buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001088 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001089
1090 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001091 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001092 return converterr("(unspecified)", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001093 *va_arg(*p_va, int *) = count;
1094 break;
1095 }
1096
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001097 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001098 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001099
1100 }
1101
1102 *p_format = format;
1103 return NULL;
1104}
Guido van Rossumaa354651996-08-19 19:32:04 +00001105
Fred Drake563dfc22001-10-23 14:41:08 +00001106static int
1107convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001108{
1109 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1110 int count;
1111 if (pb == NULL ||
1112 pb->bf_getreadbuffer == NULL ||
1113 pb->bf_getsegcount == NULL) {
1114 *errmsg = "string or read-only buffer";
1115 return -1;
1116 }
1117 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1118 *errmsg = "string or single-segment read-only buffer";
1119 return -1;
1120 }
1121 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1122 *errmsg = "(unspecified)";
1123 }
1124 return count;
1125}
Guido van Rossumaa354651996-08-19 19:32:04 +00001126
1127/* Support for keyword arguments donated by
1128 Geoff Philbrick <philbric@delphi.hks.com> */
1129
Tim Petersf8cd3e82001-10-27 04:26:57 +00001130/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001131int
1132PyArg_ParseTupleAndKeywords(PyObject *args,
1133 PyObject *keywords,
1134 char *format,
1135 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001136{
1137 int retval;
1138 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001139
1140 if ((args == NULL || !PyTuple_Check(args)) ||
1141 (keywords != NULL && !PyDict_Check(keywords)) ||
1142 format == NULL ||
1143 kwlist == NULL)
1144 {
1145 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001146 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001147 }
1148
Guido van Rossumaa354651996-08-19 19:32:04 +00001149 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001150 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
1151 va_end(va);
1152 return retval;
1153}
1154
1155
1156static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001157vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
1158 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001159{
Tim Petersdc5eff92001-10-27 06:53:00 +00001160 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001161 int levels[32];
Tim Petersf8cd3e82001-10-27 04:26:57 +00001162 char *fname, *message;
1163 int min, max;
Tim Peters6fb26352001-10-27 04:38:11 +00001164 char *formatsave;
Tim Petersb639d492001-10-27 07:00:56 +00001165 int i, len, nargs, nkeywords;
Tim Petersc2f01122001-10-27 07:25:06 +00001166 char *msg, **p;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001167 PyObject *freelist = NULL;
Tim Petersf4331c12001-10-27 00:17:34 +00001168
Tim Peters45772cd2001-10-27 03:58:40 +00001169 assert(args != NULL && PyTuple_Check(args));
1170 assert(keywords == NULL || PyDict_Check(keywords));
1171 assert(format != NULL);
1172 assert(kwlist != NULL);
1173 assert(p_va != NULL);
1174
Tim Petersf8cd3e82001-10-27 04:26:57 +00001175 /* Search the format:
1176 message <- error msg, if any (else NULL).
Tim Peterscffed4b2001-11-29 03:26:37 +00001177 fname <- routine name, if any (else NULL).
Tim Petersf8cd3e82001-10-27 04:26:57 +00001178 min <- # of required arguments, or -1 if all are required.
1179 max <- most arguments (required + optional).
Tim Peters62d48e12001-10-27 06:42:16 +00001180 Check that kwlist has a non-NULL entry for each arg.
Tim Petersf8cd3e82001-10-27 04:26:57 +00001181 Raise error if a tuple arg spec is found.
1182 */
1183 fname = message = NULL;
Tim Peters6fb26352001-10-27 04:38:11 +00001184 formatsave = format;
Tim Peters62d48e12001-10-27 06:42:16 +00001185 p = kwlist;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001186 min = -1;
1187 max = 0;
1188 while ((i = *format++) != '\0') {
Tim Peters62d48e12001-10-27 06:42:16 +00001189 if (isalpha(i) && i != 'e') {
Tim Petersf8cd3e82001-10-27 04:26:57 +00001190 max++;
Tim Peters62d48e12001-10-27 06:42:16 +00001191 if (*p == NULL) {
Tim Peterscffed4b2001-11-29 03:26:37 +00001192 PyErr_SetString(PyExc_RuntimeError,
1193 "more argument specifiers than "
1194 "keyword list entries");
Tim Peters62d48e12001-10-27 06:42:16 +00001195 return 0;
1196 }
1197 p++;
1198 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001199 else if (i == '|')
1200 min = max;
1201 else if (i == ':') {
1202 fname = format;
1203 break;
1204 }
1205 else if (i == ';') {
1206 message = format;
1207 break;
1208 }
1209 else if (i == '(') {
Tim Peterscffed4b2001-11-29 03:26:37 +00001210 PyErr_SetString(PyExc_RuntimeError,
1211 "tuple found in format when using keyword "
1212 "arguments");
Guido van Rossumaa354651996-08-19 19:32:04 +00001213 return 0;
1214 }
Tim Peters62d48e12001-10-27 06:42:16 +00001215 }
1216 format = formatsave;
1217 if (*p != NULL) {
Tim Peterscffed4b2001-11-29 03:26:37 +00001218 PyErr_SetString(PyExc_RuntimeError,
1219 "more keyword list entries than "
1220 "argument specifiers");
Tim Peters62d48e12001-10-27 06:42:16 +00001221 return 0;
1222 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001223 if (min < 0) {
1224 /* All arguments are required. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001225 min = max;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001226 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001227
Tim Peters6fb26352001-10-27 04:38:11 +00001228 nargs = PyTuple_GET_SIZE(args);
Tim Petersb0872fc2001-10-27 04:45:34 +00001229 nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001230
Guido van Rossumaa354651996-08-19 19:32:04 +00001231 /* make sure there are no duplicate values for an argument;
1232 its not clear when to use the term "keyword argument vs.
1233 keyword parameter in messages */
Tim Petersb054be42001-10-27 05:07:41 +00001234 if (nkeywords > 0) {
Tim Peters6fb26352001-10-27 04:38:11 +00001235 for (i = 0; i < nargs; i++) {
Tim Petersa9f47392001-10-27 00:46:09 +00001236 char *thiskw = kwlist[i];
1237 if (thiskw == NULL)
1238 break;
Tim Peters077f5742001-10-27 05:50:39 +00001239 if (PyDict_GetItemString(keywords, thiskw)) {
Tim Petersb054be42001-10-27 05:07:41 +00001240 PyErr_Format(PyExc_TypeError,
1241 "keyword parameter '%s' was given "
1242 "by position and by name",
Tim Petersa9f47392001-10-27 00:46:09 +00001243 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001244 return 0;
1245 }
Tim Peters0af49162001-10-27 06:14:32 +00001246 else if (PyErr_Occurred())
1247 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001248 }
1249 }
Tim Peters61dde632001-10-27 05:30:17 +00001250
Guido van Rossumaa354651996-08-19 19:32:04 +00001251 /* required arguments missing from args can be supplied by keyword
Tim Peters62d48e12001-10-27 06:42:16 +00001252 arguments; set len to the number of posiitional arguments, and,
1253 if that's less than the minimum required, add in the number of
1254 required arguments that are supplied by keywords */
Tim Peters6fb26352001-10-27 04:38:11 +00001255 len = nargs;
Tim Peters62d48e12001-10-27 06:42:16 +00001256 if (nkeywords > 0 && nargs < min) {
Tim Peters6fb26352001-10-27 04:38:11 +00001257 for (i = nargs; i < min; i++) {
Tim Peters077f5742001-10-27 05:50:39 +00001258 if (PyDict_GetItemString(keywords, kwlist[i]))
Guido van Rossumaa354651996-08-19 19:32:04 +00001259 len++;
Tim Peters0af49162001-10-27 06:14:32 +00001260 else if (PyErr_Occurred())
1261 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001262 }
1263 }
Tim Peters0af49162001-10-27 06:14:32 +00001264
Guido van Rossumaa354651996-08-19 19:32:04 +00001265 /* make sure we got an acceptable number of arguments; the message
1266 is a little confusing with keywords since keyword arguments
1267 which are supplied, but don't match the required arguments
1268 are not included in the "%d given" part of the message */
Guido van Rossumaa354651996-08-19 19:32:04 +00001269 if (len < min || max < len) {
1270 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +00001271 PyOS_snprintf(msgbuf, sizeof(msgbuf),
1272 "%.200s%s takes %s %d argument%s "
1273 "(%d given)",
1274 fname==NULL ? "function" : fname,
1275 fname==NULL ? "" : "()",
1276 min==max ? "exactly"
1277 : len < min ? "at least" : "at most",
1278 len < min ? min : max,
1279 (len < min ? min : max) == 1 ? "" : "s",
1280 len);
Guido van Rossumaa354651996-08-19 19:32:04 +00001281 message = msgbuf;
1282 }
1283 PyErr_SetString(PyExc_TypeError, message);
1284 return 0;
1285 }
Tim Petersc2f01122001-10-27 07:25:06 +00001286
1287 /* convert the positional arguments */
Tim Peters6fb26352001-10-27 04:38:11 +00001288 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001289 if (*format == '|')
1290 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001291 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001292 levels, msgbuf, sizeof(msgbuf), &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001293 if (msg) {
1294 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001295 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001296 }
1297 }
1298
Tim Petersc2f01122001-10-27 07:25:06 +00001299 /* handle no keyword parameters in call */
Tim Petersb054be42001-10-27 05:07:41 +00001300 if (nkeywords == 0)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001301 return cleanreturn(1, freelist);
Tim Petersb054be42001-10-27 05:07:41 +00001302
Guido van Rossumaa354651996-08-19 19:32:04 +00001303 /* convert the keyword arguments; this uses the format
1304 string where it was left after processing args */
Tim Petersb639d492001-10-27 07:00:56 +00001305 for (i = nargs; i < max; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001306 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001307 if (*format == '|')
1308 format++;
Tim Peters077f5742001-10-27 05:50:39 +00001309 item = PyDict_GetItemString(keywords, kwlist[i]);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001310 if (item != NULL) {
Tim Peters077f5742001-10-27 05:50:39 +00001311 Py_INCREF(item);
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001312 msg = convertitem(item, &format, p_va, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001313 sizeof(msgbuf), &freelist);
Tim Peters077f5742001-10-27 05:50:39 +00001314 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001315 if (msg) {
1316 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001317 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001318 }
Tim Petersc2f01122001-10-27 07:25:06 +00001319 --nkeywords;
1320 if (nkeywords == 0)
1321 break;
Guido van Rossumaa354651996-08-19 19:32:04 +00001322 }
Tim Peters0af49162001-10-27 06:14:32 +00001323 else if (PyErr_Occurred())
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001324 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001325 else {
Guido van Rossumaa354651996-08-19 19:32:04 +00001326 msg = skipitem(&format, p_va);
1327 if (msg) {
1328 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001329 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001330 }
1331 }
1332 }
Tim Petersb054be42001-10-27 05:07:41 +00001333
Guido van Rossumaa354651996-08-19 19:32:04 +00001334 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001335 if (nkeywords > 0) {
1336 PyObject *key, *value;
1337 int pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001338 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001339 int match = 0;
Guido van Rossum55474762002-04-04 16:22:30 +00001340 char *ks;
1341 if (!PyString_Check(key)) {
1342 PyErr_SetString(PyExc_TypeError,
1343 "keywords must be strings");
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001344 return cleanreturn(0, freelist);
Guido van Rossum55474762002-04-04 16:22:30 +00001345 }
1346 ks = PyString_AsString(key);
Tim Petersb639d492001-10-27 07:00:56 +00001347 for (i = 0; i < max; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001348 if (!strcmp(ks, kwlist[i])) {
1349 match = 1;
1350 break;
1351 }
1352 }
1353 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001354 PyErr_Format(PyExc_TypeError,
1355 "'%s' is an invalid keyword "
1356 "argument for this function",
1357 ks);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001358 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001359 }
1360 }
1361 }
Tim Petersc2f01122001-10-27 07:25:06 +00001362
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001363 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001364}
1365
1366
1367static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001368skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001369{
1370 char *format = *p_format;
1371 char c = *format++;
1372
1373 switch (c) {
1374
1375 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001376 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001377 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001378 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001379 break;
1380 }
1381
1382 case 'h': /* short int */
1383 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001384 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001385 break;
1386 }
1387
Jack Jansencc22fbe2000-08-05 21:29:58 +00001388 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001389 {
1390 (void) va_arg(*p_va, unsigned short *);
1391 break;
1392 }
1393
Guido van Rossumaa354651996-08-19 19:32:04 +00001394 case 'i': /* int */
1395 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001396 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001397 break;
1398 }
1399
1400 case 'l': /* long int */
1401 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001402 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001403 break;
1404 }
1405
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001406#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001407 case 'L': /* PY_LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001408 {
Martin v. Löwisb9a0f912003-03-29 10:06:18 +00001409 (void) va_arg(*p_va, PY_LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001410 break;
1411 }
1412#endif
1413
Guido van Rossumaa354651996-08-19 19:32:04 +00001414 case 'f': /* float */
1415 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001416 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001417 break;
1418 }
1419
1420 case 'd': /* double */
1421 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001422 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001423 break;
1424 }
1425
1426#ifndef WITHOUT_COMPLEX
1427 case 'D': /* complex double */
1428 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001429 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001430 break;
1431 }
1432#endif /* WITHOUT_COMPLEX */
1433
1434 case 'c': /* char */
1435 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001436 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001437 break;
1438 }
1439
1440 case 's': /* string */
1441 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001442 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001443 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001444 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001445 format++;
1446 }
1447 break;
1448 }
1449
1450 case 'z': /* string */
1451 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001452 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001453 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001454 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001455 format++;
1456 }
1457 break;
1458 }
1459
1460 case 'S': /* string object */
1461 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001462 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001463 break;
1464 }
1465
1466 case 'O': /* object */
1467 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001468 if (*format == '!') {
1469 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001470 (void) va_arg(*p_va, PyTypeObject*);
1471 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001472 }
1473#if 0
1474/* I don't know what this is for */
1475 else if (*format == '?') {
1476 inquiry pred = va_arg(*p_va, inquiry);
1477 format++;
1478 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001479 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001480 }
1481 }
1482#endif
1483 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001484 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001485 (void) va_arg(*p_va, converter);
1486 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001487 format++;
1488 }
1489 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001490 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001491 }
1492 break;
1493 }
1494
1495 default:
1496 return "impossible<bad format char>";
1497
1498 }
1499
1500 *p_format = format;
1501 return NULL;
1502}
Fred Drakee4616e62001-10-23 21:09:29 +00001503
1504
1505int
1506PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
1507{
1508 int i, l;
1509 PyObject **o;
1510 va_list vargs;
1511
1512#ifdef HAVE_STDARG_PROTOTYPES
1513 va_start(vargs, max);
1514#else
1515 va_start(vargs);
1516#endif
1517
1518 assert(min >= 0);
1519 assert(min <= max);
1520 if (!PyTuple_Check(args)) {
1521 PyErr_SetString(PyExc_SystemError,
1522 "PyArg_UnpackTuple() argument list is not a tuple");
1523 return 0;
1524 }
1525 l = PyTuple_GET_SIZE(args);
1526 if (l < min) {
1527 if (name != NULL)
1528 PyErr_Format(
1529 PyExc_TypeError,
1530 "%s expected %s%d arguments, got %d",
1531 name, (min == max ? "" : "at least "), min, l);
1532 else
1533 PyErr_Format(
1534 PyExc_TypeError,
1535 "unpacked tuple should have %s%d elements,"
1536 " but has %d",
1537 (min == max ? "" : "at least "), min, l);
1538 va_end(vargs);
1539 return 0;
1540 }
1541 if (l > max) {
1542 if (name != NULL)
1543 PyErr_Format(
1544 PyExc_TypeError,
1545 "%s expected %s%d arguments, got %d",
1546 name, (min == max ? "" : "at most "), max, l);
1547 else
1548 PyErr_Format(
1549 PyExc_TypeError,
1550 "unpacked tuple should have %s%d elements,"
1551 " but has %d",
1552 (min == max ? "" : "at most "), max, l);
1553 va_end(vargs);
1554 return 0;
1555 }
1556 for (i = 0; i < l; i++) {
1557 o = va_arg(vargs, PyObject **);
1558 *o = PyTuple_GET_ITEM(args, i);
1559 }
1560 va_end(vargs);
1561 return 1;
1562}