getargs.c 33.9 KB
Newer Older
1 2 3

/* New getargs implementation */

4
#include "Python.h"
5

6 7
#include <ctype.h>

8

9 10 11
int PyArg_Parse(PyObject *, char *, ...);
int PyArg_ParseTuple(PyObject *, char *, ...);
int PyArg_VaParse(PyObject *, char *, va_list);
12

13 14
int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
				char *, char **, ...);
15 16

/* Forward */
17 18
static int vgetargs1(PyObject *, char *, va_list *, int);
static void seterror(int, char *, int *, char *, char *);
19 20
static char *convertitem(PyObject *, char **, va_list *, int *, char *, 
			 size_t);
21
static char *converttuple(PyObject *, char **, va_list *,
22 23
			  int *, char *, size_t, int);
static char *convertsimple(PyObject *, char **, va_list *, char *, size_t);
24
static int convertbuffer(PyObject *, void **p, char **);
25 26 27 28

static int vgetargskeywords(PyObject *, PyObject *,
			    char *, char **, va_list *);
static char *skipitem(char **, va_list *);
29

30 31
int
PyArg_Parse(PyObject *args, char *format, ...)
32 33 34 35 36
{
	int retval;
	va_list va;
	
	va_start(va, format);
37
	retval = vgetargs1(args, format, &va, 1);
38 39 40 41 42
	va_end(va);
	return retval;
}


43 44
int
PyArg_ParseTuple(PyObject *args, char *format, ...)
45 46 47 48 49
{
	int retval;
	va_list va;
	
	va_start(va, format);
50
	retval = vgetargs1(args, format, &va, 0);
51 52 53 54 55 56
	va_end(va);
	return retval;
}


int
57
PyArg_VaParse(PyObject *args, char *format, va_list va)
58
{
59 60 61 62
	va_list lva;

#ifdef VA_LIST_IS_ARRAY
	memcpy(lva, va, sizeof(va_list));
63 64 65
#else
#ifdef __va_copy
	__va_copy(lva, va);
66 67
#else
	lva = va;
68
#endif
69 70 71
#endif

	return vgetargs1(args, format, &lva, 0);
72 73 74 75
}


static int
76
vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
77 78 79 80 81 82 83 84
{
	char msgbuf[256];
	int levels[32];
	char *fname = NULL;
	char *message = NULL;
	int min = -1;
	int max = 0;
	int level = 0;
85
	int endfmt = 0;
86 87 88 89
	char *formatsave = format;
	int i, len;
	char *msg;
	
90 91
	assert(compat || (args != (PyObject*)NULL));

92
	while (endfmt == 0) {
93
		int c = *format++;
94 95
		switch (c) {
		case '(':
96 97 98
			if (level == 0)
				max++;
			level++;
99 100
			break;
		case ')':
101
			if (level == 0)
102
				Py_FatalError("excess ')' in getargs format");
103 104 105
			else
				level--;
			break;
106 107 108 109
		case '\0':
			endfmt = 1;
			break;
		case ':':
110
			fname = format;
111
			endfmt = 1;
112
			break;
113
		case ';':
114
			message = format;
115 116 117 118 119 120 121 122 123 124 125 126
			endfmt = 1;
			break;
		default:
			if (level == 0) {
				if (c == 'O')
					max++;
				else if (isalpha(c)) {
					if (c != 'e') /* skip encoded */
						max++;
				} else if (c == '|')
					min = max;
			}
127 128 129 130 131
			break;
		}
	}
	
	if (level != 0)
132
		Py_FatalError(/* '(' */ "missing ')' in getargs format");
133 134 135 136 137 138 139 140 141 142
	
	if (min < 0)
		min = max;
	
	format = formatsave;
	
	if (compat) {
		if (max == 0) {
			if (args == NULL)
				return 1;
143 144 145 146
			PyOS_snprintf(msgbuf, sizeof(msgbuf),
				      "%.200s%s takes no arguments",
				      fname==NULL ? "function" : fname,
				      fname==NULL ? "" : "()");
147
			PyErr_SetString(PyExc_TypeError, msgbuf);
148 149 150
			return 0;
		}
		else if (min == 1 && max == 1) {
151
			if (args == NULL) {
152 153 154 155
				PyOS_snprintf(msgbuf, sizeof(msgbuf),
				      "%.200s%s takes at least one argument",
					      fname==NULL ? "function" : fname,
					      fname==NULL ? "" : "()");
156
				PyErr_SetString(PyExc_TypeError, msgbuf);
157 158
				return 0;
			}
159 160
			msg = convertitem(args, &format, p_va, levels, msgbuf,
					  sizeof(msgbuf));
161 162 163 164 165 166
			if (msg == NULL)
				return 1;
			seterror(levels[0], msg, levels+1, fname, message);
			return 0;
		}
		else {
167
			PyErr_SetString(PyExc_SystemError,
168 169 170 171 172
			    "old style getargs format uses new features");
			return 0;
		}
	}
	
173 174
	if (!PyTuple_Check(args)) {
		PyErr_SetString(PyExc_SystemError,
175 176 177 178
		    "new style getargs format but argument is not a tuple");
		return 0;
	}
	
179
	len = PyTuple_GET_SIZE(args);
180 181 182
	
	if (len < min || max < len) {
		if (message == NULL) {
183 184 185 186 187 188 189 190 191 192
			PyOS_snprintf(msgbuf, sizeof(msgbuf),
				      "%.150s%s takes %s %d argument%s "
				      "(%d given)",
				      fname==NULL ? "function" : fname,
				      fname==NULL ? "" : "()",
				      min==max ? "exactly"
				      : len < min ? "at least" : "at most",
				      len < min ? min : max,
				      (len < min ? min : max) == 1 ? "" : "s",
				      len);
193 194
			message = msgbuf;
		}
195
		PyErr_SetString(PyExc_TypeError, message);
196 197 198 199 200 201
		return 0;
	}
	
	for (i = 0; i < len; i++) {
		if (*format == '|')
			format++;
202
		msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
203
				  levels, msgbuf, sizeof(msgbuf));
204 205 206 207 208
		if (msg) {
			seterror(i+1, msg, levels, fname, message);
			return 0;
		}
	}
209

