blob: 9bcf9bc754eb1fbbdb029fbe85ec5626646c80a4 [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
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00009int PyArg_Parse(PyObject *, const char *, ...);
10int PyArg_ParseTuple(PyObject *, const char *, ...);
11int PyArg_VaParse(PyObject *, const char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000012
Tim Petersdbd9ba62000-07-09 03:09:57 +000013int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000014 const char *, const char **, ...);
Brett Cannon711e7d92004-07-10 22:20:32 +000015int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000016 const char *, const char **, va_list);
Brett Cannon711e7d92004-07-10 22:20:32 +000017
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000018
19/* Forward */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000020static int vgetargs1(PyObject *, const char *, va_list *, int);
21static void seterror(int, const char *, int *, const char *, const char *);
22static char *convertitem(PyObject *, const char **, va_list *, int *, char *,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000023 size_t, PyObject **);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000024static char *converttuple(PyObject *, const char **, va_list *,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000025 int *, char *, size_t, int, PyObject **);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000026static char *convertsimple(PyObject *, const char **, va_list *, char *,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000027 size_t, PyObject **);
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 *,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000031 const char *, const char **, va_list *);
32static char *skipitem(const char **, va_list *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000033
Fred Drake563dfc22001-10-23 14:41:08 +000034int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000035PyArg_Parse(PyObject *args, const 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
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000048PyArg_ParseTuple(PyObject *args, const 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
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000061PyArg_VaParse(PyObject *args, const 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
Martin v. Löwis75d2d942002-07-28 10:23:27 +000068#ifdef __va_copy
69 __va_copy(lva, va);
70#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +000071 lva = va;
72#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +000073#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +000074
75 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000076}
77
78
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000079/* Handle cleanup of allocated memory in case of exception */
80
81static int
82addcleanup(void *ptr, PyObject **freelist)
83{
84 PyObject *cobj;
85 if (!*freelist) {
86 *freelist = PyList_New(0);
87 if (!*freelist) {
88 PyMem_FREE(ptr);
89 return -1;
90 }
91 }
92 cobj = PyCObject_FromVoidPtr(ptr, NULL);
93 if (!cobj) {
94 PyMem_FREE(ptr);
95 return -1;
96 }
97 if(PyList_Append(*freelist, cobj)) {
98 PyMem_FREE(ptr);
99 Py_DECREF(cobj);
100 return -1;
101 }
102 Py_DECREF(cobj);
103 return 0;
104}
105
106static int
107cleanreturn(int retval, PyObject *freelist)
108{
109 if(freelist) {
110 if((retval) == 0) {
111 int len = PyList_GET_SIZE(freelist), i;
112 for (i = 0; i < len; i++)
113 PyMem_FREE(PyCObject_AsVoidPtr(
114 PyList_GET_ITEM(freelist, i)));
115 }
116 Py_DECREF(freelist);
117 }
118 return retval;
119}
120
121
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000122static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000123vgetargs1(PyObject *args, const char *format, va_list *p_va, int compat)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000124{
125 char msgbuf[256];
126 int levels[32];
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000127 const char *fname = NULL;
128 const char *message = NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000129 int min = -1;
130 int max = 0;
131 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000132 int endfmt = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000133 const char *formatsave = format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000134 int i, len;
135 char *msg;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000136 PyObject *freelist = NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000137
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000138 assert(compat || (args != (PyObject*)NULL));
139
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000140 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000141 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000142 switch (c) {
143 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000144 if (level == 0)
145 max++;
146 level++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000147 break;
148 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000149 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000150 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000151 else
152 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000153 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000154 case '\0':
155 endfmt = 1;
156 break;
157 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000158 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000159 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000160 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000161 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000162 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000163 endfmt = 1;
164 break;
165 default:
166 if (level == 0) {
167 if (c == 'O')
168 max++;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000169 else if (isalpha(Py_CHARMASK(c))) {
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000170 if (c != 'e') /* skip encoded */
171 max++;
172 } else if (c == '|')
173 min = max;
174 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000175 break;
176 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000177 }
178
179 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000180 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000181
182 if (min < 0)
183 min = max;
184
185 format = formatsave;
186
187 if (compat) {
188 if (max == 0) {
189 if (args == NULL)
190 return 1;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000191 PyOS_snprintf(msgbuf, sizeof(msgbuf),
192 "%.200s%s takes no arguments",
193 fname==NULL ? "function" : fname,
194 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000195 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000196 return 0;
197 }
198 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000199 if (args == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000200 PyOS_snprintf(msgbuf, sizeof(msgbuf),
201 "%.200s%s takes at least one argument",
202 fname==NULL ? "function" : fname,
203 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000204 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000205 return 0;
206 }
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000207 msg = convertitem(args, &format, p_va, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000208 sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000209 if (msg == NULL)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000210 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000211 seterror(levels[0], msg, levels+1, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000212 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000213 }
214 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000215 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000216 "old style getargs format uses new features");
217 return 0;
218 }
219 }
220
Guido van Rossum79f25d91997-04-29 20:08:16 +0000221 if (!PyTuple_Check(args)) {
222 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000223 "new style getargs format but argument is not a tuple");
224 return 0;
225 }
226
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000227 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000228
229 if (len < min || max < len) {
230 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000231 PyOS_snprintf(msgbuf, sizeof(msgbuf),
232 "%.150s%s takes %s %d argument%s "
233 "(%d given)",
234 fname==NULL ? "function" : fname,
235 fname==NULL ? "" : "()",
236 min==max ? "exactly"
237 : len < min ? "at least" : "at most",
238 len < min ? min : max,
239 (len < min ? min : max) == 1 ? "" : "s",
240 len);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000241 message = msgbuf;
242 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000243 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000244 return 0;
245 }
246
247 for (i = 0; i < len; i++) {
248 if (*format == '|')
249 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000250 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000251 levels, msgbuf, sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000252 if (msg) {
253 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000254 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000255 }
256 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000257
Neal Norwitz4ac13df2005-12-19 06:10:07 +0000258 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000259 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000260 *format != '|' && *format != ':' && *format != ';') {
261 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000262 "bad format string: %.200s", formatsave);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000263 return cleanreturn(0, freelist);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000264 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000265
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000266 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000267}
268
269
270
271static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000272seterror(int iarg, const char *msg, int *levels, const char *fname,
273 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000274{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000275 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000276 int i;
277 char *p = buf;
278
Guido van Rossum79f25d91997-04-29 20:08:16 +0000279 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000280 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000281 else if (message == NULL) {
282 if (fname != NULL) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000283 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000284 p += strlen(p);
285 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000286 if (iarg != 0) {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000287 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000288 "argument %d", iarg);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000289 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000290 p += strlen(p);
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000291 while (levels[i] > 0 && (int)(p-buf) < 220) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000292 PyOS_snprintf(p, sizeof(buf) - (buf - p),
293 ", item %d", levels[i]-1);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000294 p += strlen(p);
295 i++;
296 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000297 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000298 else {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000299 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
Ka-Ping Yee20579702001-01-15 22:14:16 +0000300 p += strlen(p);
301 }
Tim Petersfaad5ad2001-12-03 00:43:33 +0000302 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000303 message = buf;
304 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000305 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000306}
307
308
309/* Convert a tuple argument.
310 On entry, *p_format points to the character _after_ the opening '('.
311 On successful exit, *p_format points to the closing ')'.
312 If successful:
313 *p_format and *p_va are updated,
314 *levels and *msgbuf are untouched,
315 and NULL is returned.
316 If the argument is invalid:
317 *p_format is unchanged,
318 *p_va is undefined,
319 *levels is a 0-terminated list of item numbers,
320 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000321 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000322 <typename1> is the name of the expected type, and
323 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000324 and msgbuf is returned.
325*/
326
327static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000328converttuple(PyObject *arg, const char **p_format, va_list *p_va, int *levels,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000329 char *msgbuf, size_t bufsize, int toplevel, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000330{
331 int level = 0;
332 int n = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000333 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000334 int i;
335
336 for (;;) {
337 int c = *format++;
338 if (c == '(') {
339 if (level == 0)
340 n++;
341 level++;
342 }
343 else if (c == ')') {
344 if (level == 0)
345 break;
346 level--;
347 }
348 else if (c == ':' || c == ';' || c == '\0')
349 break;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000350 else if (level == 0 && isalpha(Py_CHARMASK(c)))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000351 n++;
352 }
353
Ka-Ping Yee20579702001-01-15 22:14:16 +0000354 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000355 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000356 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000357 toplevel ? "expected %d arguments, not %.50s" :
358 "must be %d-item sequence, not %.50s",
359 n,
360 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000361 return msgbuf;
362 }
363
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000364 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000365 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000366 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000367 toplevel ? "expected %d arguments, not %d" :
368 "must be sequence of length %d, not %d",
369 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000370 return msgbuf;
371 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000372
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000373 format = *p_format;
374 for (i = 0; i < n; i++) {
375 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000376 PyObject *item;
377 item = PySequence_GetItem(arg, i);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000378 msg = convertitem(item, &format, p_va, levels+1, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000379 bufsize, freelist);
Guido van Rossum66368cc1999-02-17 23:16:43 +0000380 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
381 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000382 if (msg != NULL) {
383 levels[0] = i+1;
384 return msg;
385 }
386 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000387
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000388 *p_format = format;
389 return NULL;
390}
391
392
393/* Convert a single item. */
394
395static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000396convertitem(PyObject *arg, const char **p_format, va_list *p_va, int *levels,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000397 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000398{
399 char *msg;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000400 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000401
402 if (*format == '(' /* ')' */) {
403 format++;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000404 msg = converttuple(arg, &format, p_va, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000405 bufsize, 0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000406 if (msg == NULL)
407 format++;
408 }
409 else {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000410 msg = convertsimple(arg, &format, p_va, msgbuf, bufsize,
411 freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000412 if (msg != NULL)
413 levels[0] = 0;
414 }
415 if (msg == NULL)
416 *p_format = format;
417 return msg;
418}
419
420
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000421
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000422#define UNICODE_DEFAULT_ENCODING(arg) \
423 _PyUnicode_AsDefaultEncodedString(arg, NULL)
424
425/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000426
427static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000428converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000429{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000430 assert(expected != NULL);
431 assert(arg != NULL);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000432 PyOS_snprintf(msgbuf, bufsize,
433 "must be %.50s, not %.50s", expected,
434 arg == Py_None ? "None" : arg->ob_type->tp_name);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000435 return msgbuf;
436}
437
438#define CONV_UNICODE "(unicode conversion error)"
439
Neil Schemenauer5042da62003-02-04 20:59:40 +0000440/* explicitly check for float arguments when integers are expected. For now
441 * signal a warning. Returns true if an exception was raised. */
442static int
443float_argument_error(PyObject *arg)
444{
445 if (PyFloat_Check(arg) &&
446 PyErr_Warn(PyExc_DeprecationWarning,
447 "integer argument expected, got float" ))
448 return 1;
449 else
450 return 0;
451}
452
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000453/* Convert a non-tuple argument. Return NULL if conversion went OK,
454 or a string with a message describing the failure. The message is
455 formatted as "must be <desired type>, not <actual type>".
456 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000457 Don't call if a tuple is expected.
458
459 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000460*/
461
462static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000463convertsimple(PyObject *arg, const char **p_format, va_list *p_va,
464 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000465{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000466 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000467 char c = *format++;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000468#ifdef Py_USING_UNICODE
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000469 PyObject *uarg;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000470#endif
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000471
472 switch (c) {
473
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000474 case 'b': { /* unsigned byte -- very short int */
475 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000476 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000477 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000478 return converterr("integer<b>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000479 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000480 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000481 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000482 else if (ival < 0) {
483 PyErr_SetString(PyExc_OverflowError,
484 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000485 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000486 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000487 else if (ival > UCHAR_MAX) {
488 PyErr_SetString(PyExc_OverflowError,
489 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000490 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000491 }
492 else
493 *p = (unsigned char) ival;
494 break;
495 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000496
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000497 case 'B': {/* byte sized bitfield - both signed and unsigned
498 values allowed */
499 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000500 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000501 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000502 return converterr("integer<B>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000503 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000504 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000505 return converterr("integer<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000506 else
507 *p = (unsigned char) ival;
508 break;
509 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000510
Guido van Rossumfce26e72003-04-18 00:12:30 +0000511 case 'h': {/* signed short int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000512 short *p = va_arg(*p_va, short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000513 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000514 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000515 return converterr("integer<h>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000516 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000517 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000518 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfce26e72003-04-18 00:12:30 +0000519 else if (ival < SHRT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000520 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000521 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000522 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000523 }
Guido van Rossumfce26e72003-04-18 00:12:30 +0000524 else if (ival > SHRT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000525 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000526 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000527 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000528 }
529 else
530 *p = (short) ival;
531 break;
532 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000533
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000534 case 'H': { /* short int sized bitfield, both signed and
535 unsigned allowed */
536 unsigned short *p = va_arg(*p_va, unsigned short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000537 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000538 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000539 return converterr("integer<H>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000540 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000541 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000542 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000543 else
544 *p = (unsigned short) ival;
545 break;
546 }
Jack Jansend50338f2000-07-06 12:22:00 +0000547
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000548 case 'i': {/* signed int */
549 int *p = va_arg(*p_va, int *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000550 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000551 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000552 return converterr("integer<i>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000553 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000554 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000555 return converterr("integer<i>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000556 else if (ival > INT_MAX) {
557 PyErr_SetString(PyExc_OverflowError,
558 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000559 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000560 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000561 else if (ival < INT_MIN) {
562 PyErr_SetString(PyExc_OverflowError,
563 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000564 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000565 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000566 else
567 *p = ival;
568 break;
569 }
570
Thomas Hellera4ea6032003-04-17 18:55:45 +0000571 case 'I': { /* int sized bitfield, both signed and
572 unsigned allowed */
573 unsigned int *p = va_arg(*p_va, unsigned int *);
574 unsigned int ival;
575 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000576 return converterr("integer<I>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000577 ival = PyInt_AsUnsignedLongMask(arg);
578 if (ival == -1 && PyErr_Occurred())
579 return converterr("integer<I>", arg, msgbuf, bufsize);
580 else
581 *p = ival;
582 break;
583 }
584
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000585 case 'l': {/* long int */
586 long *p = va_arg(*p_va, long *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000587 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000588 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000589 return converterr("integer<l>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000590 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000591 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000592 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000593 else
594 *p = ival;
595 break;
596 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000597
598 case 'k': { /* long sized bitfield */
599 unsigned long *p = va_arg(*p_va, unsigned long *);
600 unsigned long ival;
601 if (PyInt_Check(arg))
602 ival = PyInt_AsUnsignedLongMask(arg);
603 else if (PyLong_Check(arg))
604 ival = PyLong_AsUnsignedLongMask(arg);
605 else
606 return converterr("integer<k>", arg, msgbuf, bufsize);
607 *p = ival;
608 break;
609 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000610
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000611#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000612 case 'L': {/* PY_LONG_LONG */
613 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
614 PY_LONG_LONG ival = PyLong_AsLongLong( arg );
615 if( ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000616 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000617 } else {
618 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000619 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000620 break;
621 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000622
623 case 'K': { /* long long sized bitfield */
624 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
625 unsigned PY_LONG_LONG ival;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000626 if (PyInt_Check(arg))
627 ival = PyInt_AsUnsignedLongMask(arg);
628 else if (PyLong_Check(arg))
629 ival = PyLong_AsUnsignedLongLongMask(arg);
630 else
631 return converterr("integer<K>", arg, msgbuf, bufsize);
632 *p = ival;
633 break;
634 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000635#endif
636
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000637 case 'f': {/* float */
638 float *p = va_arg(*p_va, float *);
639 double dval = PyFloat_AsDouble(arg);
640 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000641 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000642 else
643 *p = (float) dval;
644 break;
645 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000646
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000647 case 'd': {/* double */
648 double *p = va_arg(*p_va, double *);
649 double dval = PyFloat_AsDouble(arg);
650 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000651 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000652 else
653 *p = dval;
654 break;
655 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000656
Guido van Rossum530956d1996-07-21 02:27:43 +0000657#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000658 case 'D': {/* complex double */
659 Py_complex *p = va_arg(*p_va, Py_complex *);
660 Py_complex cval;
661 cval = PyComplex_AsCComplex(arg);
662 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000663 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000664 else
665 *p = cval;
666 break;
667 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000668#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000669
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000670 case 'c': {/* char */
671 char *p = va_arg(*p_va, char *);
672 if (PyString_Check(arg) && PyString_Size(arg) == 1)
Jeremy Hylton0407aea2001-10-10 02:51:57 +0000673 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000674 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000675 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000676 break;
677 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000678
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000679 case 's': {/* string */
680 if (*format == '#') {
681 void **p = (void **)va_arg(*p_va, char **);
682 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000683
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000684 if (PyString_Check(arg)) {
685 *p = PyString_AS_STRING(arg);
686 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000687 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000688#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000689 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000690 uarg = UNICODE_DEFAULT_ENCODING(arg);
691 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000692 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000693 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000694 *p = PyString_AS_STRING(uarg);
695 *q = PyString_GET_SIZE(uarg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000696 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000697#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000698 else { /* any buffer-like object */
699 char *buf;
700 int count = convertbuffer(arg, p, &buf);
701 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000702 return converterr(buf, arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000703 *q = count;
704 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000705 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000706 } else {
707 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000708
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000709 if (PyString_Check(arg))
710 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000711#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000712 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000713 uarg = UNICODE_DEFAULT_ENCODING(arg);
714 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000715 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000716 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000717 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000718 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000719#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000720 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000721 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000722 if ((int)strlen(*p) != PyString_Size(arg))
723 return converterr("string without null bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000724 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000725 }
726 break;
727 }
728
729 case 'z': {/* string, may be NULL (None) */
730 if (*format == '#') { /* any buffer-like object */
731 void **p = (void **)va_arg(*p_va, char **);
732 int *q = va_arg(*p_va, int *);
733
734 if (arg == Py_None) {
735 *p = 0;
736 *q = 0;
737 }
738 else if (PyString_Check(arg)) {
739 *p = PyString_AS_STRING(arg);
740 *q = PyString_GET_SIZE(arg);
741 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000742#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000743 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000744 uarg = UNICODE_DEFAULT_ENCODING(arg);
745 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000746 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000747 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000748 *p = PyString_AS_STRING(uarg);
749 *q = PyString_GET_SIZE(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000750 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000751#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000752 else { /* any buffer-like object */
753 char *buf;
754 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000755 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000756 return converterr(buf, arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000757 *q = count;
758 }
759 format++;
760 } else {
761 char **p = va_arg(*p_va, char **);
762
763 if (arg == Py_None)
764 *p = 0;
765 else if (PyString_Check(arg))
Jeremy Hyltona4c8cd72001-10-10 02:51:08 +0000766 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000767#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000768 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000769 uarg = UNICODE_DEFAULT_ENCODING(arg);
770 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000771 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000772 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000773 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000774 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000775#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000776 else
777 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000778 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000779 if (*format == '#') {
780 int *q = va_arg(*p_va, int *);
781 if (arg == Py_None)
782 *q = 0;
783 else
784 *q = PyString_Size(arg);
785 format++;
786 }
787 else if (*p != NULL &&
788 (int)strlen(*p) != PyString_Size(arg))
789 return converterr(
790 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000791 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000792 }
793 break;
794 }
795
796 case 'e': {/* encoded string */
797 char **buffer;
798 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000799 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000800 int size, recode_strings;
801
802 /* Get 'e' parameter: the encoding name */
803 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000804#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000805 if (encoding == NULL)
806 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000807#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000808
809 /* Get output buffer parameter:
810 's' (recode all objects via Unicode) or
811 't' (only recode non-string objects)
812 */
813 if (*format == 's')
814 recode_strings = 1;
815 else if (*format == 't')
816 recode_strings = 0;
817 else
818 return converterr(
819 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000820 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000821 buffer = (char **)va_arg(*p_va, char **);
822 format++;
823 if (buffer == NULL)
824 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000825 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000826
827 /* Encode object */
828 if (!recode_strings && PyString_Check(arg)) {
829 s = arg;
830 Py_INCREF(s);
831 }
832 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000833#ifdef Py_USING_UNICODE
834 PyObject *u;
835
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000836 /* Convert object to Unicode */
837 u = PyUnicode_FromObject(arg);
838 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000839 return converterr(
840 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000841 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000842
843 /* Encode object; use default error handling */
844 s = PyUnicode_AsEncodedString(u,
845 encoding,
846 NULL);
847 Py_DECREF(u);
848 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000849 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000850 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000851 if (!PyString_Check(s)) {
852 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000853 return converterr(
854 "(encoder failed to return a string)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000855 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000856 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000857#else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000858 return converterr("string<e>", arg, msgbuf, bufsize);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000859#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000860 }
861 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000862
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000863 /* Write output; output is guaranteed to be 0-terminated */
864 if (*format == '#') {
865 /* Using buffer length parameter '#':
866
867 - if *buffer is NULL, a new buffer of the
868 needed size is allocated and the data
869 copied into it; *buffer is updated to point
870 to the new buffer; the caller is
871 responsible for PyMem_Free()ing it after
872 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000873
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000874 - if *buffer is not NULL, the data is
875 copied to *buffer; *buffer_len has to be
876 set to the size of the buffer on input;
877 buffer overflow is signalled with an error;
878 buffer has to provide enough room for the
879 encoded string plus the trailing 0-byte
880
881 - in both cases, *buffer_len is updated to
882 the size of the buffer /excluding/ the
883 trailing 0-byte
884
885 */
886 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000887
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000888 format++;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000889 if (buffer_len == NULL) {
890 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000891 return converterr(
892 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000893 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000894 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000895 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000896 *buffer = PyMem_NEW(char, size + 1);
897 if (*buffer == NULL) {
898 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000899 return converterr(
900 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000901 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000902 }
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000903 if(addcleanup(*buffer, freelist)) {
904 Py_DECREF(s);
905 return converterr(
906 "(cleanup problem)",
907 arg, msgbuf, bufsize);
908 }
Fred Drake25871c02000-05-03 15:17:02 +0000909 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000910 if (size + 1 > *buffer_len) {
911 Py_DECREF(s);
912 return converterr(
913 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000914 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000915 }
Fred Drake25871c02000-05-03 15:17:02 +0000916 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000917 memcpy(*buffer,
918 PyString_AS_STRING(s),
919 size + 1);
920 *buffer_len = size;
921 } else {
922 /* Using a 0-terminated buffer:
923
924 - the encoded string has to be 0-terminated
925 for this variant to work; if it is not, an
926 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000927
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000928 - a new buffer of the needed size is
929 allocated and the data copied into it;
930 *buffer is updated to point to the new
931 buffer; the caller is responsible for
932 PyMem_Free()ing it after usage
933
934 */
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000935 if ((int)strlen(PyString_AS_STRING(s)) != size) {
936 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000937 return converterr(
938 "(encoded string without NULL bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000939 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000940 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000941 *buffer = PyMem_NEW(char, size + 1);
942 if (*buffer == NULL) {
943 Py_DECREF(s);
944 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000945 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000946 }
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000947 if(addcleanup(*buffer, freelist)) {
948 Py_DECREF(s);
949 return converterr("(cleanup problem)",
950 arg, msgbuf, bufsize);
951 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000952 memcpy(*buffer,
953 PyString_AS_STRING(s),
954 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000955 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000956 Py_DECREF(s);
957 break;
958 }
959
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000960#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000961 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
962 if (*format == '#') { /* any buffer-like object */
963 void **p = (void **)va_arg(*p_va, char **);
964 int *q = va_arg(*p_va, int *);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000965 if (PyUnicode_Check(arg)) {
966 *p = PyUnicode_AS_UNICODE(arg);
967 *q = PyUnicode_GET_SIZE(arg);
968 }
969 else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000970 char *buf;
971 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000972 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000973 return converterr(buf, arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000974 *q = count/(sizeof(Py_UNICODE));
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +0000975 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000976 format++;
977 } else {
978 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossume826ef02000-03-10 23:02:17 +0000979 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000980 *p = PyUnicode_AS_UNICODE(arg);
981 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000982 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000983 }
984 break;
985 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000986#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000987
988 case 'S': { /* string object */
989 PyObject **p = va_arg(*p_va, PyObject **);
990 if (PyString_Check(arg))
991 *p = arg;
992 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000993 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000994 break;
995 }
996
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000997#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000998 case 'U': { /* Unicode object */
999 PyObject **p = va_arg(*p_va, PyObject **);
1000 if (PyUnicode_Check(arg))
1001 *p = arg;
1002 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001003 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001004 break;
1005 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001006#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001007
1008 case 'O': { /* object */
1009 PyTypeObject *type;
1010 PyObject **p;
1011 if (*format == '!') {
1012 type = va_arg(*p_va, PyTypeObject*);
1013 p = va_arg(*p_va, PyObject **);
1014 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +00001015 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +00001016 *p = arg;
1017 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001018 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +00001019
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001020 }
1021 else if (*format == '?') {
1022 inquiry pred = va_arg(*p_va, inquiry);
1023 p = va_arg(*p_va, PyObject **);
1024 format++;
1025 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001026 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001027 else
1028 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001029 arg, msgbuf, bufsize);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001030
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001031 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001032 else if (*format == '&') {
1033 typedef int (*converter)(PyObject *, void *);
1034 converter convert = va_arg(*p_va, converter);
1035 void *addr = va_arg(*p_va, void *);
1036 format++;
1037 if (! (*convert)(arg, addr))
1038 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001039 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001040 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001041 else {
1042 p = va_arg(*p_va, PyObject **);
1043 *p = arg;
1044 }
1045 break;
1046 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001047
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001048
1049 case 'w': { /* memory buffer, read-write access */
1050 void **p = va_arg(*p_va, void **);
1051 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1052 int count;
1053
1054 if (pb == NULL ||
1055 pb->bf_getwritebuffer == NULL ||
1056 pb->bf_getsegcount == NULL)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001057 return converterr("read-write buffer", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001058 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1059 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001060 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001061 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001062 return converterr("(unspecified)", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001063 if (*format == '#') {
1064 int *q = va_arg(*p_va, int *);
1065
1066 *q = count;
1067 format++;
1068 }
1069 break;
1070 }
1071
1072 case 't': { /* 8-bit character buffer, read-only access */
1073 const char **p = va_arg(*p_va, const char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001074 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001075 int count;
1076
1077 if (*format++ != '#')
1078 return converterr(
1079 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001080 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001081 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +00001082 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1083 pb == NULL || pb->bf_getcharbuffer == NULL ||
1084 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001085 return converterr(
1086 "string or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001087 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001088
Jeremy Hylton4819e972001-10-11 14:40:37 +00001089 if (pb->bf_getsegcount(arg, NULL) != 1)
1090 return converterr(
1091 "string or single-segment read-only buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001092 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001093
1094 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001095 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001096 return converterr("(unspecified)", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001097 *va_arg(*p_va, int *) = count;
1098 break;
1099 }
1100
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001101 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001102 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001103
1104 }
1105
1106 *p_format = format;
1107 return NULL;
1108}
Guido van Rossumaa354651996-08-19 19:32:04 +00001109
Fred Drake563dfc22001-10-23 14:41:08 +00001110static int
1111convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001112{
1113 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1114 int count;
1115 if (pb == NULL ||
1116 pb->bf_getreadbuffer == NULL ||
1117 pb->bf_getsegcount == NULL) {
1118 *errmsg = "string or read-only buffer";
1119 return -1;
1120 }
1121 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1122 *errmsg = "string or single-segment read-only buffer";
1123 return -1;
1124 }
1125 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1126 *errmsg = "(unspecified)";
1127 }
1128 return count;
1129}
Guido van Rossumaa354651996-08-19 19:32:04 +00001130
1131/* Support for keyword arguments donated by
1132 Geoff Philbrick <philbric@delphi.hks.com> */
1133
Tim Petersf8cd3e82001-10-27 04:26:57 +00001134/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001135int
1136PyArg_ParseTupleAndKeywords(PyObject *args,
1137 PyObject *keywords,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001138 const char *format,
1139 const char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001140{
1141 int retval;
1142 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001143
1144 if ((args == NULL || !PyTuple_Check(args)) ||
1145 (keywords != NULL && !PyDict_Check(keywords)) ||
1146 format == NULL ||
1147 kwlist == NULL)
1148 {
1149 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001150 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001151 }
1152
Guido van Rossumaa354651996-08-19 19:32:04 +00001153 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001154 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
1155 va_end(va);
1156 return retval;
1157}
1158
1159
Brett Cannon711e7d92004-07-10 22:20:32 +00001160int
1161PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001162 PyObject *keywords,
1163 const char *format,
1164 const char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001165{
1166 int retval;
1167 va_list lva;
1168
1169 if ((args == NULL || !PyTuple_Check(args)) ||
1170 (keywords != NULL && !PyDict_Check(keywords)) ||
1171 format == NULL ||
1172 kwlist == NULL)
1173 {
1174 PyErr_BadInternalCall();
1175 return 0;
1176 }
1177
1178#ifdef VA_LIST_IS_ARRAY
1179 memcpy(lva, va, sizeof(va_list));
1180#else
1181#ifdef __va_copy
1182 __va_copy(lva, va);
1183#else
1184 lva = va;
1185#endif
1186#endif
1187
1188 retval = vgetargskeywords(args, keywords, format, kwlist, &lva);
1189 return retval;
1190}
1191
1192
Guido van Rossumaa354651996-08-19 19:32:04 +00001193static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001194vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
1195 const char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001196{
Tim Petersdc5eff92001-10-27 06:53:00 +00001197 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001198 int levels[32];
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001199 const char *fname, *message;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001200 int min, max;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001201 const char *formatsave;
Tim Petersb639d492001-10-27 07:00:56 +00001202 int i, len, nargs, nkeywords;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001203 const char *msg, **p;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001204 PyObject *freelist = NULL;
Tim Petersf4331c12001-10-27 00:17:34 +00001205
Tim Peters45772cd2001-10-27 03:58:40 +00001206 assert(args != NULL && PyTuple_Check(args));
1207 assert(keywords == NULL || PyDict_Check(keywords));
1208 assert(format != NULL);
1209 assert(kwlist != NULL);
1210 assert(p_va != NULL);
1211
Tim Petersf8cd3e82001-10-27 04:26:57 +00001212 /* Search the format:
1213 message <- error msg, if any (else NULL).
Tim Peterscffed4b2001-11-29 03:26:37 +00001214 fname <- routine name, if any (else NULL).
Tim Petersf8cd3e82001-10-27 04:26:57 +00001215 min <- # of required arguments, or -1 if all are required.
1216 max <- most arguments (required + optional).
Tim Peters62d48e12001-10-27 06:42:16 +00001217 Check that kwlist has a non-NULL entry for each arg.
Tim Petersf8cd3e82001-10-27 04:26:57 +00001218 Raise error if a tuple arg spec is found.
1219 */
1220 fname = message = NULL;
Tim Peters6fb26352001-10-27 04:38:11 +00001221 formatsave = format;
Tim Peters62d48e12001-10-27 06:42:16 +00001222 p = kwlist;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001223 min = -1;
1224 max = 0;
1225 while ((i = *format++) != '\0') {
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001226 if (isalpha(Py_CHARMASK(i)) && i != 'e') {
Tim Petersf8cd3e82001-10-27 04:26:57 +00001227 max++;
Tim Peters62d48e12001-10-27 06:42:16 +00001228 if (*p == NULL) {
Tim Peterscffed4b2001-11-29 03:26:37 +00001229 PyErr_SetString(PyExc_RuntimeError,
1230 "more argument specifiers than "
1231 "keyword list entries");
Tim Peters62d48e12001-10-27 06:42:16 +00001232 return 0;
1233 }
1234 p++;
1235 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001236 else if (i == '|')
1237 min = max;
1238 else if (i == ':') {
1239 fname = format;
1240 break;
1241 }
1242 else if (i == ';') {
1243 message = format;
1244 break;
1245 }
1246 else if (i == '(') {
Tim Peterscffed4b2001-11-29 03:26:37 +00001247 PyErr_SetString(PyExc_RuntimeError,
1248 "tuple found in format when using keyword "
1249 "arguments");
Guido van Rossumaa354651996-08-19 19:32:04 +00001250 return 0;
1251 }
Tim Peters62d48e12001-10-27 06:42:16 +00001252 }
1253 format = formatsave;
1254 if (*p != NULL) {
Tim Peterscffed4b2001-11-29 03:26:37 +00001255 PyErr_SetString(PyExc_RuntimeError,
1256 "more keyword list entries than "
1257 "argument specifiers");
Tim Peters62d48e12001-10-27 06:42:16 +00001258 return 0;
1259 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001260 if (min < 0) {
1261 /* All arguments are required. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001262 min = max;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001263 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001264
Tim Peters6fb26352001-10-27 04:38:11 +00001265 nargs = PyTuple_GET_SIZE(args);
Tim Petersb0872fc2001-10-27 04:45:34 +00001266 nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001267
Guido van Rossumaa354651996-08-19 19:32:04 +00001268 /* make sure there are no duplicate values for an argument;
1269 its not clear when to use the term "keyword argument vs.
1270 keyword parameter in messages */
Tim Petersb054be42001-10-27 05:07:41 +00001271 if (nkeywords > 0) {
Tim Peters6fb26352001-10-27 04:38:11 +00001272 for (i = 0; i < nargs; i++) {
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001273 const char *thiskw = kwlist[i];
Tim Petersa9f47392001-10-27 00:46:09 +00001274 if (thiskw == NULL)
1275 break;
Tim Peters077f5742001-10-27 05:50:39 +00001276 if (PyDict_GetItemString(keywords, thiskw)) {
Tim Petersb054be42001-10-27 05:07:41 +00001277 PyErr_Format(PyExc_TypeError,
1278 "keyword parameter '%s' was given "
1279 "by position and by name",
Tim Petersa9f47392001-10-27 00:46:09 +00001280 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001281 return 0;
1282 }
Tim Peters0af49162001-10-27 06:14:32 +00001283 else if (PyErr_Occurred())
1284 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001285 }
1286 }
Tim Peters61dde632001-10-27 05:30:17 +00001287
Guido van Rossumaa354651996-08-19 19:32:04 +00001288 /* required arguments missing from args can be supplied by keyword
Georg Brandld7048172006-01-20 17:53:27 +00001289 arguments; set len to the number of positional arguments, and,
Tim Peters62d48e12001-10-27 06:42:16 +00001290 if that's less than the minimum required, add in the number of
1291 required arguments that are supplied by keywords */
Tim Peters6fb26352001-10-27 04:38:11 +00001292 len = nargs;
Tim Peters62d48e12001-10-27 06:42:16 +00001293 if (nkeywords > 0 && nargs < min) {
Tim Peters6fb26352001-10-27 04:38:11 +00001294 for (i = nargs; i < min; i++) {
Tim Peters077f5742001-10-27 05:50:39 +00001295 if (PyDict_GetItemString(keywords, kwlist[i]))
Guido van Rossumaa354651996-08-19 19:32:04 +00001296 len++;
Tim Peters0af49162001-10-27 06:14:32 +00001297 else if (PyErr_Occurred())
1298 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001299 }
1300 }
Tim Peters0af49162001-10-27 06:14:32 +00001301
Guido van Rossumaa354651996-08-19 19:32:04 +00001302 /* make sure we got an acceptable number of arguments; the message
1303 is a little confusing with keywords since keyword arguments
1304 which are supplied, but don't match the required arguments
Michael W. Hudson5253c302005-03-30 16:41:55 +00001305 are not included in the "%d given" part of the message
1306 XXX and this isn't a bug!? */
Guido van Rossumaa354651996-08-19 19:32:04 +00001307 if (len < min || max < len) {
1308 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +00001309 PyOS_snprintf(msgbuf, sizeof(msgbuf),
1310 "%.200s%s takes %s %d argument%s "
1311 "(%d given)",
1312 fname==NULL ? "function" : fname,
1313 fname==NULL ? "" : "()",
1314 min==max ? "exactly"
1315 : len < min ? "at least" : "at most",
1316 len < min ? min : max,
1317 (len < min ? min : max) == 1 ? "" : "s",
1318 len);
Guido van Rossumaa354651996-08-19 19:32:04 +00001319 message = msgbuf;
1320 }
1321 PyErr_SetString(PyExc_TypeError, message);
1322 return 0;
1323 }
Tim Petersc2f01122001-10-27 07:25:06 +00001324
1325 /* convert the positional arguments */
Tim Peters6fb26352001-10-27 04:38:11 +00001326 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001327 if (*format == '|')
1328 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001329 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001330 levels, msgbuf, sizeof(msgbuf), &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001331 if (msg) {
1332 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001333 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001334 }
1335 }
1336
Tim Petersc2f01122001-10-27 07:25:06 +00001337 /* handle no keyword parameters in call */
Tim Petersb054be42001-10-27 05:07:41 +00001338 if (nkeywords == 0)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001339 return cleanreturn(1, freelist);
Tim Petersb054be42001-10-27 05:07:41 +00001340
Guido van Rossumaa354651996-08-19 19:32:04 +00001341 /* convert the keyword arguments; this uses the format
1342 string where it was left after processing args */
Tim Petersb639d492001-10-27 07:00:56 +00001343 for (i = nargs; i < max; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001344 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001345 if (*format == '|')
1346 format++;
Tim Peters077f5742001-10-27 05:50:39 +00001347 item = PyDict_GetItemString(keywords, kwlist[i]);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001348 if (item != NULL) {
Tim Peters077f5742001-10-27 05:50:39 +00001349 Py_INCREF(item);
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001350 msg = convertitem(item, &format, p_va, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001351 sizeof(msgbuf), &freelist);
Tim Peters077f5742001-10-27 05:50:39 +00001352 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001353 if (msg) {
1354 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001355 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001356 }
Tim Petersc2f01122001-10-27 07:25:06 +00001357 --nkeywords;
1358 if (nkeywords == 0)
1359 break;
Guido van Rossumaa354651996-08-19 19:32:04 +00001360 }
Tim Peters0af49162001-10-27 06:14:32 +00001361 else if (PyErr_Occurred())
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001362 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001363 else {
Guido van Rossumaa354651996-08-19 19:32:04 +00001364 msg = skipitem(&format, p_va);
1365 if (msg) {
1366 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001367 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001368 }
1369 }
1370 }
Tim Petersb054be42001-10-27 05:07:41 +00001371
Guido van Rossumaa354651996-08-19 19:32:04 +00001372 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001373 if (nkeywords > 0) {
1374 PyObject *key, *value;
1375 int pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001376 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001377 int match = 0;
Guido van Rossum55474762002-04-04 16:22:30 +00001378 char *ks;
1379 if (!PyString_Check(key)) {
1380 PyErr_SetString(PyExc_TypeError,
1381 "keywords must be strings");
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001382 return cleanreturn(0, freelist);
Guido van Rossum55474762002-04-04 16:22:30 +00001383 }
1384 ks = PyString_AsString(key);
Tim Petersb639d492001-10-27 07:00:56 +00001385 for (i = 0; i < max; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001386 if (!strcmp(ks, kwlist[i])) {
1387 match = 1;
1388 break;
1389 }
1390 }
1391 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001392 PyErr_Format(PyExc_TypeError,
1393 "'%s' is an invalid keyword "
1394 "argument for this function",
1395 ks);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001396 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001397 }
1398 }
1399 }
Tim Petersc2f01122001-10-27 07:25:06 +00001400
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001401 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001402}
1403
1404
1405static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001406skipitem(const char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001407{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001408 const char *format = *p_format;
Guido van Rossumaa354651996-08-19 19:32:04 +00001409 char c = *format++;
1410
1411 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001412
1413 /* simple codes
1414 * The individual types (second arg of va_arg) are irrelevant */
1415
Guido van Rossumaa354651996-08-19 19:32:04 +00001416 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001417 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001418 case 'h': /* short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001419 case 'H': /* short int as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001420 case 'i': /* int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001421 case 'I': /* int sized bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001422 case 'l': /* long int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001423 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001424#ifdef HAVE_LONG_LONG
Georg Brandl6dd14612005-09-14 19:29:53 +00001425 case 'L': /* PY_LONG_LONG */
1426 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001427#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001428 case 'f': /* float */
Guido van Rossumaa354651996-08-19 19:32:04 +00001429 case 'd': /* double */
Guido van Rossumaa354651996-08-19 19:32:04 +00001430#ifndef WITHOUT_COMPLEX
1431 case 'D': /* complex double */
Georg Brandl6dd14612005-09-14 19:29:53 +00001432#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001433 case 'c': /* char */
1434 {
Georg Brandl6dd14612005-09-14 19:29:53 +00001435 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001436 break;
1437 }
1438
Georg Brandl6dd14612005-09-14 19:29:53 +00001439 /* string codes */
1440
1441 case 'e': /* string with encoding */
1442 {
1443 (void) va_arg(*p_va, const char *);
1444 if (!(*format == 's' || *format == 't'))
1445 /* after 'e', only 's' and 't' is allowed */
1446 goto err;
1447 format++;
1448 /* explicit fallthrough to string cases */
1449 }
1450
Guido van Rossumaa354651996-08-19 19:32:04 +00001451 case 's': /* string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001452 case 'z': /* string or None */
1453#ifdef Py_USING_UNICODE
1454 case 'u': /* unicode string */
1455#endif
1456 case 't': /* buffer, read-only */
1457 case 'w': /* buffer, read-write */
Guido van Rossumaa354651996-08-19 19:32:04 +00001458 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001459 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001460 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001461 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001462 format++;
1463 }
1464 break;
1465 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001466
1467 /* object codes */
1468
Guido van Rossumaa354651996-08-19 19:32:04 +00001469 case 'S': /* string object */
Georg Brandl6dd14612005-09-14 19:29:53 +00001470#ifdef Py_USING_UNICODE
1471 case 'U': /* unicode string object */
1472#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001473 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001474 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001475 break;
1476 }
1477
1478 case 'O': /* object */
1479 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001480 if (*format == '!') {
1481 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001482 (void) va_arg(*p_va, PyTypeObject*);
1483 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001484 }
1485#if 0
1486/* I don't know what this is for */
1487 else if (*format == '?') {
1488 inquiry pred = va_arg(*p_va, inquiry);
1489 format++;
1490 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001491 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001492 }
1493 }
1494#endif
1495 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001496 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001497 (void) va_arg(*p_va, converter);
1498 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001499 format++;
1500 }
1501 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001502 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001503 }
1504 break;
1505 }
1506
1507 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001508err:
Guido van Rossumaa354651996-08-19 19:32:04 +00001509 return "impossible<bad format char>";
1510
1511 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001512
1513 /* The "(...)" format code for tuples is not handled here because
1514 * it is not allowed with keyword args. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001515
1516 *p_format = format;
1517 return NULL;
1518}
Fred Drakee4616e62001-10-23 21:09:29 +00001519
1520
1521int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001522PyArg_UnpackTuple(PyObject *args, const char *name, int min, int max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001523{
1524 int i, l;
1525 PyObject **o;
1526 va_list vargs;
1527
1528#ifdef HAVE_STDARG_PROTOTYPES
1529 va_start(vargs, max);
1530#else
1531 va_start(vargs);
1532#endif
1533
1534 assert(min >= 0);
1535 assert(min <= max);
1536 if (!PyTuple_Check(args)) {
1537 PyErr_SetString(PyExc_SystemError,
1538 "PyArg_UnpackTuple() argument list is not a tuple");
1539 return 0;
1540 }
1541 l = PyTuple_GET_SIZE(args);
1542 if (l < min) {
1543 if (name != NULL)
1544 PyErr_Format(
1545 PyExc_TypeError,
1546 "%s expected %s%d arguments, got %d",
1547 name, (min == max ? "" : "at least "), min, l);
1548 else
1549 PyErr_Format(
1550 PyExc_TypeError,
1551 "unpacked tuple should have %s%d elements,"
1552 " but has %d",
1553 (min == max ? "" : "at least "), min, l);
1554 va_end(vargs);
1555 return 0;
1556 }
1557 if (l > max) {
1558 if (name != NULL)
1559 PyErr_Format(
1560 PyExc_TypeError,
1561 "%s expected %s%d arguments, got %d",
1562 name, (min == max ? "" : "at most "), max, l);
1563 else
1564 PyErr_Format(
1565 PyExc_TypeError,
1566 "unpacked tuple should have %s%d elements,"
1567 " but has %d",
1568 (min == max ? "" : "at most "), max, l);
1569 va_end(vargs);
1570 return 0;
1571 }
1572 for (i = 0; i < l; i++) {
1573 o = va_arg(vargs, PyObject **);
1574 *o = PyTuple_GET_ITEM(args, i);
1575 }
1576 va_end(vargs);
1577 return 1;
1578}
Georg Brandl02c42872005-08-26 06:42:30 +00001579
1580
1581/* For type constructors that don't take keyword args
1582 *
1583 * Sets a TypeError and returns 0 if the kwds dict is
1584 * not emtpy, returns 1 otherwise
1585 */
1586int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001587_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001588{
1589 if (kw == NULL)
1590 return 1;
1591 if (!PyDict_CheckExact(kw)) {
1592 PyErr_BadInternalCall();
1593 return 0;
1594 }
1595 if (PyDict_Size(kw) == 0)
1596 return 1;
1597
1598 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1599 funcname);
1600 return 0;
1601}