blob: 9df2a2e56672dda3af83ae6b801bc291f5e21924 [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 *,
20 size_t);
Tim Petersdbd9ba62000-07-09 03:09:57 +000021static char *converttuple(PyObject *, char **, va_list *,
Jeremy Hyltonb048b262001-11-28 22:14:37 +000022 int *, char *, size_t, int);
23static char *convertsimple(PyObject *, char **, va_list *, char *, size_t);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +000024static int convertbuffer(PyObject *, void **p, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000025
Tim Petersdbd9ba62000-07-09 03:09:57 +000026static int vgetargskeywords(PyObject *, PyObject *,
27 char *, char **, va_list *);
28static char *skipitem(char **, va_list *);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000029
Fred Drake563dfc22001-10-23 14:41:08 +000030int
31PyArg_Parse(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000032{
33 int retval;
34 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000035
36 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000037 retval = vgetargs1(args, format, &va, 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000038 va_end(va);
39 return retval;
40}
41
42
Fred Drake563dfc22001-10-23 14:41:08 +000043int
44PyArg_ParseTuple(PyObject *args, char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000045{
46 int retval;
47 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000048
49 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000051 va_end(va);
52 return retval;
53}
54
55
56int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000057PyArg_VaParse(PyObject *args, char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000058{
Guido van Rossum1ae940a1995-01-02 19:04:15 +000059 va_list lva;
60
61#ifdef VA_LIST_IS_ARRAY
62 memcpy(lva, va, sizeof(va_list));
63#else
64 lva = va;
65#endif
66
67 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000068}
69
70
71static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000072vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000073{
74 char msgbuf[256];
75 int levels[32];
76 char *fname = NULL;
77 char *message = NULL;
78 int min = -1;
79 int max = 0;
80 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000081 int endfmt = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000082 char *formatsave = format;
83 int i, len;
84 char *msg;
85
Tim Peters5c4d5bf2001-02-12 22:13:26 +000086 assert(compat || (args != (PyObject*)NULL));
87
Jeremy Hylton25916bd2001-05-29 17:46:19 +000088 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000089 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000090 switch (c) {
91 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000092 if (level == 0)
93 max++;
94 level++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +000095 break;
96 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000097 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +000098 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000099 else
100 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000101 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000102 case '\0':
103 endfmt = 1;
104 break;
105 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000106 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000107 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000108 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000109 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000110 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000111 endfmt = 1;
112 break;
113 default:
114 if (level == 0) {
115 if (c == 'O')
116 max++;
117 else if (isalpha(c)) {
118 if (c != 'e') /* skip encoded */
119 max++;
120 } else if (c == '|')
121 min = max;
122 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000123 break;
124 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000125 }
126
127 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000128 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000129
130 if (min < 0)
131 min = max;
132
133 format = formatsave;
134
135 if (compat) {
136 if (max == 0) {
137 if (args == NULL)
138 return 1;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000139 PyOS_snprintf(msgbuf, sizeof(msgbuf),
140 "%.200s%s takes no arguments",
141 fname==NULL ? "function" : fname,
142 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000143 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000144 return 0;
145 }
146 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000147 if (args == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000148 PyOS_snprintf(msgbuf, sizeof(msgbuf),
149 "%.200s%s takes at least one argument",
150 fname==NULL ? "function" : fname,
151 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000152 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000153 return 0;
154 }
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000155 msg = convertitem(args, &format, p_va, levels, msgbuf,
156 sizeof(msgbuf));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000157 if (msg == NULL)
158 return 1;
159 seterror(levels[0], msg, levels+1, fname, message);
160 return 0;
161 }
162 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000164 "old style getargs format uses new features");
165 return 0;
166 }
167 }
168
Guido van Rossum79f25d91997-04-29 20:08:16 +0000169 if (!PyTuple_Check(args)) {
170 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000171 "new style getargs format but argument is not a tuple");
172 return 0;
173 }
174
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000175 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000176
177 if (len < min || max < len) {
178 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000179 PyOS_snprintf(msgbuf, sizeof(msgbuf),
180 "%.150s%s takes %s %d argument%s "
181 "(%d given)",
182 fname==NULL ? "function" : fname,
183 fname==NULL ? "" : "()",
184 min==max ? "exactly"
185 : len < min ? "at least" : "at most",
186 len < min ? min : max,
187 (len < min ? min : max) == 1 ? "" : "s",
188 len);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000189 message = msgbuf;
190 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000191 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000192 return 0;
193 }
194
195 for (i = 0; i < len; i++) {
196 if (*format == '|')
197 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000198 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000199 levels, msgbuf, sizeof(msgbuf));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000200 if (msg) {
201 seterror(i+1, msg, levels, fname, message);
202 return 0;
203 }
204 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000205
Guido van Rossum730806d1998-04-10 22:27:42 +0000206 if (*format != '\0' && !isalpha((int)(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000207 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000208 *format != '|' && *format != ':' && *format != ';') {
209 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000210 "bad format string: %.200s", formatsave);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000211 return 0;
212 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000213
214 return 1;
215}
216
217
218
219static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000220seterror(int iarg, char *msg, int *levels, char *fname, char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000221{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000222 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000223 int i;
224 char *p = buf;
225
Guido van Rossum79f25d91997-04-29 20:08:16 +0000226 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000227 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000228 else if (message == NULL) {
229 if (fname != NULL) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000230 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000231 p += strlen(p);
232 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000233 if (iarg != 0) {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000234 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000235 "argument %d", iarg);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000236 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000237 p += strlen(p);
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000238 while (levels[i] > 0 && (int)(p-buf) < 220) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000239 PyOS_snprintf(p, sizeof(buf) - (buf - p),
240 ", item %d", levels[i]-1);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000241 p += strlen(p);
242 i++;
243 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000244 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000245 else {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000246 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
Ka-Ping Yee20579702001-01-15 22:14:16 +0000247 p += strlen(p);
248 }
Tim Petersfaad5ad2001-12-03 00:43:33 +0000249 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000250 message = buf;
251 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000252 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000253}
254
255
256/* Convert a tuple argument.
257 On entry, *p_format points to the character _after_ the opening '('.
258 On successful exit, *p_format points to the closing ')'.
259 If successful:
260 *p_format and *p_va are updated,
261 *levels and *msgbuf are untouched,
262 and NULL is returned.
263 If the argument is invalid:
264 *p_format is unchanged,
265 *p_va is undefined,
266 *levels is a 0-terminated list of item numbers,
267 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000268 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000269 <typename1> is the name of the expected type, and
270 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000271 and msgbuf is returned.
272*/
273
274static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000275converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000276 char *msgbuf, size_t bufsize, int toplevel)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000277{
278 int level = 0;
279 int n = 0;
280 char *format = *p_format;
281 int i;
282
283 for (;;) {
284 int c = *format++;
285 if (c == '(') {
286 if (level == 0)
287 n++;
288 level++;
289 }
290 else if (c == ')') {
291 if (level == 0)
292 break;
293 level--;
294 }
295 else if (c == ':' || c == ';' || c == '\0')
296 break;
297 else if (level == 0 && isalpha(c))
298 n++;
299 }
300
Ka-Ping Yee20579702001-01-15 22:14:16 +0000301 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000302 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000303 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000304 toplevel ? "expected %d arguments, not %.50s" :
305 "must be %d-item sequence, not %.50s",
306 n,
307 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000308 return msgbuf;
309 }
310
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000311 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000312 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000313 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000314 toplevel ? "expected %d arguments, not %d" :
315 "must be sequence of length %d, not %d",
316 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000317 return msgbuf;
318 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000319
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000320 format = *p_format;
321 for (i = 0; i < n; i++) {
322 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000323 PyObject *item;
324 item = PySequence_GetItem(arg, i);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000325 msg = convertitem(item, &format, p_va, levels+1, msgbuf,
326 bufsize);
Guido van Rossum66368cc1999-02-17 23:16:43 +0000327 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
328 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000329 if (msg != NULL) {
330 levels[0] = i+1;
331 return msg;
332 }
333 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000334
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000335 *p_format = format;
336 return NULL;
337}
338
339
340/* Convert a single item. */
341
342static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000343convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000344 char *msgbuf, size_t bufsize)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000345{
346 char *msg;
347 char *format = *p_format;
348
349 if (*format == '(' /* ')' */) {
350 format++;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000351 msg = converttuple(arg, &format, p_va, levels, msgbuf,
352 bufsize, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000353 if (msg == NULL)
354 format++;
355 }
356 else {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000357 msg = convertsimple(arg, &format, p_va, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000358 if (msg != NULL)
359 levels[0] = 0;
360 }
361 if (msg == NULL)
362 *p_format = format;
363 return msg;
364}
365
366
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000367
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000368#define UNICODE_DEFAULT_ENCODING(arg) \
369 _PyUnicode_AsDefaultEncodedString(arg, NULL)
370
371/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000372
373static char *
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000374converterr(char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000375{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000376 assert(expected != NULL);
377 assert(arg != NULL);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000378 PyOS_snprintf(msgbuf, bufsize,
379 "must be %.50s, not %.50s", expected,
380 arg == Py_None ? "None" : arg->ob_type->tp_name);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000381 return msgbuf;
382}
383
384#define CONV_UNICODE "(unicode conversion error)"
385
386/* Convert a non-tuple argument. Return NULL if conversion went OK,
387 or a string with a message describing the failure. The message is
388 formatted as "must be <desired type>, not <actual type>".
389 When failing, an exception may or may not have been raised.
390 Don't call if a tuple is expected.
391*/
392
393static char *
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000394convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
395 size_t bufsize)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000396{
397 char *format = *p_format;
398 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000399 PyObject *uarg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000400
401 switch (c) {
402
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000403 case 'b': { /* unsigned byte -- very short int */
404 char *p = va_arg(*p_va, char *);
405 long ival = PyInt_AsLong(arg);
406 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000407 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000408 else if (ival < 0) {
409 PyErr_SetString(PyExc_OverflowError,
410 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000411 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000412 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000413 else if (ival > UCHAR_MAX) {
414 PyErr_SetString(PyExc_OverflowError,
415 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000416 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000417 }
418 else
419 *p = (unsigned char) ival;
420 break;
421 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000422
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000423 case 'B': {/* byte sized bitfield - both signed and unsigned
424 values allowed */
425 char *p = va_arg(*p_va, char *);
426 long ival = PyInt_AsLong(arg);
427 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000428 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000429 else if (ival < SCHAR_MIN) {
430 PyErr_SetString(PyExc_OverflowError,
431 "byte-sized integer bitfield is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000432 return converterr("integer<B>", arg, msgbuf, bufsize);
Jack Jansencc22fbe2000-08-05 21:29:58 +0000433 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000434 else if (ival > (int)UCHAR_MAX) {
435 PyErr_SetString(PyExc_OverflowError,
436 "byte-sized integer bitfield is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000437 return converterr("integer<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000438 }
439 else
440 *p = (unsigned char) ival;
441 break;
442 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000443
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000444 case 'h': {/* signed short int */
445 short *p = va_arg(*p_va, short *);
446 long ival = PyInt_AsLong(arg);
447 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000448 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000449 else if (ival < SHRT_MIN) {
450 PyErr_SetString(PyExc_OverflowError,
451 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000452 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000453 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000454 else if (ival > SHRT_MAX) {
455 PyErr_SetString(PyExc_OverflowError,
456 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000457 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000458 }
459 else
460 *p = (short) ival;
461 break;
462 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000463
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000464 case 'H': { /* short int sized bitfield, both signed and
465 unsigned allowed */
466 unsigned short *p = va_arg(*p_va, unsigned short *);
467 long ival = PyInt_AsLong(arg);
468 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000469 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000470 else if (ival < SHRT_MIN) {
471 PyErr_SetString(PyExc_OverflowError,
472 "short integer bitfield is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000473 return converterr("integer<H>", arg, msgbuf, bufsize);
Jack Jansend50338f2000-07-06 12:22:00 +0000474 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000475 else if (ival > USHRT_MAX) {
476 PyErr_SetString(PyExc_OverflowError,
477 "short integer bitfield is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000478 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000479 }
480 else
481 *p = (unsigned short) ival;
482 break;
483 }
Jack Jansend50338f2000-07-06 12:22:00 +0000484
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000485 case 'i': {/* signed int */
486 int *p = va_arg(*p_va, int *);
487 long ival = PyInt_AsLong(arg);
488 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000489 return converterr("integer<i>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000490 else if (ival > INT_MAX) {
491 PyErr_SetString(PyExc_OverflowError,
492 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000493 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000494 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000495 else if (ival < INT_MIN) {
496 PyErr_SetString(PyExc_OverflowError,
497 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000498 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000499 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000500 else
501 *p = ival;
502 break;
503 }
504
505 case 'l': {/* long int */
506 long *p = va_arg(*p_va, long *);
507 long ival = PyInt_AsLong(arg);
508 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000509 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000510 else
511 *p = ival;
512 break;
513 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000514
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000515#ifdef HAVE_LONG_LONG
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000516 case 'L': {/* LONG_LONG */
517 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
518 LONG_LONG ival = PyLong_AsLongLong( arg );
519 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000520 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000521 } else {
522 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000523 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000524 break;
525 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000526#endif
527
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000528 case 'f': {/* float */
529 float *p = va_arg(*p_va, float *);
530 double dval = PyFloat_AsDouble(arg);
531 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000532 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000533 else
534 *p = (float) dval;
535 break;
536 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000537
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000538 case 'd': {/* double */
539 double *p = va_arg(*p_va, double *);
540 double dval = PyFloat_AsDouble(arg);
541 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000542 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000543 else
544 *p = dval;
545 break;
546 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000547
Guido van Rossum530956d1996-07-21 02:27:43 +0000548#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000549 case 'D': {/* complex double */
550 Py_complex *p = va_arg(*p_va, Py_complex *);
551 Py_complex cval;
552 cval = PyComplex_AsCComplex(arg);
553 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000554 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000555 else
556 *p = cval;
557 break;
558 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000559#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000560
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000561 case 'c': {/* char */
562 char *p = va_arg(*p_va, char *);
563 if (PyString_Check(arg) && PyString_Size(arg) == 1)
Jeremy Hylton0407aea2001-10-10 02:51:57 +0000564 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000565 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000566 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000567 break;
568 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000569
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000570 case 's': {/* string */
571 if (*format == '#') {
572 void **p = (void **)va_arg(*p_va, char **);
573 int *q = va_arg(*p_va, int *);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000574
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000575 if (PyString_Check(arg)) {
576 *p = PyString_AS_STRING(arg);
577 *q = PyString_GET_SIZE(arg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000578 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000579#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000580 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000581 uarg = UNICODE_DEFAULT_ENCODING(arg);
582 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000583 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000584 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000585 *p = PyString_AS_STRING(uarg);
586 *q = PyString_GET_SIZE(uarg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000587 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000588#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000589 else { /* any buffer-like object */
590 char *buf;
591 int count = convertbuffer(arg, p, &buf);
592 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000593 return converterr(buf, arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000594 *q = count;
595 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000596 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000597 } else {
598 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000599
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000600 if (PyString_Check(arg))
601 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000602#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000603 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000604 uarg = UNICODE_DEFAULT_ENCODING(arg);
605 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000606 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000607 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000608 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000609 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000610#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000611 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000612 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000613 if ((int)strlen(*p) != PyString_Size(arg))
614 return converterr("string without null bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000615 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000616 }
617 break;
618 }
619
620 case 'z': {/* string, may be NULL (None) */
621 if (*format == '#') { /* any buffer-like object */
622 void **p = (void **)va_arg(*p_va, char **);
623 int *q = va_arg(*p_va, int *);
624
625 if (arg == Py_None) {
626 *p = 0;
627 *q = 0;
628 }
629 else if (PyString_Check(arg)) {
630 *p = PyString_AS_STRING(arg);
631 *q = PyString_GET_SIZE(arg);
632 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000633#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000634 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000635 uarg = UNICODE_DEFAULT_ENCODING(arg);
636 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000637 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000638 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000639 *p = PyString_AS_STRING(uarg);
640 *q = PyString_GET_SIZE(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000641 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000642#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000643 else { /* any buffer-like object */
644 char *buf;
645 int count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000646 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000647 return converterr(buf, arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000648 *q = count;
649 }
650 format++;
651 } else {
652 char **p = va_arg(*p_va, char **);
653
654 if (arg == Py_None)
655 *p = 0;
656 else if (PyString_Check(arg))
Jeremy Hyltona4c8cd72001-10-10 02:51:08 +0000657 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000658#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000659 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000660 uarg = UNICODE_DEFAULT_ENCODING(arg);
661 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000662 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000663 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000664 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000665 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000666#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000667 else
668 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000669 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000670 if (*format == '#') {
671 int *q = va_arg(*p_va, int *);
672 if (arg == Py_None)
673 *q = 0;
674 else
675 *q = PyString_Size(arg);
676 format++;
677 }
678 else if (*p != NULL &&
679 (int)strlen(*p) != PyString_Size(arg))
680 return converterr(
681 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000682 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000683 }
684 break;
685 }
686
687 case 'e': {/* encoded string */
688 char **buffer;
689 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000690 PyObject *s;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000691 int size, recode_strings;
692
693 /* Get 'e' parameter: the encoding name */
694 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000695#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000696 if (encoding == NULL)
697 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000698#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000699
700 /* Get output buffer parameter:
701 's' (recode all objects via Unicode) or
702 't' (only recode non-string objects)
703 */
704 if (*format == 's')
705 recode_strings = 1;
706 else if (*format == 't')
707 recode_strings = 0;
708 else
709 return converterr(
710 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000711 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000712 buffer = (char **)va_arg(*p_va, char **);
713 format++;
714 if (buffer == NULL)
715 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000716 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000717
718 /* Encode object */
719 if (!recode_strings && PyString_Check(arg)) {
720 s = arg;
721 Py_INCREF(s);
722 }
723 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000724#ifdef Py_USING_UNICODE
725 PyObject *u;
726
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000727 /* Convert object to Unicode */
728 u = PyUnicode_FromObject(arg);
729 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000730 return converterr(
731 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000732 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000733
734 /* Encode object; use default error handling */
735 s = PyUnicode_AsEncodedString(u,
736 encoding,
737 NULL);
738 Py_DECREF(u);
739 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000740 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000741 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000742 if (!PyString_Check(s)) {
743 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000744 return converterr(
745 "(encoder failed to return a string)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000746 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000747 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000748#else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000749 return converterr("string<e>", arg, msgbuf, bufsize);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000750#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000751 }
752 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000753
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000754 /* Write output; output is guaranteed to be 0-terminated */
755 if (*format == '#') {
756 /* Using buffer length parameter '#':
757
758 - if *buffer is NULL, a new buffer of the
759 needed size is allocated and the data
760 copied into it; *buffer is updated to point
761 to the new buffer; the caller is
762 responsible for PyMem_Free()ing it after
763 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000764
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000765 - if *buffer is not NULL, the data is
766 copied to *buffer; *buffer_len has to be
767 set to the size of the buffer on input;
768 buffer overflow is signalled with an error;
769 buffer has to provide enough room for the
770 encoded string plus the trailing 0-byte
771
772 - in both cases, *buffer_len is updated to
773 the size of the buffer /excluding/ the
774 trailing 0-byte
775
776 */
777 int *buffer_len = va_arg(*p_va, int *);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000778
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000779 format++;
780 if (buffer_len == NULL)
781 return converterr(
782 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000783 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000784 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000785 *buffer = PyMem_NEW(char, size + 1);
786 if (*buffer == NULL) {
787 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000788 return converterr(
789 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000790 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000791 }
Fred Drake25871c02000-05-03 15:17:02 +0000792 } else {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000793 if (size + 1 > *buffer_len) {
794 Py_DECREF(s);
795 return converterr(
796 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000797 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000798 }
Fred Drake25871c02000-05-03 15:17:02 +0000799 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000800 memcpy(*buffer,
801 PyString_AS_STRING(s),
802 size + 1);
803 *buffer_len = size;
804 } else {
805 /* Using a 0-terminated buffer:
806
807 - the encoded string has to be 0-terminated
808 for this variant to work; if it is not, an
809 error raised
Fred Drake25871c02000-05-03 15:17:02 +0000810
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000811 - a new buffer of the needed size is
812 allocated and the data copied into it;
813 *buffer is updated to point to the new
814 buffer; the caller is responsible for
815 PyMem_Free()ing it after usage
816
817 */
818 if ((int)strlen(PyString_AS_STRING(s)) != size)
819 return converterr(
820 "(encoded string without NULL bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000821 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000822 *buffer = PyMem_NEW(char, size + 1);
823 if (*buffer == NULL) {
824 Py_DECREF(s);
825 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000826 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000827 }
828 memcpy(*buffer,
829 PyString_AS_STRING(s),
830 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000831 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000832 Py_DECREF(s);
833 break;
834 }
835
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000836#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000837 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
838 if (*format == '#') { /* any buffer-like object */
839 void **p = (void **)va_arg(*p_va, char **);
840 int *q = va_arg(*p_va, int *);
841 char *buf;
842 int count = convertbuffer(arg, p, &buf);
843
844 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000845 return converterr(buf, arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000846 *q = count/(sizeof(Py_UNICODE));
847 format++;
848 } else {
849 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
850
Guido van Rossume826ef02000-03-10 23:02:17 +0000851 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000852 *p = PyUnicode_AS_UNICODE(arg);
853 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000854 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000855 }
856 break;
857 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000858#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000859
860 case 'S': { /* string object */
861 PyObject **p = va_arg(*p_va, PyObject **);
862 if (PyString_Check(arg))
863 *p = arg;
864 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000865 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000866 break;
867 }
868
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000869#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000870 case 'U': { /* Unicode object */
871 PyObject **p = va_arg(*p_va, PyObject **);
872 if (PyUnicode_Check(arg))
873 *p = arg;
874 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000875 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000876 break;
877 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000878#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000879
880 case 'O': { /* object */
881 PyTypeObject *type;
882 PyObject **p;
883 if (*format == '!') {
884 type = va_arg(*p_va, PyTypeObject*);
885 p = va_arg(*p_va, PyObject **);
886 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +0000887 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +0000888 *p = arg;
889 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000890 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +0000891
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000892 }
893 else if (*format == '?') {
894 inquiry pred = va_arg(*p_va, inquiry);
895 p = va_arg(*p_va, PyObject **);
896 format++;
897 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000898 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000899 else
900 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000901 arg, msgbuf, bufsize);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000902
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000903 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000904 else if (*format == '&') {
905 typedef int (*converter)(PyObject *, void *);
906 converter convert = va_arg(*p_va, converter);
907 void *addr = va_arg(*p_va, void *);
908 format++;
909 if (! (*convert)(arg, addr))
910 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000911 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000912 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000913 else {
914 p = va_arg(*p_va, PyObject **);
915 *p = arg;
916 }
917 break;
918 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +0000919
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000920
921 case 'w': { /* memory buffer, read-write access */
922 void **p = va_arg(*p_va, void **);
923 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
924 int count;
925
926 if (pb == NULL ||
927 pb->bf_getwritebuffer == NULL ||
928 pb->bf_getsegcount == NULL)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000929 return converterr("read-write buffer", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000930 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
931 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000932 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000933 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000934 return converterr("(unspecified)", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000935 if (*format == '#') {
936 int *q = va_arg(*p_va, int *);
937
938 *q = count;
939 format++;
940 }
941 break;
942 }
943
944 case 't': { /* 8-bit character buffer, read-only access */
945 const char **p = va_arg(*p_va, const char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +0000946 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000947 int count;
948
949 if (*format++ != '#')
950 return converterr(
951 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000952 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000953 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +0000954 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
955 pb == NULL || pb->bf_getcharbuffer == NULL ||
956 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000957 return converterr(
958 "string or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000959 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000960
Jeremy Hylton4819e972001-10-11 14:40:37 +0000961 if (pb->bf_getsegcount(arg, NULL) != 1)
962 return converterr(
963 "string or single-segment read-only buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000964 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +0000965
966 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000967 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000968 return converterr("(unspecified)", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000969 *va_arg(*p_va, int *) = count;
970 break;
971 }
972
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000973 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000974 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000975
976 }
977
978 *p_format = format;
979 return NULL;
980}
Guido van Rossumaa354651996-08-19 19:32:04 +0000981
Fred Drake563dfc22001-10-23 14:41:08 +0000982static int
983convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000984{
985 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
986 int count;
987 if (pb == NULL ||
988 pb->bf_getreadbuffer == NULL ||
989 pb->bf_getsegcount == NULL) {
990 *errmsg = "string or read-only buffer";
991 return -1;
992 }
993 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
994 *errmsg = "string or single-segment read-only buffer";
995 return -1;
996 }
997 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
998 *errmsg = "(unspecified)";
999 }
1000 return count;
1001}
Guido van Rossumaa354651996-08-19 19:32:04 +00001002
1003/* Support for keyword arguments donated by
1004 Geoff Philbrick <philbric@delphi.hks.com> */
1005
Tim Petersf8cd3e82001-10-27 04:26:57 +00001006/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001007int
1008PyArg_ParseTupleAndKeywords(PyObject *args,
1009 PyObject *keywords,
1010 char *format,
1011 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001012{
1013 int retval;
1014 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001015
1016 if ((args == NULL || !PyTuple_Check(args)) ||
1017 (keywords != NULL && !PyDict_Check(keywords)) ||
1018 format == NULL ||
1019 kwlist == NULL)
1020 {
1021 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001022 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001023 }
1024
Guido van Rossumaa354651996-08-19 19:32:04 +00001025 va_start(va, kwlist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001026 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
1027 va_end(va);
1028 return retval;
1029}
1030
1031
1032static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001033vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
1034 char **kwlist, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001035{
Tim Petersdc5eff92001-10-27 06:53:00 +00001036 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001037 int levels[32];
Tim Petersf8cd3e82001-10-27 04:26:57 +00001038 char *fname, *message;
1039 int min, max;
Tim Peters6fb26352001-10-27 04:38:11 +00001040 char *formatsave;
Tim Petersb639d492001-10-27 07:00:56 +00001041 int i, len, nargs, nkeywords;
Tim Petersc2f01122001-10-27 07:25:06 +00001042 char *msg, **p;
Tim Petersf4331c12001-10-27 00:17:34 +00001043
Tim Peters45772cd2001-10-27 03:58:40 +00001044 assert(args != NULL && PyTuple_Check(args));
1045 assert(keywords == NULL || PyDict_Check(keywords));
1046 assert(format != NULL);
1047 assert(kwlist != NULL);
1048 assert(p_va != NULL);
1049
Tim Petersf8cd3e82001-10-27 04:26:57 +00001050 /* Search the format:
1051 message <- error msg, if any (else NULL).
Tim Peterscffed4b2001-11-29 03:26:37 +00001052 fname <- routine name, if any (else NULL).
Tim Petersf8cd3e82001-10-27 04:26:57 +00001053 min <- # of required arguments, or -1 if all are required.
1054 max <- most arguments (required + optional).
Tim Peters62d48e12001-10-27 06:42:16 +00001055 Check that kwlist has a non-NULL entry for each arg.
Tim Petersf8cd3e82001-10-27 04:26:57 +00001056 Raise error if a tuple arg spec is found.
1057 */
1058 fname = message = NULL;
Tim Peters6fb26352001-10-27 04:38:11 +00001059 formatsave = format;
Tim Peters62d48e12001-10-27 06:42:16 +00001060 p = kwlist;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001061 min = -1;
1062 max = 0;
1063 while ((i = *format++) != '\0') {
Tim Peters62d48e12001-10-27 06:42:16 +00001064 if (isalpha(i) && i != 'e') {
Tim Petersf8cd3e82001-10-27 04:26:57 +00001065 max++;
Tim Peters62d48e12001-10-27 06:42:16 +00001066 if (*p == NULL) {
Tim Peterscffed4b2001-11-29 03:26:37 +00001067 PyErr_SetString(PyExc_RuntimeError,
1068 "more argument specifiers than "
1069 "keyword list entries");
Tim Peters62d48e12001-10-27 06:42:16 +00001070 return 0;
1071 }
1072 p++;
1073 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001074 else if (i == '|')
1075 min = max;
1076 else if (i == ':') {
1077 fname = format;
1078 break;
1079 }
1080 else if (i == ';') {
1081 message = format;
1082 break;
1083 }
1084 else if (i == '(') {
Tim Peterscffed4b2001-11-29 03:26:37 +00001085 PyErr_SetString(PyExc_RuntimeError,
1086 "tuple found in format when using keyword "
1087 "arguments");
Guido van Rossumaa354651996-08-19 19:32:04 +00001088 return 0;
1089 }
Tim Peters62d48e12001-10-27 06:42:16 +00001090 }
1091 format = formatsave;
1092 if (*p != NULL) {
Tim Peterscffed4b2001-11-29 03:26:37 +00001093 PyErr_SetString(PyExc_RuntimeError,
1094 "more keyword list entries than "
1095 "argument specifiers");
Tim Peters62d48e12001-10-27 06:42:16 +00001096 return 0;
1097 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001098 if (min < 0) {
1099 /* All arguments are required. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001100 min = max;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001101 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001102
Tim Peters6fb26352001-10-27 04:38:11 +00001103 nargs = PyTuple_GET_SIZE(args);
Tim Petersb0872fc2001-10-27 04:45:34 +00001104 nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001105
Guido van Rossumaa354651996-08-19 19:32:04 +00001106 /* make sure there are no duplicate values for an argument;
1107 its not clear when to use the term "keyword argument vs.
1108 keyword parameter in messages */
Tim Petersb054be42001-10-27 05:07:41 +00001109 if (nkeywords > 0) {
Tim Peters6fb26352001-10-27 04:38:11 +00001110 for (i = 0; i < nargs; i++) {
Tim Petersa9f47392001-10-27 00:46:09 +00001111 char *thiskw = kwlist[i];
1112 if (thiskw == NULL)
1113 break;
Tim Peters077f5742001-10-27 05:50:39 +00001114 if (PyDict_GetItemString(keywords, thiskw)) {
Tim Petersb054be42001-10-27 05:07:41 +00001115 PyErr_Format(PyExc_TypeError,
1116 "keyword parameter '%s' was given "
1117 "by position and by name",
Tim Petersa9f47392001-10-27 00:46:09 +00001118 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001119 return 0;
1120 }
Tim Peters0af49162001-10-27 06:14:32 +00001121 else if (PyErr_Occurred())
1122 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001123 }
1124 }
Tim Peters61dde632001-10-27 05:30:17 +00001125
Guido van Rossumaa354651996-08-19 19:32:04 +00001126 /* required arguments missing from args can be supplied by keyword
Tim Peters62d48e12001-10-27 06:42:16 +00001127 arguments; set len to the number of posiitional arguments, and,
1128 if that's less than the minimum required, add in the number of
1129 required arguments that are supplied by keywords */
Tim Peters6fb26352001-10-27 04:38:11 +00001130 len = nargs;
Tim Peters62d48e12001-10-27 06:42:16 +00001131 if (nkeywords > 0 && nargs < min) {
Tim Peters6fb26352001-10-27 04:38:11 +00001132 for (i = nargs; i < min; i++) {
Tim Peters077f5742001-10-27 05:50:39 +00001133 if (PyDict_GetItemString(keywords, kwlist[i]))
Guido van Rossumaa354651996-08-19 19:32:04 +00001134 len++;
Tim Peters0af49162001-10-27 06:14:32 +00001135 else if (PyErr_Occurred())
1136 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001137 }
1138 }
Tim Peters0af49162001-10-27 06:14:32 +00001139
Guido van Rossumaa354651996-08-19 19:32:04 +00001140 /* make sure we got an acceptable number of arguments; the message
1141 is a little confusing with keywords since keyword arguments
1142 which are supplied, but don't match the required arguments
1143 are not included in the "%d given" part of the message */
Guido van Rossumaa354651996-08-19 19:32:04 +00001144 if (len < min || max < len) {
1145 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +00001146 PyOS_snprintf(msgbuf, sizeof(msgbuf),
1147 "%.200s%s takes %s %d argument%s "
1148 "(%d given)",
1149 fname==NULL ? "function" : fname,
1150 fname==NULL ? "" : "()",
1151 min==max ? "exactly"
1152 : len < min ? "at least" : "at most",
1153 len < min ? min : max,
1154 (len < min ? min : max) == 1 ? "" : "s",
1155 len);
Guido van Rossumaa354651996-08-19 19:32:04 +00001156 message = msgbuf;
1157 }
1158 PyErr_SetString(PyExc_TypeError, message);
1159 return 0;
1160 }
Tim Petersc2f01122001-10-27 07:25:06 +00001161
1162 /* convert the positional arguments */
Tim Peters6fb26352001-10-27 04:38:11 +00001163 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001164 if (*format == '|')
1165 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001166 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001167 levels, msgbuf, sizeof(msgbuf));
Guido van Rossumaa354651996-08-19 19:32:04 +00001168 if (msg) {
1169 seterror(i+1, msg, levels, fname, message);
1170 return 0;
1171 }
1172 }
1173
Tim Petersc2f01122001-10-27 07:25:06 +00001174 /* handle no keyword parameters in call */
Tim Petersb054be42001-10-27 05:07:41 +00001175 if (nkeywords == 0)
Tim Peters28bf7a92001-10-27 04:33:41 +00001176 return 1;
Tim Petersb054be42001-10-27 05:07:41 +00001177
Guido van Rossumaa354651996-08-19 19:32:04 +00001178 /* convert the keyword arguments; this uses the format
1179 string where it was left after processing args */
Tim Petersb639d492001-10-27 07:00:56 +00001180 for (i = nargs; i < max; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001182 if (*format == '|')
1183 format++;
Tim Peters077f5742001-10-27 05:50:39 +00001184 item = PyDict_GetItemString(keywords, kwlist[i]);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001185 if (item != NULL) {
Tim Peters077f5742001-10-27 05:50:39 +00001186 Py_INCREF(item);
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001187 msg = convertitem(item, &format, p_va, levels, msgbuf,
1188 sizeof(msgbuf));
Tim Peters077f5742001-10-27 05:50:39 +00001189 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001190 if (msg) {
1191 seterror(i+1, msg, levels, fname, message);
1192 return 0;
1193 }
Tim Petersc2f01122001-10-27 07:25:06 +00001194 --nkeywords;
1195 if (nkeywords == 0)
1196 break;
Guido van Rossumaa354651996-08-19 19:32:04 +00001197 }
Tim Peters0af49162001-10-27 06:14:32 +00001198 else if (PyErr_Occurred())
1199 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001200 else {
Guido van Rossumaa354651996-08-19 19:32:04 +00001201 msg = skipitem(&format, p_va);
1202 if (msg) {
1203 seterror(i+1, msg, levels, fname, message);
1204 return 0;
1205 }
1206 }
1207 }
Tim Petersb054be42001-10-27 05:07:41 +00001208
Guido van Rossumaa354651996-08-19 19:32:04 +00001209 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001210 if (nkeywords > 0) {
1211 PyObject *key, *value;
1212 int pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001213 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001214 int match = 0;
1215 char *ks = PyString_AsString(key);
Tim Petersb639d492001-10-27 07:00:56 +00001216 for (i = 0; i < max; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001217 if (!strcmp(ks, kwlist[i])) {
1218 match = 1;
1219 break;
1220 }
1221 }
1222 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001223 PyErr_Format(PyExc_TypeError,
1224 "'%s' is an invalid keyword "
1225 "argument for this function",
1226 ks);
Guido van Rossumaa354651996-08-19 19:32:04 +00001227 return 0;
1228 }
1229 }
1230 }
Tim Petersc2f01122001-10-27 07:25:06 +00001231
Guido van Rossumaa354651996-08-19 19:32:04 +00001232 return 1;
1233}
1234
1235
1236static char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001237skipitem(char **p_format, va_list *p_va)
Guido van Rossumaa354651996-08-19 19:32:04 +00001238{
1239 char *format = *p_format;
1240 char c = *format++;
1241
1242 switch (c) {
1243
1244 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001245 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001246 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001247 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001248 break;
1249 }
1250
1251 case 'h': /* short int */
1252 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001253 (void) va_arg(*p_va, short *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001254 break;
1255 }
1256
Jack Jansencc22fbe2000-08-05 21:29:58 +00001257 case 'H': /* short int as bitfield */
Jack Jansend50338f2000-07-06 12:22:00 +00001258 {
1259 (void) va_arg(*p_va, unsigned short *);
1260 break;
1261 }
1262
Guido van Rossumaa354651996-08-19 19:32:04 +00001263 case 'i': /* int */
1264 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001265 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001266 break;
1267 }
1268
1269 case 'l': /* long int */
1270 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001271 (void) va_arg(*p_va, long *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001272 break;
1273 }
1274
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001275#ifdef HAVE_LONG_LONG
Guido van Rossum3293b071998-08-25 16:07:15 +00001276 case 'L': /* LONG_LONG int */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001277 {
Guido van Rossum3293b071998-08-25 16:07:15 +00001278 (void) va_arg(*p_va, LONG_LONG *);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001279 break;
1280 }
1281#endif
1282
Guido van Rossumaa354651996-08-19 19:32:04 +00001283 case 'f': /* float */
1284 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001285 (void) va_arg(*p_va, float *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001286 break;
1287 }
1288
1289 case 'd': /* double */
1290 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001291 (void) va_arg(*p_va, double *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001292 break;
1293 }
1294
1295#ifndef WITHOUT_COMPLEX
1296 case 'D': /* complex double */
1297 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001298 (void) va_arg(*p_va, Py_complex *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001299 break;
1300 }
1301#endif /* WITHOUT_COMPLEX */
1302
1303 case 'c': /* char */
1304 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001305 (void) va_arg(*p_va, char *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001306 break;
1307 }
1308
1309 case 's': /* string */
1310 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001311 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001312 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001313 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001314 format++;
1315 }
1316 break;
1317 }
1318
1319 case 'z': /* string */
1320 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001321 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001322 if (*format == '#') {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001323 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001324 format++;
1325 }
1326 break;
1327 }
1328
1329 case 'S': /* string object */
1330 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001332 break;
1333 }
1334
1335 case 'O': /* object */
1336 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001337 if (*format == '!') {
1338 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001339 (void) va_arg(*p_va, PyTypeObject*);
1340 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001341 }
1342#if 0
1343/* I don't know what this is for */
1344 else if (*format == '?') {
1345 inquiry pred = va_arg(*p_va, inquiry);
1346 format++;
1347 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001348 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001349 }
1350 }
1351#endif
1352 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001353 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001354 (void) va_arg(*p_va, converter);
1355 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001356 format++;
1357 }
1358 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001359 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001360 }
1361 break;
1362 }
1363
1364 default:
1365 return "impossible<bad format char>";
1366
1367 }
1368
1369 *p_format = format;
1370 return NULL;
1371}
Fred Drakee4616e62001-10-23 21:09:29 +00001372
1373
1374int
1375PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
1376{
1377 int i, l;
1378 PyObject **o;
1379 va_list vargs;
1380
1381#ifdef HAVE_STDARG_PROTOTYPES
1382 va_start(vargs, max);
1383#else
1384 va_start(vargs);
1385#endif
1386
1387 assert(min >= 0);
1388 assert(min <= max);
1389 if (!PyTuple_Check(args)) {
1390 PyErr_SetString(PyExc_SystemError,
1391 "PyArg_UnpackTuple() argument list is not a tuple");
1392 return 0;
1393 }
1394 l = PyTuple_GET_SIZE(args);
1395 if (l < min) {
1396 if (name != NULL)
1397 PyErr_Format(
1398 PyExc_TypeError,
1399 "%s expected %s%d arguments, got %d",
1400 name, (min == max ? "" : "at least "), min, l);
1401 else
1402 PyErr_Format(
1403 PyExc_TypeError,
1404 "unpacked tuple should have %s%d elements,"
1405 " but has %d",
1406 (min == max ? "" : "at least "), min, l);
1407 va_end(vargs);
1408 return 0;
1409 }
1410 if (l > max) {
1411 if (name != NULL)
1412 PyErr_Format(
1413 PyExc_TypeError,
1414 "%s expected %s%d arguments, got %d",
1415 name, (min == max ? "" : "at most "), max, l);
1416 else
1417 PyErr_Format(
1418 PyExc_TypeError,
1419 "unpacked tuple should have %s%d elements,"
1420 " but has %d",
1421 (min == max ? "" : "at most "), max, l);
1422 va_end(vargs);
1423 return 0;
1424 }
1425 for (i = 0; i < l; i++) {
1426 o = va_arg(vargs, PyObject **);
1427 *o = PyTuple_GET_ITEM(args, i);
1428 }
1429 va_end(vargs);
1430 return 1;
1431}