210
	if (*format != '\0' && !isalpha((int)(*format)) &&
211
	    *format != '(' &&
212 213
	    *format != '|' && *format != ':' && *format != ';') {
		PyErr_Format(PyExc_SystemError,
214
			     "bad format string: %.200s", formatsave);
215 216
		return 0;
	}
217 218 219 220 221 222 223
	
	return 1;
}



static void
224
seterror(int iarg, char *msg, int *levels, char *fname, char *message)
225
{
226
	char buf[512];
227 228 229
	int i;
	char *p = buf;

230
	if (PyErr_Occurred())
231
		return;
232 233
	else if (message == NULL) {
		if (fname != NULL) {
234
			PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
235 236
			p += strlen(p);
		}
237
		if (iarg != 0) {
238
			PyOS_snprintf(p, sizeof(buf) - (p - buf),
239
				      "argument %d", iarg);
240 241
			i = 0;
			p += strlen(p);
242
			while (levels[i] > 0 && (int)(p-buf) < 220) {
243 244
				PyOS_snprintf(p, sizeof(buf) - (buf - p),
					      ", item %d", levels[i]-1);
245 246 247 248 249
				p += strlen(p);
				i++;
			}
		}
		else {
250
			PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
251 252
			p += strlen(p);
		}
253
		PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
254 255
		message = buf;
	}
256
	PyErr_SetString(PyExc_TypeError, message);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}


/* Convert a tuple argument.
   On entry, *p_format points to the character _after_ the opening '('.
   On successful exit, *p_format points to the closing ')'.
   If successful:
      *p_format and *p_va are updated,
      *levels and *msgbuf are untouched,
      and NULL is returned.
   If the argument is invalid:
      *p_format is unchanged,
      *p_va is undefined,
      *levels is a 0-terminated list of item numbers,
      *msgbuf contains an error message, whose format is:
272
         "must be <typename1>, not <typename2>", where:
273 274 275 276 277 278
            <typename1> is the name of the expected type, and
            <typename2> is the name of the actual type,
      and msgbuf is returned.
*/

static char *
279
converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
280
	     char *msgbuf, size_t bufsize, int toplevel)
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
{
	int level = 0;
	int n = 0;
	char *format = *p_format;
	int i;
	
	for (;;) {
		int c = *format++;
		if (c == '(') {
			if (level == 0)
				n++;
			level++;
		}
		else if (c == ')') {
			if (level == 0)
				break;
			level--;
		}
		else if (c == ':' || c == ';' || c == '\0')
			break;
		else if (level == 0 && isalpha(c))
			n++;
	}
	
305
	if (!PySequence_Check(arg) || PyString_Check(arg)) {
306
		levels[0] = 0;
307
		PyOS_snprintf(msgbuf, bufsize,
308 309 310 311
			      toplevel ? "expected %d arguments, not %.50s" :
			              "must be %d-item sequence, not %.50s",
			      n, 
			      arg == Py_None ? "None" : arg->ob_type->tp_name);
312 313 314
		return msgbuf;
	}
	
315
	if ((i = PySequence_Size(arg)) != n) {
316
		levels[0] = 0;
317
		PyOS_snprintf(msgbuf, bufsize,
318 319 320
			      toplevel ? "expected %d arguments, not %d" :
			             "must be sequence of length %d, not %d",
			      n, i);
321 322
		return msgbuf;
	}
323

324 325 326
	format = *p_format;
	for (i = 0; i < n; i++) {
		char *msg;
327 328
		PyObject *item;
		item = PySequence_GetItem(arg, i);
329 330
		msg = convertitem(item, &format, p_va, levels+1, msgbuf,
				  bufsize);
331 332
		/* PySequence_GetItem calls tp->sq_item, which INCREFs */
		Py_XDECREF(item);
333 334 335 336 337
		if (msg != NULL) {
			levels[0] = i+1;
			return msg;
		}
	}
338

339 340 341 342 343 344 345 346
	*p_format = format;
	return NULL;
}


/* Convert a single item. */

static char *
347
convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
348
	    char *msgbuf, size_t bufsize)
349 350 351 352 353 354
{
	char *msg;
	char *format = *p_format;
	
	if (*format == '(' /* ')' */) {
		format++;
355 356
		msg = converttuple(arg, &format, p_va, levels, msgbuf, 
				   bufsize, 0);
357 358 359 360
		if (msg == NULL)
			format++;
	}
	else {
361
		msg = convertsimple(arg, &format, p_va, msgbuf, bufsize);
362 363 364 365 366 367 368 369 370
		if (msg != NULL)
			levels[0] = 0;
	}
	if (msg == NULL)
		*p_format = format;
	return msg;
}


371 372 373 374 375

#define UNICODE_DEFAULT_ENCODING(arg) \
        _PyUnicode_AsDefaultEncodedString(arg, NULL)

/* Format an error message generated by convertsimple(). */
376 377

static char *
378
converterr(char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
379
{
380 381
	assert(expected != NULL);
	assert(arg != NULL); 
382 383 384
	PyOS_snprintf(msgbuf, bufsize,
		      "must be %.50s, not %.50s", expected,
		      arg == Py_None ? "None" : arg->ob_type->tp_name);
385
	return msgbuf;
386 387
}

388
#define CONV_UNICODE "(unicode conversion error)"
Guido van Rossum's avatar
Guido van Rossum committed
389

390 391 392 393 394 395 396 397 398 399 400 401 402
/* explicitly check for float arguments when integers are expected.  For now
 * signal a warning.  Returns true if an exception was raised. */
static int
float_argument_error(PyObject *arg)
{
	if (PyFloat_Check(arg) &&
	    PyErr_Warn(PyExc_DeprecationWarning,
		       "integer argument expected, got float" ))
		return 1;
	else
		return 0;
}

403
/* Convert a non-tuple argument.  Return NULL if conversion went OK,
404 405
   or a string with a message describing the failure.  The message is
   formatted as "must be <desired type>, not <actual type>".
406
   When failing, an exception may or may not have been raised.
407 408
   Don't call if a tuple is expected. 
*/
409 410

static char *
411 412
convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf,
	      size_t bufsize)
413 414 415
{
	char *format = *p_format;
	char c = *format++;
416
#ifdef Py_USING_UNICODE
417
	PyObject *uarg;
418
#endif
419 420 421
	
	switch (c) {
	
422 423
	case 'b': { /* unsigned byte -- very short int */
		char *p = va_arg(*p_va, char *);
424
		long ival;
425 426
		if (float_argument_error(arg))
			return NULL;
427
		ival = PyInt_AsLong(arg);
428
		if (ival == -1 && PyErr_Occurred())
429
			return converterr("integer<b>", arg, msgbuf, bufsize);
430 431 432
		else if (ival < 0) {
			PyErr_SetString(PyExc_OverflowError,
			"unsigned byte integer is less than minimum");
433
			return converterr("integer<b>", arg, msgbuf, bufsize);
434 435 436 437
		}
		else if (ival > UCHAR_MAX) {
			PyErr_SetString(PyExc_OverflowError,
			"unsigned byte integer is greater than maximum");
438
			return converterr("integer<b>", arg, msgbuf, bufsize);
439 440 441 442 443
		}
		else
			*p = (unsigned char) ival;
		break;
	}
444
	
445 446 447
	case 'B': {/* byte sized bitfield - both signed and unsigned
		      values allowed */  
		char *p = va_arg(*p_va, char *);
448
		long ival;
449 450
		if (float_argument_error(arg))
			return NULL;
451
		ival = PyInt_AsLong(arg);
452
		if (ival == -1 && PyErr_Occurred())
453
			return converterr("integer<b>", arg, msgbuf, bufsize);
454 455 456
		else if (ival < SCHAR_MIN) {
			PyErr_SetString(PyExc_OverflowError,
			"byte-sized integer bitfield is less than minimum");
457
			return converterr("integer<B>", arg, msgbuf, bufsize);
458 459 460 461
		}
		else if (ival > (int)UCHAR_MAX) {
			PyErr_SetString(PyExc_OverflowError,
			"byte-sized integer bitfield is greater than maximum");
462
			return converterr("integer<B>", arg, msgbuf, bufsize);
463 464 465 466 467
		}
		else
			*p = (unsigned char) ival;
		break;
	}
468
	
469 470
	case 'h': {/* signed short int */
		short *p = va_arg(*p_va, short *);
471
		long ival;
472 473
		if (float_argument_error(arg))
			return NULL;
474
		ival = PyInt_AsLong(arg);
475
		if (ival == -1 && PyErr_Occurred())
476
			return converterr("integer<h>", arg, msgbuf, bufsize);
477 478 479
		else if (ival < SHRT_MIN) {
			PyErr_SetString(PyExc_OverflowError,
			"signed short integer is less than minimum");
480
			return converterr("integer<h>", arg, msgbuf, bufsize);
481 482 483 484
		}
		else if (ival > SHRT_MAX) {
			PyErr_SetString(PyExc_OverflowError,
			"signed short integer is greater than maximum");
485
			return converterr("integer<h>", arg, msgbuf, bufsize);
486 487 488 489 490
		}
		else
			*p = (short) ival;
		break;
	}
491
	
492 493 494
	case 'H': { /* short int sized bitfield, both signed and
		       unsigned allowed */ 
		unsigned short *p = va_arg(*p_va, unsigned short *);
495
		long ival;
496 497
		if (float_argument_error(arg))
			return NULL;
498
		ival = PyInt_AsLong(arg);
499
		if (ival == -1 && PyErr_Occurred())
500
			return converterr("integer<H>", arg, msgbuf, bufsize);
501 502 503
		else if (ival < SHRT_MIN) {
			PyErr_SetString(PyExc_OverflowError,
			"short integer bitfield is less than minimum");
504
			return converterr("integer<H>", arg, msgbuf, bufsize);
505 506 507 508
		}
		else if (ival > USHRT_MAX) {
			PyErr_SetString(PyExc_OverflowError,
			"short integer bitfield is greater than maximum");
509
			return converterr("integer<H>", arg, msgbuf, bufsize);
510 511 512 513 514
		}
		else
			*p = (unsigned short) ival;
		break;
	}
515
	
516 517
	case 'i': {/* signed int */
		int *p = va_arg(*p_va, int *);
518
		long ival;
519 520
		if (float_argument_error(arg))
			return NULL;
521
		ival = PyInt_AsLong(arg);
522
		if (ival == -1 && PyErr_Occurred())
523
			return converterr("integer<i>", arg, msgbuf, bufsize);
524 525 526
		else if (ival > INT_MAX) {
			PyErr_SetString(PyExc_OverflowError,
				"signed integer is greater than maximum");
527
			return converterr("integer<i>", arg, msgbuf, bufsize);
528 529 530 531
		}
		else if (ival < INT_MIN) {
			PyErr_SetString(PyExc_OverflowError,
				"signed integer is less than minimum");
532
			return converterr("integer<i>", arg, msgbuf, bufsize);
533 534 535 536 537 538 539 540
		}
		else
			*p = ival;
		break;
	}

	case 'l': {/* long int */
		long *p = va_arg(*p_va, long *);
541
		long ival;
542 543
		if (float_argument_error(arg))
			return NULL;
544
		ival = PyInt_AsLong(arg);
545
		if (ival == -1 && PyErr_Occurred())
546
			return converterr("integer<l>", arg, msgbuf, bufsize);
547 548 549 550
		else
			*p = ival;
		break;
	}
551
	
552
#ifdef HAVE_LONG_LONG
553 554 555 556
	case 'L': {/* LONG_LONG */
		LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
		LONG_LONG ival = PyLong_AsLongLong( arg );
		if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
557
			return converterr("long<L>", arg, msgbuf, bufsize);
558 559 560 561 562
		} else {
			*p = ival;
		}
		break;
	}
563 564
#endif
	
565 566 567 568
	case 'f': {/* float */
		float *p = va_arg(*p_va, float *);
		double dval = PyFloat_AsDouble(arg);
		if (PyErr_Occurred())
569
			return converterr("float<f>", arg, msgbuf, bufsize);
570 571 572 573
		else
			*p = (float) dval;
		break;
	}
574
	
575 576 577 578
	case 'd': {/* double */
		double *p = va_arg(*p_va, double *);
		double dval = PyFloat_AsDouble(arg);
		if (PyErr_Occurred())
579
			return converterr("float<d>", arg, msgbuf, bufsize);
580 581 582 583
		else
			*p = dval;
		break;
	}
584
	
585
#ifndef WITHOUT_COMPLEX
586 587 588 589 590
	case 'D': {/* complex double */
		Py_complex *p = va_arg(*p_va, Py_complex *);
		Py_complex cval;
		cval = PyComplex_AsCComplex(arg);
		if (PyErr_Occurred())
591
			return converterr("complex<D>", arg, msgbuf, bufsize);
592 593 594 595
		else
			*p = cval;
		break;
	}
596
#endif /* WITHOUT_COMPLEX */
597
	
598 599 600
	case 'c': {/* char */
		char *p = va_arg(*p_va, char *);
		if (PyString_Check(arg) && PyString_Size(arg) == 1)
601
			*p = PyString_AS_STRING(arg)[0];
602
		else
603
			return converterr("char", arg, msgbuf, bufsize);
604 605
		break;
	}
606
	
607 608 609 610
	case 's': {/* string */
		if (*format == '#') {
			void **p = (void **)va_arg(*p_va, char **);
			int *q = va_arg(*p_va, int *);
611
			
612 613 614
			if (PyString_Check(arg)) {
				*p = PyString_AS_STRING(arg);
				*q = PyString_GET_SIZE(arg);
615
			}
616
#ifdef Py_USING_UNICODE
617
			else if (PyUnicode_Check(arg)) {
618 619
				uarg = UNICODE_DEFAULT_ENCODING(arg);
				if (uarg == NULL)
620
					return converterr(CONV_UNICODE,
621
							  arg, msgbuf, bufsize);
622 623
				*p = PyString_AS_STRING(uarg);
				*q = PyString_GET_SIZE(uarg);
624
			}
625
#endif
626 627 628 629
			else { /* any buffer-like object */
				char *buf;
				int count = convertbuffer(arg, p, &buf);
				if (count < 0)
630
					return converterr(buf, arg, msgbuf, bufsize);
631 632 633 634 635 636 637 638
				*q = count;
			}
			format++;
		} else {
			char **p = va_arg(*p_va, char **);
			
			if (PyString_Check(arg))
				*p = PyString_AS_STRING(arg);
639
#ifdef Py_USING_UNICODE
640
			else if (PyUnicode_Check(arg)) {
641 642
				uarg = UNICODE_DEFAULT_ENCODING(arg);
				if (uarg == NULL)
643
					return converterr(CONV_UNICODE,
644
							  arg, msgbuf, bufsize);
645
				*p = PyString_AS_STRING(uarg);
646
			}
647
#endif
648
			else
649
				return converterr("string", arg, msgbuf, bufsize);
650 651
			if ((int)strlen(*p) != PyString_Size(arg))
				return converterr("string without null bytes",
652
						  arg, msgbuf, bufsize);
653
		}
654 655
		break;
	}
656

657 658 659 660 661 662 663 664 665 666 667 668 669
	case 'z': {/* string, may be NULL (None) */
		if (*format == '#') { /* any buffer-like object */
			void **p = (void **)va_arg(*p_va, char **);
			int *q = va_arg(*p_va, int *);
			
			if (arg == Py_None) {
				*p = 0;
				*q = 0;
			}
			else if (PyString_Check(arg)) {
				*p = PyString_AS_STRING(arg);
				*q = PyString_GET_SIZE(arg);
			}
670
#ifdef Py_USING_UNICODE
671
			else if (PyUnicode_Check(arg)) {
672 673
				uarg = UNICODE_DEFAULT_ENCODING(arg);
				if (uarg == NULL)
674
					return converterr(CONV_UNICODE,
675
							  arg, msgbuf, bufsize);
676 677
				*p = PyString_AS_STRING(uarg);
				*q = PyString_GET_SIZE(uarg);
678
			}
679
#endif
680 681 682 683
			else { /* any buffer-like object */
				char *buf;
				int count = convertbuffer(arg, p, &buf);
				if (count < 0)
684
					return converterr(buf, arg, msgbuf, bufsize);
685 686 687 688 689
				*q = count;
			}
			format++;
		} else {
			char **p = va_arg(*p_va, char **);
690
			
691 692 693
			if (arg == Py_None)
				*p = 0;
			else if (PyString_Check(arg))
694
				*p = PyString_AS_STRING(arg);
695
#ifdef Py_USING_UNICODE
696
			else if (PyUnicode_Check(arg)) {
697 698
				uarg = UNICODE_DEFAULT_ENCODING(arg);
				if (uarg == NULL)
699
					return converterr(CONV_UNICODE,
700
							  arg, msgbuf, bufsize);
701
				*p = PyString_AS_STRING(uarg);
702
			}
703
#endif
704 705
			else
				return converterr("string or None", 
706
						  arg, msgbuf, bufsize);
707 708
			if (*format == '#') {
				int *q = va_arg(*p_va, int *);
Guido van Rossum's avatar
Guido van Rossum committed
709
				if (arg == Py_None)
710
					*q = 0;
711
				else
712 713
					*q = PyString_Size(arg);
				format++;
714
			}
715 716 717 718
			else if (*p != NULL &&
				 (int)strlen(*p) != PyString_Size(arg))
				return converterr(
					"string without null bytes or None", 
719
					arg, msgbuf, bufsize);
720
		}
721 722
		break;
	}
723
	
724 725 726
	case 'e': {/* encoded string */
		char **buffer;
		const char *encoding;
727
		PyObject *s;
728
		int size, recode_strings;
Guido van Rossum's avatar
Guido van Rossum committed
729

730 731
		/* Get 'e' parameter: the encoding name */
		encoding = (const char *)va_arg(*p_va, const char *);
732
#ifdef Py_USING_UNICODE
733 734
		if (encoding == NULL)
			encoding = PyUnicode_GetDefaultEncoding();
735
#endif
Guido van Rossum's avatar
Guido van Rossum committed
736
			
737 738 739 740 741 742 743 744 745 746 747
		/* Get output buffer parameter:
		   's' (recode all objects via Unicode) or
		   't' (only recode non-string objects) 
		*/
		if (*format == 's')
			recode_strings = 1;
		else if (*format == 't')
			recode_strings = 0;
		else
			return converterr(
				"(unknown parser marker combination)",
748
				arg, msgbuf, bufsize);
749 750 751 752
		buffer = (char **)va_arg(*p_va, char **);
		format++;
		if (buffer == NULL)
			return converterr("(buffer is NULL)", 
753
					  arg, msgbuf, bufsize);
Guido van Rossum's avatar
Guido van Rossum committed
754
			
755 756 757 758 759 760
		/* Encode object */
		if (!recode_strings && PyString_Check(arg)) {
			s = arg;
			Py_INCREF(s);
		}
		else {
761 762 763
#ifdef Py_USING_UNICODE
		    	PyObject *u;

Guido van Rossum's avatar
Guido van Rossum committed
764 765 766
			/* Convert object to Unicode */
			u = PyUnicode_FromObject(arg);
			if (u == NULL)
767 768
				return converterr(
					"string or unicode or text buffer", 
769
					arg, msgbuf, bufsize);
Guido van Rossum's avatar
Guido van Rossum committed
770 771 772 773 774 775 776
			
			/* Encode object; use default error handling */
			s = PyUnicode_AsEncodedString(u,
						      encoding,
						      NULL);
			Py_DECREF(u);
			if (s == NULL)
777
				return converterr("(encoding failed)",
778
						  arg, msgbuf, bufsize);
Guido van Rossum's avatar
Guido van Rossum committed
779 780
			if (!PyString_Check(s)) {
				Py_DECREF(s);
781 782
				return converterr(
					"(encoder failed to return a string)",
783
					arg, msgbuf, bufsize);
Guido van Rossum's avatar
Guido van Rossum committed
784
			}
785
#else
786
			return converterr("string<e>", arg, msgbuf, bufsize);
787
#endif
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
		}
		size = PyString_GET_SIZE(s);

		/* Write output; output is guaranteed to be 0-terminated */
		if (*format == '#') { 
			/* Using buffer length parameter '#':
				   
			   - if *buffer is NULL, a new buffer of the
			   needed size is allocated and the data
			   copied into it; *buffer is updated to point
			   to the new buffer; the caller is
			   responsible for PyMem_Free()ing it after
			   usage 

			   - if *buffer is not NULL, the data is
			   copied to *buffer; *buffer_len has to be
			   set to the size of the buffer on input;
			   buffer overflow is signalled with an error;
			   buffer has to provide enough room for the
			   encoded string plus the trailing 0-byte
			   
			   - in both cases, *buffer_len is updated to
			   the size of the buffer /excluding/ the
			   trailing 0-byte 
			   
			*/
			int *buffer_len = va_arg(*p_va, int *);
Guido van Rossum's avatar
Guido van Rossum committed
815

816 817 818 819
			format++;
			if (buffer_len == NULL)
				return converterr(
					"(buffer_len is NULL)",
820
					arg, msgbuf, bufsize);
821 822
			if (*buffer == NULL) {
				*buffer = PyMem_NEW(char, size + 1);
Guido van Rossum's avatar
Guido van Rossum committed
823
				if (*buffer == NULL) {
824 825 826
					Py_DECREF(s);
					return converterr(
						"(memory error)",
827
						arg, msgbuf, bufsize);
Guido van Rossum's avatar
Guido van Rossum committed
828 829
				}
			} else {
830
				if (size + 1 > *buffer_len) {
Guido van Rossum's avatar
Guido van Rossum committed
831
					Py_DECREF(s);
832 833
					return converterr(
						"(buffer overflow)", 
834
						arg, msgbuf, bufsize);
Guido van Rossum's avatar
Guido van Rossum committed
835 836
				}
			}
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
			memcpy(*buffer,
			       PyString_AS_STRING(s),
			       size + 1);
			*buffer_len = size;
		} else {
			/* Using a 0-terminated buffer:
				   
			   - the encoded string has to be 0-terminated
			   for this variant to work; if it is not, an
			   error raised 

			   - a new buffer of the needed size is
			   allocated and the data copied into it;
			   *buffer is updated to point to the new
			   buffer; the caller is responsible for
			   PyMem_Free()ing it after usage
Guido van Rossum's avatar
Guido van Rossum committed
853

854 855 856 857
			*/
			if ((int)strlen(PyString_AS_STRING(s)) != size)
				return converterr(
					"(encoded string without NULL bytes)",
858
					arg, msgbuf, bufsize);
859 860 861 862
			*buffer = PyMem_NEW(char, size + 1);
			if (*buffer == NULL) {
				Py_DECREF(s);
				return converterr("(memory error)",
863
						  arg, msgbuf, bufsize);
864
			}
865 866 867
			memcpy(*buffer,
			       PyString_AS_STRING(s),
			       size + 1);
868
		}
869 870 871
		Py_DECREF(s);
		break;
	}
872

873
#ifdef Py_USING_UNICODE
874 875 876 877
	case 'u': {/* raw unicode buffer (Py_UNICODE *) */
		if (*format == '#') { /* any buffer-like object */
			void **p = (void **)va_arg(*p_va, char **);
			int *q = va_arg(*p_va, int *);
878 879 880 881 882
			if (PyUnicode_Check(arg)) {
			    	*p = PyUnicode_AS_UNICODE(arg);
				*q = PyUnicode_GET_SIZE(arg);
			}
			else {
883 884 885
			char *buf;
			int count = convertbuffer(arg, p, &buf);
			if (count < 0)
886
				return converterr(buf, arg, msgbuf, bufsize);
887
			*q = count/(sizeof(Py_UNICODE)); 
888
			}
889 890 891 892 893
			format++;
		} else {
			Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
			if (PyUnicode_Check(arg))
				*p = PyUnicode_AS_UNICODE(arg);
894
			else
895
				return converterr("unicode", arg, msgbuf, bufsize);
896
		}
897 898
		break;
	}
899
#endif
900 901 902 903 904 905

	case 'S': { /* string object */
		PyObject **p = va_arg(*p_va, PyObject **);
		if (PyString_Check(arg))
			*p = arg;
		else
906
			return converterr("string", arg, msgbuf, bufsize);
907 908
		break;
	}
909
	
910
#ifdef Py_USING_UNICODE
911 912 913 914 915
	case 'U': { /* Unicode object */
		PyObject **p = va_arg(*p_va, PyObject **);
		if (PyUnicode_Check(arg))
			*p = arg;
		else
916
			return converterr("unicode", arg, msgbuf, bufsize);
917 918
		break;
	}
919
#endif
920 921 922 923 924 925 926 927
	
	case 'O': { /* object */
		PyTypeObject *type;
		PyObject **p;
		if (*format == '!') {
			type = va_arg(*p_va, PyTypeObject*);
			p = va_arg(*p_va, PyObject **);
			format++;
928
			if (PyType_IsSubtype(arg->ob_type, type))
929 930
				*p = arg;
			else
931
				return converterr(type->tp_name, arg, msgbuf, bufsize);
932

933 934 935 936 937 938
		}
		else if (*format == '?') {
			inquiry pred = va_arg(*p_va, inquiry);
			p = va_arg(*p_va, PyObject **);
			format++;
			if ((*pred)(arg)) 
939
				*p = arg;
940 941
			else
				return converterr("(unspecified)", 
942
						  arg, msgbuf, bufsize);
943 944 945 946 947 948 949 950 951
				
		}
		else if (*format == '&') {
			typedef int (*converter)(PyObject *, void *);
			converter convert = va_arg(*p_va, converter);
			void *addr = va_arg(*p_va, void *);
			format++;
			if (! (*convert)(arg, addr))
				return converterr("(unspecified)", 
952
						  arg, msgbuf, bufsize);
953
		}
954 955 956 957 958 959
		else {
			p = va_arg(*p_va, PyObject **);
			*p = arg;
		}
		break;
	}
960 961
		
		
962 963 964 965
	case 'w': { /* memory buffer, read-write access */
		void **p = va_arg(*p_va, void **);
		PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
		int count;
966
			
967 968 969
		if (pb == NULL || 
		    pb->bf_getwritebuffer == NULL ||
		    pb->bf_getsegcount == NULL)
970
			return converterr("read-write buffer", arg, msgbuf, bufsize);
971 972
		if ((*pb->bf_getsegcount)(arg, NULL) != 1)
			return converterr("single-segment read-write buffer", 
973
					  arg, msgbuf, bufsize);
974
		if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
975
			return converterr("(unspecified)", arg, msgbuf, bufsize);
976 977 978 979 980
		if (*format == '#') {
			int *q = va_arg(*p_va, int *);
			
			*q = count;
			format++;
981
		}
982 983
		break;
	}
984
		
985 986
	case 't': { /* 8-bit character buffer, read-only access */
		const char **p = va_arg(*p_va, const char **);
987
		PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
988
		int count;
989
		
990 991 992
		if (*format++ != '#')
			return converterr(
				"invalid use of 't' format character", 
993
				arg, msgbuf, bufsize);
994
		if (!PyType_HasFeature(arg->ob_type,
995 996 997
				       Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
		    pb == NULL || pb->bf_getcharbuffer == NULL ||
		    pb->bf_getsegcount == NULL)
998 999
			return converterr(
				"string or read-only character buffer",
1000
				arg, msgbuf, bufsize);
1001

1002 1003 1004
		if (pb->bf_getsegcount(arg, NULL) != 1)
			return converterr(
				"string or single-segment read-only buffer",
1005
				arg, msgbuf, bufsize);
1006 1007

		count = pb->bf_getcharbuffer(arg, 0, p);
1008
		if (count < 0)
1009
			return converterr("(unspecified)", arg, msgbuf, bufsize);
1010 1011 1012 1013
		*va_arg(*p_va, int *) = count;
		break;
	}

1014
	default:
1015
		return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1016 1017 1018 1019 1020 1021
	
	}
	
	*p_format = format;
	return NULL;
}
1022

1023 1024
static int
convertbuffer(PyObject *arg, void **p, char **errmsg)
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
{
	PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
	int count;
	if (pb == NULL ||
	    pb->bf_getreadbuffer == NULL ||
	    pb->bf_getsegcount == NULL) {
		*errmsg = "string or read-only buffer";
		return -1;
	}
	if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
		*errmsg = "string or single-segment read-only buffer";
		return -1;
	}
	if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
		*errmsg = "(unspecified)";
	}
	return count;
}
1043 1044 1045 1046

/* Support for keyword arguments donated by
   Geoff Philbrick <philbric@delphi.hks.com> */

1047
/* Return false (0) for error, else true. */
1048 1049 1050 1051 1052
int
PyArg_ParseTupleAndKeywords(PyObject *args,
			    PyObject *keywords,
			    char *format, 
			    char **kwlist, ...)
1053 1054 1055
{
	int retval;
	va_list va;
1056 1057 1058 1059 1060 1061 1062

	if ((args == NULL || !PyTuple_Check(args)) ||
	    (keywords != NULL && !PyDict_Check(keywords)) ||
	    format == NULL ||
	    kwlist == NULL)
	{
		PyErr_BadInternalCall();
1063
		return 0;
1064 1065
	}

1066 1067 1068 1069 1070 1071 1072 1073
	va_start(va, kwlist);
	retval = vgetargskeywords(args, keywords, format, kwlist, &va);	
	va_end(va);
	return retval;
}


static int
1074 1075
vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
	         char **kwlist, va_list *p_va)
1076
{
1077
	char msgbuf[512];
1078
	int levels[32];
1079 1080
	char *fname, *message;
	int min, max;
Tim Peters's avatar
Tim Peters committed
1081
	char *formatsave;
1082
	int i, len, nargs, nkeywords;
Tim Peters's avatar
Tim Peters committed
1083
	char *msg, **p;
1084

1085 1086 1087 1088 1089 1090
	assert(args != NULL && PyTuple_Check(args));
	assert(keywords == NULL || PyDict_Check(keywords));
	assert(format != NULL);
	assert(kwlist != NULL);
	assert(p_va != NULL);

1091 1092
	/* Search the format:
	   message <- error msg, if any (else NULL).
1093
	   fname <- routine name, if any (else NULL).
1094 1095
	   min <- # of required arguments, or -1 if all are required.
	   max <- most arguments (required + optional).
1096
	   Check that kwlist has a non-NULL entry for each arg.
1097 1098 1099
	   Raise error if a tuple arg spec is found.
	*/
	fname = message = NULL;
Tim Peters's avatar
Tim Peters committed
1100
	formatsave = format;
1101
	p = kwlist;
1102 1103 1104
	min = -1;
	max = 0;
	while ((i = *format++) != '\0') {
1105
		if (isalpha(i) && i != 'e') {
1106
			max++;
1107
			if (*p == NULL) {
1108 1109 1110
				PyErr_SetString(PyExc_RuntimeError,
					"more argument specifiers than "
					"keyword list entries");
1111 1112 1113 1114
				return 0;
			}
			p++;
		}
1115 1116 1117
		else if (i == '|')
			min = max;
		else if (i == ':') {
1118 1119
			fname = format;
			break;
1120 1121
		}
		else if (i == ';') {
1122 1123
			message = format;
			break;
1124 1125
		}
		else if (i == '(') {
1126 1127 1128
			PyErr_SetString(PyExc_RuntimeError,
				"tuple found in format when using keyword "
				"arguments");
1129 1130
			return 0;
		}
1131 1132 1133
	}
	format = formatsave;
	if (*p != NULL) {
1134 1135 1136
		PyErr_SetString(PyExc_RuntimeError,
			"more keyword list entries than "
			"argument specifiers");
1137 1138
		return 0;
	}
1139 1140
	if (min < 0) {
		/* All arguments are required. */
1141
		min = max;
1142 1143
	}

Tim Peters's avatar
Tim Peters committed
1144
	nargs = PyTuple_GET_SIZE(args);
Tim Peters's avatar
Tim Peters committed
1145
	nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
1146

1147 1148 1149
	/* make sure there are no duplicate values for an argument;
	   its not clear when to use the term "keyword argument vs. 
	   keyword parameter in messages */
Tim Peters's avatar
Tim Peters committed
1150
	if (nkeywords > 0) {
Tim Peters's avatar
Tim Peters committed
1151
		for (i = 0; i < nargs; i++) {
1152 1153 1154
			char *thiskw = kwlist[i];
			if (thiskw == NULL)
				break;
1155
			if (PyDict_GetItemString(keywords, thiskw)) {
Tim Peters's avatar
Tim Peters committed
1156 1157 1158
				PyErr_Format(PyExc_TypeError,
					"keyword parameter '%s' was given "
					"by position and by name",
1159
					thiskw);
1160 1161
				return 0;
			}
1162 1163
			else if (PyErr_Occurred())
				return 0;
1164 1165
		}
	}
1166

1167
	/* required arguments missing from args can be supplied by keyword 
1168 1169 1170
	   arguments; set len to the number of posiitional arguments, and,
	   if that's less than the minimum required, add in the number of
	   required arguments that are supplied by keywords */
Tim Peters's avatar
Tim Peters committed
1171
	len = nargs;
1172
	if (nkeywords > 0 && nargs < min) {
Tim Peters's avatar
Tim Peters committed
1173
		for (i = nargs; i < min; i++) {
1174
			if (PyDict_GetItemString(keywords, kwlist[i]))
1175
				len++;
1176 1177
			else if (PyErr_Occurred())
				return 0;
1178 1179
		}
	}
1180

1181 1182 1183 1184 1185 1186
	/* make sure we got an acceptable number of arguments; the message
	   is a little confusing with keywords since keyword arguments
	   which are supplied, but don't match the required arguments
	   are not included in the "%d given" part of the message */
	if (len < min || max < len) {
		if (message == NULL) {
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
			PyOS_snprintf(msgbuf, sizeof(msgbuf),
				      "%.200s%s takes %s %d argument%s "
				      "(%d given)",
				      fname==NULL ? "function" : fname,
				      fname==NULL ? "" : "()",
				      min==max ? "exactly"
			              : len < min ? "at least" : "at most",
				      len < min ? min : max,
				      (len < min ? min : max) == 1 ? "" : "s",
				      len);
1197 1198 1199 1200 1201
			message = msgbuf;
		}
		PyErr_SetString(PyExc_TypeError, message);
		return 0;
	}
Tim Peters's avatar
Tim Peters committed
1202 1203

	/* convert the positional arguments */
Tim Peters's avatar
Tim Peters committed
1204
	for (i = 0; i < nargs; i++) {
1205 1206
		if (*format == '|')
			format++;
1207
		msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
1208
				 levels, msgbuf, sizeof(msgbuf));
1209 1210 1211 1212 1213 1214
		if (msg) {
			seterror(i+1, msg, levels, fname, message);
			return 0;
		}
	}

Tim Peters's avatar
Tim Peters committed
1215
	/* handle no keyword parameters in call */	
Tim Peters's avatar
Tim Peters committed
1216
	if (nkeywords == 0)
Tim Peters's avatar
Tim Peters committed
1217
		return 1; 
Tim Peters's avatar
Tim Peters committed
1218

1219 1220
	/* convert the keyword arguments; this uses the format 
	   string where it was left after processing args */
1221
	for (i = nargs; i < max; i++) {
1222
		PyObject *item;
1223 1224
		if (*format == '|')
			format++;
1225
		item = PyDict_GetItemString(keywords, kwlist[i]);
Guido van Rossum's avatar
Guido van Rossum committed
1226
		if (item != NULL) {
1227
			Py_INCREF(item);
1228 1229
			msg = convertitem(item, &format, p_va, levels, msgbuf,
					  sizeof(msgbuf));
1230
			Py_DECREF(item);
1231 1232 1233 1234
			if (msg) {
				seterror(i+1, msg, levels, fname, message);
				return 0;
			}
Tim Peters's avatar
Tim Peters committed
1235 1236 1237
			--nkeywords;
			if (nkeywords == 0)
				break;
1238
		}
1239 1240
		else if (PyErr_Occurred())
			return 0;
1241 1242 1243 1244 1245 1246 1247 1248
		else {
			msg = skipitem(&format, p_va);
			if (msg) {
				seterror(i+1, msg, levels, fname, message);
				return 0;
			}
		}
	}
Tim Peters's avatar
Tim Peters committed
1249

1250
	/* make sure there are no extraneous keyword arguments */
Tim Peters's avatar
Tim Peters committed
1251 1252 1253
	if (nkeywords > 0) {
		PyObject *key, *value;
		int pos = 0;
1254
		while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Peters's avatar
Tim Peters committed
1255
			int match = 0;
1256 1257 1258 1259 1260 1261 1262
			char *ks;
			if (!PyString_Check(key)) {
				PyErr_SetString(PyExc_TypeError, 
					        "keywords must be strings");
				return 0;
			}
			ks = PyString_AsString(key);
1263
			for (i = 0; i < max; i++) {
1264 1265 1266 1267 1268 1269
				if (!strcmp(ks, kwlist[i])) {
					match = 1;
					break;
				}
			}
			if (!match) {
Tim Peters's avatar
Tim Peters committed
1270 1271 1272 1273
				PyErr_Format(PyExc_TypeError,
					     "'%s' is an invalid keyword "
					     "argument for this function",
					     ks);
1274 1275 1276 1277
				return 0;
			}
		}
	}
Tim Peters's avatar
Tim Peters committed
1278

1279 1280 1281 1282 1283
	return 1;
}


static char *
1284
skipitem(char **p_format, va_list *p_va)
1285 1286 1287 1288 1289 1290 1291
{
	char *format = *p_format;
	char c = *format++;
	
	switch (c) {
	
	case 'b': /* byte -- very short int */
1292
	case 'B': /* byte as bitfield */
1293
		{
Guido van Rossum's avatar
Guido van Rossum committed
1294
			(void) va_arg(*p_va, char *);
1295 1296 1297 1298 1299
			break;
		}
	
	case 'h': /* short int */
		{
Guido van Rossum's avatar
Guido van Rossum committed
1300
			(void) va_arg(*p_va, short *);
1301 1302 1303
			break;
		}
	
1304
	case 'H': /* short int as bitfield */
1305 1306 1307 1308 1309
		{
			(void) va_arg(*p_va, unsigned short *);
			break;
		}
	
1310 1311
	case 'i': /* int */
		{
Guido van Rossum's avatar
Guido van Rossum committed
1312
			(void) va_arg(*p_va, int *);
1313 1314 1315 1316 1317
			break;
		}
	
	case 'l': /* long int */
		{
Guido van Rossum's avatar
Guido van Rossum committed
1318
			(void) va_arg(*p_va, long *);
1319 1320 1321
			break;
		}
	
1322
#ifdef HAVE_LONG_LONG
1323
	case 'L': /* LONG_LONG int */
1324
		{
1325
			(void) va_arg(*p_va, LONG_LONG *);
1326 1327 1328 1329
			break;
		}
#endif
	
1330 1331
	case 'f': /* float */
		{
Guido van Rossum's avatar
Guido van Rossum committed
1332
			(void) va_arg(*p_va, float *);
1333 1334 1335 1336 1337
			break;
		}
	
	case 'd': /* double */
		{
Guido van Rossum's avatar
Guido van Rossum committed
1338
			(void) va_arg(*p_va, double *);
1339 1340 1341 1342 1343 1344
			break;
		}
	
#ifndef WITHOUT_COMPLEX
	case 'D': /* complex double */
		{
Guido van Rossum's avatar
Guido van Rossum committed
1345
			(void) va_arg(*p_va, Py_complex *);
1346 1347 1348 1349 1350 1351
			break;
		}
#endif /* WITHOUT_COMPLEX */
	
	case 'c': /* char */
		{
Guido van Rossum's avatar
Guido van Rossum committed
1352
			(void) va_arg(*p_va, char *);
1353 1354 1355 1356 1357
			break;
		}
	
	case 's': /* string */
		{
Guido van Rossum's avatar
Guido van Rossum committed
1358
			(void) va_arg(*p_va, char **);
1359
			if (*format == '#') {
Guido van Rossum's avatar
Guido van Rossum committed
1360
				(void) va_arg(*p_va, int *);
1361 1362 1363 1364 1365 1366 1367
				format++;
			}
			break;
		}
	
	case 'z': /* string */
		{
Guido van Rossum's avatar
Guido van Rossum committed
1368
			(void) va_arg(*p_va, char **);
1369
			if (*format == '#') {
Guido van Rossum's avatar
Guido van Rossum committed
1370
				(void) va_arg(*p_va, int *);
1371 1372 1373 1374 1375 1376 1377
				format++;
			}
			break;
		}
	
	case 'S': /* string object */
		{
1378
			(void) va_arg(*p_va, PyObject **);
1379 1380 1381 1382 1383 1384 1385
			break;
		}
	
	case 'O': /* object */
		{
			if (*format == '!') {
				format++;
1386 1387
				(void) va_arg(*p_va, PyTypeObject*);
				(void) va_arg(*p_va, PyObject **);
1388 1389 1390 1391 1392 1393 1394
			}
#if 0
/* I don't know what this is for */
			else if (*format == '?') {
				inquiry pred = va_arg(*p_va, inquiry);
				format++;
				if ((*pred)(arg)) {
1395
					(void) va_arg(*p_va, PyObject **);
1396 1397 1398 1399
				}
			}
#endif
			else if (*format == '&') {
1400
				typedef int (*converter)(PyObject *, void *);
Guido van Rossum's avatar
Guido van Rossum committed
1401 1402
				(void) va_arg(*p_va, converter);
				(void) va_arg(*p_va, void *);
1403 1404 1405
				format++;
			}
			else {
1406
				(void) va_arg(*p_va, PyObject **);
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
			}
			break;
		}
	
	default:
		return "impossible<bad format char>";
	
	}
	
	*p_format = format;
	return NULL;
}
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478


int
PyArg_UnpackTuple(PyObject *args, char *name, int min, int max, ...)
{
	int i, l;
	PyObject **o;
	va_list vargs;

#ifdef HAVE_STDARG_PROTOTYPES
	va_start(vargs, max);
#else
	va_start(vargs);
#endif

	assert(min >= 0);
	assert(min <= max);
	if (!PyTuple_Check(args)) {
		PyErr_SetString(PyExc_SystemError,
		    "PyArg_UnpackTuple() argument list is not a tuple");
		return 0;
	}	
	l = PyTuple_GET_SIZE(args);
	if (l < min) {
		if (name != NULL)
			PyErr_Format(
			    PyExc_TypeError,
			    "%s expected %s%d arguments, got %d", 
			    name, (min == max ? "" : "at least "), min, l);
		else
			PyErr_Format(
			    PyExc_TypeError,
			    "unpacked tuple should have %s%d elements,"
			    " but has %d", 
			    (min == max ? "" : "at least "), min, l);
		va_end(vargs);
		return 0;
	}
	if (l > max) {
		if (name != NULL)
			PyErr_Format(
			    PyExc_TypeError,
			    "%s expected %s%d arguments, got %d", 
			    name, (min == max ? "" : "at most "), max, l);
		else
			PyErr_Format(
			    PyExc_TypeError,
			    "unpacked tuple should have %s%d elements,"
			    " but has %d", 
			    (min == max ? "" : "at most "), max, l);
		va_end(vargs);
		return 0;
	}
	for (i = 0; i < l; i++) {
		o = va_arg(vargs, PyObject **);
		*o = PyTuple_GET_ITEM(args, i);
	}
	va_end(vargs);
	return 1;
}