| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "Python.h" |
| 5 |
|
| 6 |
#include <ctype.h> |
| 7 |
|
| 8 |
|
| 9 |
#ifdef __cplusplus |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
int PyArg_Parse(PyObject *, const char *, ...); |
| 13 |
int PyArg_ParseTuple(PyObject *, const char *, ...); |
| 14 |
int PyArg_VaParse(PyObject *, const char *, va_list); |
| 15 |
|
| 16 |
int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, |
| 17 |
const char *, char **, ...); |
| 18 |
int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *, |
| 19 |
const char *, char **, va_list); |
| 20 |
|
| 21 |
#ifdef HAVE_DECLSPEC_DLL |
| 22 |
|
| 23 |
PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, char *, ...); |
| 24 |
PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, char *, ...); |
| 25 |
PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
| 26 |
const char *, char **, ...); |
| 27 |
PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...); |
| 28 |
PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, char *, va_list); |
| 29 |
PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
| 30 |
const char *, char **, va_list); |
| 31 |
#endif |
| 32 |
|
| 33 |
#define FLAG_COMPAT 1 |
| 34 |
#define FLAG_SIZE_T 2 |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static int vgetargs1(PyObject *, const char *, va_list *, int); |
| 39 |
static void seterror(int, const char *, int *, const char *, const char *); |
| 40 |
static char *convertitem(PyObject *, const char **, va_list *, int, int *, |
| 41 |
char *, size_t, PyObject **); |
| 42 |
static char *converttuple(PyObject *, const char **, va_list *, int, |
| 43 |
int *, char *, size_t, int, PyObject **); |
| 44 |
static char *convertsimple(PyObject *, const char **, va_list *, int, char *, |
| 45 |
size_t, PyObject **); |
| 46 |
static Py_ssize_t convertbuffer(PyObject *, void **p, char **); |
| 47 |
static int getbuffer(PyObject *, Py_buffer *, char**); |
| 48 |
|
| 49 |
static int vgetargskeywords(PyObject *, PyObject *, |
| 50 |
const char *, char **, va_list *, int); |
| 51 |
static char *skipitem(const char **, va_list *, int); |
| 52 |
|
| 53 |
int |
| 54 |
PyArg_Parse(PyObject *args, const char *format, ...) |
| 55 |
{ |
| 56 |
int retval; |
| 57 |
va_list va; |
| 58 |
|
| 59 |
va_start(va, format); |
| 60 |
retval = vgetargs1(args, format, &va, FLAG_COMPAT); |
| 61 |
va_end(va); |
| 62 |
return retval; |
| 63 |
} |
| 64 |
|
| 65 |
int |
| 66 |
_PyArg_Parse_SizeT(PyObject *args, char *format, ...) |
| 67 |
{ |
| 68 |
int retval; |
| 69 |
va_list va; |
| 70 |
|
| 71 |
va_start(va, format); |
| 72 |
retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T); |
| 73 |
va_end(va); |
| 74 |
return retval; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
int |
| 79 |
PyArg_ParseTuple(PyObject *args, const char *format, ...) |
| 80 |
{ |
| 81 |
int retval; |
| 82 |
va_list va; |
| 83 |
|
| 84 |
va_start(va, format); |
| 85 |
retval = vgetargs1(args, format, &va, 0); |
| 86 |
va_end(va); |
| 87 |
return retval; |
| 88 |
} |
| 89 |
|
| 90 |
int |
| 91 |
_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...) |
| 92 |
{ |
| 93 |
int retval; |
| 94 |
va_list va; |
| 95 |
|
| 96 |
va_start(va, format); |
| 97 |
retval = vgetargs1(args, format, &va, FLAG_SIZE_T); |
| 98 |
va_end(va); |
| 99 |
return retval; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
int |
| 104 |
PyArg_VaParse(PyObject *args, const char *format, va_list va) |
| 105 |
{ |
| 106 |
va_list lva; |
| 107 |
|
| 108 |
#ifdef VA_LIST_IS_ARRAY |
| 109 |
memcpy(lva, va, sizeof(va_list)); |
| 110 |
#else |
| 111 |
#ifdef __va_copy |
| 112 |
__va_copy(lva, va); |
| 113 |
#else |
| 114 |
lva = va; |
| 115 |
#endif |
| 116 |
#endif |
| 117 |
|
| 118 |
return vgetargs1(args, format, &lva, 0); |
| 119 |
} |
| 120 |
|
| 121 |
int |
| 122 |
_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va) |
| 123 |
{ |
| 124 |
va_list lva; |
| 125 |
|
| 126 |
#ifdef VA_LIST_IS_ARRAY |
| 127 |
memcpy(lva, va, sizeof(va_list)); |
| 128 |
#else |
| 129 |
#ifdef __va_copy |
| 130 |
__va_copy(lva, va); |
| 131 |
#else |
| 132 |
lva = va; |
| 133 |
#endif |
| 134 |
#endif |
| 135 |
|
| 136 |
return vgetargs1(args, format, &lva, FLAG_SIZE_T); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
#define GETARGS_CAPSULE_NAME_CLEANUP_PTR "getargs.cleanup_ptr" |
| 143 |
#define GETARGS_CAPSULE_NAME_CLEANUP_BUFFER "getargs.cleanup_buffer" |
| 144 |
|
| 145 |
static void |
| 146 |
cleanup_ptr(PyObject *self) |
| 147 |
{ |
| 148 |
void *ptr = PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_PTR); |
| 149 |
if (ptr) { |
| 150 |
PyMem_FREE(ptr); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
cleanup_buffer(PyObject *self) |
| 156 |
{ |
| 157 |
Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_BUFFER); |
| 158 |
if (ptr) { |
| 159 |
PyBuffer_Release(ptr); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
static int |
| 164 |
addcleanup(void *ptr, PyObject **freelist, PyCapsule_Destructor destr) |
| 165 |
{ |
| 166 |
PyObject *cobj; |
| 167 |
const char *name; |
| 168 |
|
| 169 |
if (!*freelist) { |
| 170 |
*freelist = PyList_New(0); |
| 171 |
if (!*freelist) { |
| 172 |
destr(ptr); |
| 173 |
return -1; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if (destr == cleanup_ptr) { |
| 178 |
name = GETARGS_CAPSULE_NAME_CLEANUP_PTR; |
| 179 |
} else if (destr == cleanup_buffer) { |
| 180 |
name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER; |
| 181 |
} else { |
| 182 |
return -1; |
| 183 |
} |
| 184 |
cobj = PyCapsule_New(ptr, name, destr); |
| 185 |
if (!cobj) { |
| 186 |
destr(ptr); |
| 187 |
return -1; |
| 188 |
} |
| 189 |
if (PyList_Append(*freelist, cobj)) { |
| 190 |
Py_DECREF(cobj); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
Py_DECREF(cobj); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
static int |
| 198 |
cleanreturn(int retval, PyObject *freelist) |
| 199 |
{ |
| 200 |
if (freelist && retval != 0) { |
| 201 |
|
| 202 |
|
| 203 |
Py_ssize_t len = PyList_GET_SIZE(freelist), i; |
| 204 |
for (i = 0; i < len; i++) |
| 205 |
PyCapsule_SetDestructor(PyList_GET_ITEM(freelist, i), NULL); |
| 206 |
} |
| 207 |
Py_XDECREF(freelist); |
| 208 |
return retval; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
static int |
| 213 |
vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags) |
| 214 |
{ |
| 215 |
char msgbuf[256]; |
| 216 |
int levels[32]; |
| 217 |
const char *fname = NULL; |
| 218 |
const char *message = NULL; |
| 219 |
int min = -1; |
| 220 |
int max = 0; |
| 221 |
int level = 0; |
| 222 |
int endfmt = 0; |
| 223 |
const char *formatsave = format; |
| 224 |
Py_ssize_t i, len; |
| 225 |
char *msg; |
| 226 |
PyObject *freelist = NULL; |
| 227 |
int compat = flags & FLAG_COMPAT; |
| 228 |
|
| 229 |
assert(compat || (args != (PyObject*)NULL)); |
| 230 |
flags = flags & ~FLAG_COMPAT; |
| 231 |
|
| 232 |
while (endfmt == 0) { |
| 233 |
int c = *format++; |
| 234 |
switch (c) { |
| 235 |
case '(': |
| 236 |
if (level == 0) |
| 237 |
max++; |
| 238 |
level++; |
| 239 |
if (level >= 30) |
| 240 |
Py_FatalError("too many tuple nesting levels " |
| 241 |
"in argument format string"); |
| 242 |
break; |
| 243 |
case ')': |
| 244 |
if (level == 0) |
| 245 |
Py_FatalError("excess ')' in getargs format"); |
| 246 |
else |
| 247 |
level--; |
| 248 |
break; |
| 249 |
case '\0': |
| 250 |
endfmt = 1; |
| 251 |
break; |
| 252 |
case ':': |
| 253 |
fname = format; |
| 254 |
endfmt = 1; |
| 255 |
break; |
| 256 |
case ';': |
| 257 |
message = format; |
| 258 |
endfmt = 1; |
| 259 |
break; |
| 260 |
default: |
| 261 |
if (level == 0) { |
| 262 |
if (c == 'O') |
| 263 |
max++; |
| 264 |
else if (isalpha(Py_CHARMASK(c))) { |
| 265 |
if (c != 'e') |
| 266 |
max++; |
| 267 |
} else if (c == '|') |
| 268 |
min = max; |
| 269 |
} |
| 270 |
break; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if (level != 0) |
| 275 |
Py_FatalError( "missing ')' in getargs format"); |
| 276 |
|
| 277 |
if (min < 0) |
| 278 |
min = max; |
| 279 |
|
| 280 |
format = formatsave; |
| 281 |
|
| 282 |
if (compat) { |
| 283 |
if (max == 0) { |
| 284 |
if (args == NULL) |
| 285 |
return 1; |
| 286 |
PyOS_snprintf(msgbuf, sizeof(msgbuf), |
| 287 |
"%.200s%s takes no arguments", |
| 288 |
fname==NULL ? "function" : fname, |
| 289 |
fname==NULL ? "" : "()"); |
| 290 |
PyErr_SetString(PyExc_TypeError, msgbuf); |
| 291 |
return 0; |
| 292 |
} |
| 293 |
else if (min == 1 && max == 1) { |
| 294 |
if (args == NULL) { |
| 295 |
PyOS_snprintf(msgbuf, sizeof(msgbuf), |
| 296 |
"%.200s%s takes at least one argument", |
| 297 |
fname==NULL ? "function" : fname, |
| 298 |
fname==NULL ? "" : "()"); |
| 299 |
PyErr_SetString(PyExc_TypeError, msgbuf); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
msg = convertitem(args, &format, p_va, flags, levels, |
| 303 |
msgbuf, sizeof(msgbuf), &freelist); |
| 304 |
if (msg == NULL) |
| 305 |
return cleanreturn(1, freelist); |
| 306 |
seterror(levels[0], msg, levels+1, fname, message); |
| 307 |
return cleanreturn(0, freelist); |
| 308 |
} |
| 309 |
else { |
| 310 |
PyErr_SetString(PyExc_SystemError, |
| 311 |
"old style getargs format uses new features"); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
if (!PyTuple_Check(args)) { |
| 317 |
PyErr_SetString(PyExc_SystemError, |
| 318 |
"new style getargs format but argument is not a tuple"); |
| 319 |
return 0; |
| 320 |
} |
| 321 |
|
| 322 |
len = PyTuple_GET_SIZE(args); |
| 323 |
|
| 324 |
if (len < min || max < len) { |
| 325 |
if (message == NULL) { |
| 326 |
PyOS_snprintf(msgbuf, sizeof(msgbuf), |
| 327 |
"%.150s%s takes %s %d argument%s " |
| 328 |
"(%ld given)", |
| 329 |
fname==NULL ? "function" : fname, |
| 330 |
fname==NULL ? "" : "()", |
| 331 |
min==max ? "exactly" |
| 332 |
: len < min ? "at least" : "at most", |
| 333 |
len < min ? min : max, |
| 334 |
(len < min ? min : max) == 1 ? "" : "s", |
| 335 |
Py_SAFE_DOWNCAST(len, Py_ssize_t, long)); |
| 336 |
message = msgbuf; |
| 337 |
} |
| 338 |
PyErr_SetString(PyExc_TypeError, message); |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
for (i = 0; i < len; i++) { |
| 343 |
if (*format == '|') |
| 344 |
format++; |
| 345 |
msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va, |
| 346 |
flags, levels, msgbuf, |
| 347 |
sizeof(msgbuf), &freelist); |
| 348 |
if (msg) { |
| 349 |
seterror(i+1, msg, levels, fname, message); |
| 350 |
return cleanreturn(0, freelist); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) && |
| 355 |
*format != '(' && |
| 356 |
*format != '|' && *format != ':' && *format != ';') { |
| 357 |
PyErr_Format(PyExc_SystemError, |
| 358 |
"bad format string: %.200s", formatsave); |
| 359 |
return cleanreturn(0, freelist); |
| 360 |
} |
| 361 |
|
| 362 |
return cleanreturn(1, freelist); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
static void |
| 368 |
seterror(int iarg, const char *msg, int *levels, const char *fname, |
| 369 |
const char *message) |
| 370 |
{ |
| 371 |
char buf[512]; |
| 372 |
int i; |
| 373 |
char *p = buf; |
| 374 |
|
| 375 |
if (PyErr_Occurred()) |
| 376 |
return; |
| 377 |
else if (message == NULL) { |
| 378 |
if (fname != NULL) { |
| 379 |
PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname); |
| 380 |
p += strlen(p); |
| 381 |
} |
| 382 |
if (iarg != 0) { |
| 383 |
PyOS_snprintf(p, sizeof(buf) - (p - buf), |
| 384 |
"argument %d", iarg); |
| 385 |
i = 0; |
| 386 |
p += strlen(p); |
| 387 |
while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) { |
| 388 |
PyOS_snprintf(p, sizeof(buf) - (p - buf), |
| 389 |
", item %d", levels[i]-1); |
| 390 |
p += strlen(p); |
| 391 |
i++; |
| 392 |
} |
| 393 |
} |
| 394 |
else { |
| 395 |
PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument"); |
| 396 |
p += strlen(p); |
| 397 |
} |
| 398 |
PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg); |
| 399 |
message = buf; |
| 400 |
} |
| 401 |
PyErr_SetString(PyExc_TypeError, message); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
static char * |
| 424 |
converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
| 425 |
int *levels, char *msgbuf, size_t bufsize, int toplevel, |
| 426 |
PyObject **freelist) |
| 427 |
{ |
| 428 |
int level = 0; |
| 429 |
int n = 0; |
| 430 |
const char *format = *p_format; |
| 431 |
int i; |
| 432 |
|
| 433 |
for (;;) { |
| 434 |
int c = *format++; |
| 435 |
if (c == '(') { |
| 436 |
if (level == 0) |
| 437 |
n++; |
| 438 |
level++; |
| 439 |
} |
| 440 |
else if (c == ')') { |
| 441 |
if (level == 0) |
| 442 |
break; |
| 443 |
level--; |
| 444 |
} |
| 445 |
else if (c == ':' || c == ';' || c == '\0') |
| 446 |
break; |
| 447 |
else if (level == 0 && isalpha(Py_CHARMASK(c))) |
| 448 |
n++; |
| 449 |
} |
| 450 |
|
| 451 |
if (!PySequence_Check(arg) || PyString_Check(arg)) { |
| 452 |
levels[0] = 0; |
| 453 |
PyOS_snprintf(msgbuf, bufsize, |
| 454 |
toplevel ? "expected %d arguments, not %.50s" : |
| 455 |
"must be %d-item sequence, not %.50s", |
| 456 |
n, |
| 457 |
arg == Py_None ? "None" : arg->ob_type->tp_name); |
| 458 |
return msgbuf; |
| 459 |
} |
| 460 |
|
| 461 |
if ((i = PySequence_Size(arg)) != n) { |
| 462 |
levels[0] = 0; |
| 463 |
PyOS_snprintf(msgbuf, bufsize, |
| 464 |
toplevel ? "expected %d arguments, not %d" : |
| 465 |
"must be sequence of length %d, not %d", |
| 466 |
n, i); |
| 467 |
return msgbuf; |
| 468 |
} |
| 469 |
|
| 470 |
format = *p_format; |
| 471 |
for (i = 0; i < n; i++) { |
| 472 |
char *msg; |
| 473 |
PyObject *item; |
| 474 |
item = PySequence_GetItem(arg, i); |
| 475 |
if (item == NULL) { |
| 476 |
PyErr_Clear(); |
| 477 |
levels[0] = i+1; |
| 478 |
levels[1] = 0; |
| 479 |
strncpy(msgbuf, "is not retrievable", bufsize); |
| 480 |
return msgbuf; |
| 481 |
} |
| 482 |
msg = convertitem(item, &format, p_va, flags, levels+1, |
| 483 |
msgbuf, bufsize, freelist); |
| 484 |
|
| 485 |
Py_XDECREF(item); |
| 486 |
if (msg != NULL) { |
| 487 |
levels[0] = i+1; |
| 488 |
return msg; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
*p_format = format; |
| 493 |
return NULL; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
static char * |
| 500 |
convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
| 501 |
int *levels, char *msgbuf, size_t bufsize, PyObject **freelist) |
| 502 |
{ |
| 503 |
char *msg; |
| 504 |
const char *format = *p_format; |
| 505 |
|
| 506 |
if (*format == '(' ) { |
| 507 |
format++; |
| 508 |
msg = converttuple(arg, &format, p_va, flags, levels, msgbuf, |
| 509 |
bufsize, 0, freelist); |
| 510 |
if (msg == NULL) |
| 511 |
format++; |
| 512 |
} |
| 513 |
else { |
| 514 |
msg = convertsimple(arg, &format, p_va, flags, |
| 515 |
msgbuf, bufsize, freelist); |
| 516 |
if (msg != NULL) |
| 517 |
levels[0] = 0; |
| 518 |
} |
| 519 |
if (msg == NULL) |
| 520 |
*p_format = format; |
| 521 |
return msg; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
#define UNICODE_DEFAULT_ENCODING(arg) \ |
| 527 |
_PyUnicode_AsDefaultEncodedString(arg, NULL) |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
static char * |
| 532 |
converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize) |
| 533 |
{ |
| 534 |
assert(expected != NULL); |
| 535 |
assert(arg != NULL); |
| 536 |
if (expected[0] == '(') { |
| 537 |
PyOS_snprintf(msgbuf, bufsize, |
| 538 |
"%.100s", expected); |
| 539 |
strncpy(msgbuf, expected, bufsize); |
| 540 |
msgbuf[bufsize-1] = '\0'; |
| 541 |
} |
| 542 |
else { |
| 543 |
PyOS_snprintf(msgbuf, bufsize, |
| 544 |
"must be %.50s, not %.50s", expected, |
| 545 |
arg == Py_None ? "None" : arg->ob_type->tp_name); |
| 546 |
} |
| 547 |
return msgbuf; |
| 548 |
} |
| 549 |
|
| 550 |
#define CONV_UNICODE "(unicode conversion error)" |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
static int |
| 555 |
float_argument_warning(PyObject *arg) |
| 556 |
{ |
| 557 |
if (PyFloat_Check(arg) && |
| 558 |
PyErr_Warn(PyExc_DeprecationWarning, |
| 559 |
"integer argument expected, got float" )) |
| 560 |
return 1; |
| 561 |
else |
| 562 |
return 0; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
static int |
| 568 |
float_argument_error(PyObject *arg) |
| 569 |
{ |
| 570 |
if (PyFloat_Check(arg)) { |
| 571 |
PyErr_SetString(PyExc_TypeError, |
| 572 |
"integer argument expected, got float"); |
| 573 |
return 1; |
| 574 |
} |
| 575 |
else |
| 576 |
return 0; |
| 577 |
} |
| 578 |
|
| 579 |
#ifdef Py_USING_UNICODE |
| 580 |
static size_t |
| 581 |
_ustrlen(Py_UNICODE *u) |
| 582 |
{ |
| 583 |
size_t i = 0; |
| 584 |
Py_UNICODE *v = u; |
| 585 |
while (*v != 0) { i++; v++; } |
| 586 |
return i; |
| 587 |
} |
| 588 |
#endif |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
static char * |
| 600 |
convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
| 601 |
char *msgbuf, size_t bufsize, PyObject **freelist) |
| 602 |
{ |
| 603 |
|
| 604 |
#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\ |
| 605 |
if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \ |
| 606 |
else q=va_arg(*p_va, int*); |
| 607 |
#define STORE_SIZE(s) \ |
| 608 |
if (flags & FLAG_SIZE_T) \ |
| 609 |
*q2=s; \ |
| 610 |
else { \ |
| 611 |
if (INT_MAX < s) { \ |
| 612 |
PyErr_SetString(PyExc_OverflowError, \ |
| 613 |
"size does not fit in an int"); \ |
| 614 |
return converterr("", arg, msgbuf, bufsize); \ |
| 615 |
} \ |
| 616 |
*q=s; \ |
| 617 |
} |
| 618 |
#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q) |
| 619 |
|
| 620 |
const char *format = *p_format; |
| 621 |
char c = *format++; |
| 622 |
#ifdef Py_USING_UNICODE |
| 623 |
PyObject *uarg; |
| 624 |
#endif |
| 625 |
|
| 626 |
switch (c) { |
| 627 |
|
| 628 |
case 'b': { |
| 629 |
char *p = va_arg(*p_va, char *); |
| 630 |
long ival; |
| 631 |
if (float_argument_error(arg)) |
| 632 |
return converterr("integer<b>", arg, msgbuf, bufsize); |
| 633 |
ival = PyInt_AsLong(arg); |
| 634 |
if (ival == -1 && PyErr_Occurred()) |
| 635 |
return converterr("integer<b>", arg, msgbuf, bufsize); |
| 636 |
else if (ival < 0) { |
| 637 |
PyErr_SetString(PyExc_OverflowError, |
| 638 |
"unsigned byte integer is less than minimum"); |
| 639 |
return converterr("integer<b>", arg, msgbuf, bufsize); |
| 640 |
} |
| 641 |
else if (ival > UCHAR_MAX) { |
| 642 |
PyErr_SetString(PyExc_OverflowError, |
| 643 |
"unsigned byte integer is greater than maximum"); |
| 644 |
return converterr("integer<b>", arg, msgbuf, bufsize); |
| 645 |
} |
| 646 |
else |
| 647 |
*p = (unsigned char) ival; |
| 648 |
break; |
| 649 |
} |
| 650 |
|
| 651 |
case 'B': { |
| 652 |
|
| 653 |
char *p = va_arg(*p_va, char *); |
| 654 |
long ival; |
| 655 |
if (float_argument_error(arg)) |
| 656 |
return converterr("integer<B>", arg, msgbuf, bufsize); |
| 657 |
ival = PyInt_AsUnsignedLongMask(arg); |
| 658 |
if (ival == -1 && PyErr_Occurred()) |
| 659 |
return converterr("integer<B>", arg, msgbuf, bufsize); |
| 660 |
else |
| 661 |
*p = (unsigned char) ival; |
| 662 |
break; |
| 663 |
} |
| 664 |
|
| 665 |
case 'h': { |
| 666 |
short *p = va_arg(*p_va, short *); |
| 667 |
long ival; |
| 668 |
if (float_argument_error(arg)) |
| 669 |
return converterr("integer<h>", arg, msgbuf, bufsize); |
| 670 |
ival = PyInt_AsLong(arg); |
| 671 |
if (ival == -1 && PyErr_Occurred()) |
| 672 |
return converterr("integer<h>", arg, msgbuf, bufsize); |
| 673 |
else if (ival < SHRT_MIN) { |
| 674 |
PyErr_SetString(PyExc_OverflowError, |
| 675 |
"signed short integer is less than minimum"); |
| 676 |
return converterr("integer<h>", arg, msgbuf, bufsize); |
| 677 |
} |
| 678 |
else if (ival > SHRT_MAX) { |
| 679 |
PyErr_SetString(PyExc_OverflowError, |
| 680 |
"signed short integer is greater than maximum"); |
| 681 |
return converterr("integer<h>", arg, msgbuf, bufsize); |
| 682 |
} |
| 683 |
else |
| 684 |
*p = (short) ival; |
| 685 |
break; |
| 686 |
} |
| 687 |
|
| 688 |
case 'H': { |
| 689 |
|
| 690 |
unsigned short *p = va_arg(*p_va, unsigned short *); |
| 691 |
long ival; |
| 692 |
if (float_argument_error(arg)) |
| 693 |
return converterr("integer<H>", arg, msgbuf, bufsize); |
| 694 |
ival = PyInt_AsUnsignedLongMask(arg); |
| 695 |
if (ival == -1 && PyErr_Occurred()) |
| 696 |
return converterr("integer<H>", arg, msgbuf, bufsize); |
| 697 |
else |
| 698 |
*p = (unsigned short) ival; |
| 699 |
break; |
| 700 |
} |
| 701 |
|
| 702 |
case 'i': { |
| 703 |
int *p = va_arg(*p_va, int *); |
| 704 |
long ival; |
| 705 |
if (float_argument_error(arg)) |
| 706 |
return converterr("integer<i>", arg, msgbuf, bufsize); |
| 707 |
ival = PyInt_AsLong(arg); |
| 708 |
if (ival == -1 && PyErr_Occurred()) |
| 709 |
return converterr("integer<i>", arg, msgbuf, bufsize); |
| 710 |
else if (ival > INT_MAX) { |
| 711 |
PyErr_SetString(PyExc_OverflowError, |
| 712 |
"signed integer is greater than maximum"); |
| 713 |
return converterr("integer<i>", arg, msgbuf, bufsize); |
| 714 |
} |
| 715 |
else if (ival < INT_MIN) { |
| 716 |
PyErr_SetString(PyExc_OverflowError, |
| 717 |
"signed integer is less than minimum"); |
| 718 |
return converterr("integer<i>", arg, msgbuf, bufsize); |
| 719 |
} |
| 720 |
else |
| 721 |
*p = ival; |
| 722 |
break; |
| 723 |
} |
| 724 |
|
| 725 |
case 'I': { |
| 726 |
|
| 727 |
unsigned int *p = va_arg(*p_va, unsigned int *); |
| 728 |
unsigned int ival; |
| 729 |
if (float_argument_error(arg)) |
| 730 |
return converterr("integer<I>", arg, msgbuf, bufsize); |
| 731 |
ival = (unsigned int)PyInt_AsUnsignedLongMask(arg); |
| 732 |
if (ival == (unsigned int)-1 && PyErr_Occurred()) |
| 733 |
return converterr("integer<I>", arg, msgbuf, bufsize); |
| 734 |
else |
| 735 |
*p = ival; |
| 736 |
break; |
| 737 |
} |
| 738 |
|
| 739 |
case 'n': |
| 740 |
#if SIZEOF_SIZE_T != SIZEOF_LONG |
| 741 |
{ |
| 742 |
Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); |
| 743 |
Py_ssize_t ival; |
| 744 |
if (float_argument_error(arg)) |
| 745 |
return converterr("integer<n>", arg, msgbuf, bufsize); |
| 746 |
ival = PyInt_AsSsize_t(arg); |
| 747 |
if (ival == -1 && PyErr_Occurred()) |
| 748 |
return converterr("integer<n>", arg, msgbuf, bufsize); |
| 749 |
*p = ival; |
| 750 |
break; |
| 751 |
} |
| 752 |
#endif |
| 753 |
|
| 754 |
case 'l': { |
| 755 |
long *p = va_arg(*p_va, long *); |
| 756 |
long ival; |
| 757 |
if (float_argument_error(arg)) |
| 758 |
return converterr("integer<l>", arg, msgbuf, bufsize); |
| 759 |
ival = PyInt_AsLong(arg); |
| 760 |
if (ival == -1 && PyErr_Occurred()) |
| 761 |
return converterr("integer<l>", arg, msgbuf, bufsize); |
| 762 |
else |
| 763 |
*p = ival; |
| 764 |
break; |
| 765 |
} |
| 766 |
|
| 767 |
case 'k': { |
| 768 |
unsigned long *p = va_arg(*p_va, unsigned long *); |
| 769 |
unsigned long ival; |
| 770 |
if (PyInt_Check(arg)) |
| 771 |
ival = PyInt_AsUnsignedLongMask(arg); |
| 772 |
else if (PyLong_Check(arg)) |
| 773 |
ival = PyLong_AsUnsignedLongMask(arg); |
| 774 |
else |
| 775 |
return converterr("an integer", arg, msgbuf, bufsize); |
| 776 |
*p = ival; |
| 777 |
break; |
| 778 |
} |
| 779 |
|
| 780 |
#ifdef HAVE_LONG_LONG |
| 781 |
case 'L': { |
| 782 |
PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * ); |
| 783 |
PY_LONG_LONG ival; |
| 784 |
if (float_argument_warning(arg)) |
| 785 |
return converterr("long<L>", arg, msgbuf, bufsize); |
| 786 |
ival = PyLong_AsLongLong(arg); |
| 787 |
if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) { |
| 788 |
return converterr("long<L>", arg, msgbuf, bufsize); |
| 789 |
} else { |
| 790 |
*p = ival; |
| 791 |
} |
| 792 |
break; |
| 793 |
} |
| 794 |
|
| 795 |
case 'K': { |
| 796 |
unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *); |
| 797 |
unsigned PY_LONG_LONG ival; |
| 798 |
if (PyInt_Check(arg)) |
| 799 |
ival = PyInt_AsUnsignedLongMask(arg); |
| 800 |
else if (PyLong_Check(arg)) |
| 801 |
ival = PyLong_AsUnsignedLongLongMask(arg); |
| 802 |
else |
| 803 |
return converterr("an integer", arg, msgbuf, bufsize); |
| 804 |
*p = ival; |
| 805 |
break; |
| 806 |
} |
| 807 |
#endif |
| 808 |
|
| 809 |
case 'f': { |
| 810 |
float *p = va_arg(*p_va, float *); |
| 811 |
double dval = PyFloat_AsDouble(arg); |
| 812 |
if (PyErr_Occurred()) |
| 813 |
return converterr("float<f>", arg, msgbuf, bufsize); |
| 814 |
else |
| 815 |
*p = (float) dval; |
| 816 |
break; |
| 817 |
} |
| 818 |
|
| 819 |
case 'd': { |
| 820 |
double *p = va_arg(*p_va, double *); |
| 821 |
double dval = PyFloat_AsDouble(arg); |
| 822 |
if (PyErr_Occurred()) |
| 823 |
return converterr("float<d>", arg, msgbuf, bufsize); |
| 824 |
else |
| 825 |
*p = dval; |
| 826 |
break; |
| 827 |
} |
| 828 |
|
| 829 |
#ifndef WITHOUT_COMPLEX |
| 830 |
case 'D': { |
| 831 |
Py_complex *p = va_arg(*p_va, Py_complex *); |
| 832 |
Py_complex cval; |
| 833 |
cval = PyComplex_AsCComplex(arg); |
| 834 |
if (PyErr_Occurred()) |
| 835 |
return converterr("complex<D>", arg, msgbuf, bufsize); |
| 836 |
else |
| 837 |
*p = cval; |
| 838 |
break; |
| 839 |
} |
| 840 |
#endif |
| 841 |
|
| 842 |
case 'c': { |
| 843 |
char *p = va_arg(*p_va, char *); |
| 844 |
if (PyString_Check(arg) && PyString_Size(arg) == 1) |
| 845 |
*p = PyString_AS_STRING(arg)[0]; |
| 846 |
else |
| 847 |
return converterr("char", arg, msgbuf, bufsize); |
| 848 |
break; |
| 849 |
} |
| 850 |
|
| 851 |
case 's': { |
| 852 |
if (*format == '*') { |
| 853 |
Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *); |
| 854 |
|
| 855 |
if (PyString_Check(arg)) { |
| 856 |
PyBuffer_FillInfo(p, arg, |
| 857 |
PyString_AS_STRING(arg), PyString_GET_SIZE(arg), |
| 858 |
1, 0); |
| 859 |
} |
| 860 |
#ifdef Py_USING_UNICODE |
| 861 |
else if (PyUnicode_Check(arg)) { |
| 862 |
uarg = UNICODE_DEFAULT_ENCODING(arg); |
| 863 |
if (uarg == NULL) |
| 864 |
return converterr(CONV_UNICODE, |
| 865 |
arg, msgbuf, bufsize); |
| 866 |
PyBuffer_FillInfo(p, arg, |
| 867 |
PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg), |
| 868 |
1, 0); |
| 869 |
} |
| 870 |
#endif |
| 871 |
else { |
| 872 |
char *buf; |
| 873 |
if (getbuffer(arg, p, &buf) < 0) |
| 874 |
return converterr(buf, arg, msgbuf, bufsize); |
| 875 |
} |
| 876 |
if (addcleanup(p, freelist, cleanup_buffer)) { |
| 877 |
return converterr( |
| 878 |
"(cleanup problem)", |
| 879 |
arg, msgbuf, bufsize); |
| 880 |
} |
| 881 |
format++; |
| 882 |
} else if (*format == '#') { |
| 883 |
void **p = (void **)va_arg(*p_va, char **); |
| 884 |
FETCH_SIZE; |
| 885 |
|
| 886 |
if (PyString_Check(arg)) { |
| 887 |
*p = PyString_AS_STRING(arg); |
| 888 |
STORE_SIZE(PyString_GET_SIZE(arg)); |
| 889 |
} |
| 890 |
#ifdef Py_USING_UNICODE |
| 891 |
else if (PyUnicode_Check(arg)) { |
| 892 |
uarg = UNICODE_DEFAULT_ENCODING(arg); |
| 893 |
if (uarg == NULL) |
| 894 |
return converterr(CONV_UNICODE, |
| 895 |
arg, msgbuf, bufsize); |
| 896 |
*p = PyString_AS_STRING(uarg); |
| 897 |
STORE_SIZE(PyString_GET_SIZE(uarg)); |
| 898 |
} |
| 899 |
#endif |
| 900 |
else { |
| 901 |
char *buf; |
| 902 |
Py_ssize_t count = convertbuffer(arg, p, &buf); |
| 903 |
if (count < 0) |
| 904 |
return converterr(buf, arg, msgbuf, bufsize); |
| 905 |
STORE_SIZE(count); |
| 906 |
} |
| 907 |
format++; |
| 908 |
} else { |
| 909 |
char **p = va_arg(*p_va, char **); |
| 910 |
|
| 911 |
if (PyString_Check(arg)) |
| 912 |
*p = PyString_AS_STRING(arg); |
| 913 |
#ifdef Py_USING_UNICODE |
| 914 |
else if (PyUnicode_Check(arg)) { |
| 915 |
uarg = UNICODE_DEFAULT_ENCODING(arg); |
| 916 |
if (uarg == NULL) |
| 917 |
return converterr(CONV_UNICODE, |
| 918 |
arg, msgbuf, bufsize); |
| 919 |
*p = PyString_AS_STRING(uarg); |
| 920 |
} |
| 921 |
#endif |
| 922 |
else |
| 923 |
return converterr("string", arg, msgbuf, bufsize); |
| 924 |
if ((Py_ssize_t)strlen(*p) != PyString_Size(arg)) |
| 925 |
return converterr("string without null bytes", |
| 926 |
arg, msgbuf, bufsize); |
| 927 |
} |
| 928 |
break; |
| 929 |
} |
| 930 |
|
| 931 |
case 'z': { |
| 932 |
if (*format == '*') { |
| 933 |
Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *); |
| 934 |
|
| 935 |
if (arg == Py_None) |
| 936 |
PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0); |
| 937 |
else if (PyString_Check(arg)) { |
| 938 |
PyBuffer_FillInfo(p, arg, |
| 939 |
PyString_AS_STRING(arg), PyString_GET_SIZE(arg), |
| 940 |
1, 0); |
| 941 |
} |
| 942 |
#ifdef Py_USING_UNICODE |
| 943 |
else if (PyUnicode_Check(arg)) { |
| 944 |
uarg = UNICODE_DEFAULT_ENCODING(arg); |
| 945 |
if (uarg == NULL) |
| 946 |
return converterr(CONV_UNICODE, |
| 947 |
arg, msgbuf, bufsize); |
| 948 |
PyBuffer_FillInfo(p, arg, |
| 949 |
PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg), |
| 950 |
1, 0); |
| 951 |
} |
| 952 |
#endif |
| 953 |
else { |
| 954 |
char *buf; |
| 955 |
if (getbuffer(arg, p, &buf) < 0) |
| 956 |
return converterr(buf, arg, msgbuf, bufsize); |
| 957 |
} |
| 958 |
if (addcleanup(p, freelist, cleanup_buffer)) { |
| 959 |
return converterr( |
| 960 |
"(cleanup problem)", |
| 961 |
arg, msgbuf, bufsize); |
| 962 |
} |
| 963 |
format++; |
| 964 |
} else if (*format == '#') { |
| 965 |
void **p = (void **)va_arg(*p_va, char **); |
| 966 |
FETCH_SIZE; |
| 967 |
|
| 968 |
if (arg == Py_None) { |
| 969 |
*p = 0; |
| 970 |
STORE_SIZE(0); |
| 971 |
} |
| 972 |
else if (PyString_Check(arg)) { |
| 973 |
*p = PyString_AS_STRING(arg); |
| 974 |
STORE_SIZE(PyString_GET_SIZE(arg)); |
| 975 |
} |
| 976 |
#ifdef Py_USING_UNICODE |
| 977 |
else if (PyUnicode_Check(arg)) { |
| 978 |
uarg = UNICODE_DEFAULT_ENCODING(arg); |
| 979 |
if (uarg == NULL) |
| 980 |
return converterr(CONV_UNICODE, |
| 981 |
arg, msgbuf, bufsize); |
| 982 |
*p = PyString_AS_STRING(uarg); |
| 983 |
STORE_SIZE(PyString_GET_SIZE(uarg)); |
| 984 |
} |
| 985 |
#endif |
| 986 |
else { |
| 987 |
char *buf; |
| 988 |
Py_ssize_t count = convertbuffer(arg, p, &buf); |
| 989 |
if (count < 0) |
| 990 |
return converterr(buf, arg, msgbuf, bufsize); |
| 991 |
STORE_SIZE(count); |
| 992 |
} |
| 993 |
format++; |
| 994 |
} else { |
| 995 |
char **p = va_arg(*p_va, char **); |
| 996 |
|
| 997 |
if (arg == Py_None) |
| 998 |
*p = 0; |
| 999 |
else if (PyString_Check(arg)) |
| 1000 |
*p = PyString_AS_STRING(arg); |
| 1001 |
#ifdef Py_USING_UNICODE |
| 1002 |
else if (PyUnicode_Check(arg)) { |
| 1003 |
uarg = UNICODE_DEFAULT_ENCODING(arg); |
| 1004 |
if (uarg == NULL) |
| 1005 |
return converterr(CONV_UNICODE, |
| 1006 |
arg, msgbuf, bufsize); |
| 1007 |
*p = PyString_AS_STRING(uarg); |
| 1008 |
} |
| 1009 |
#endif |
| 1010 |
else |
| 1011 |
return converterr("string or None", |
| 1012 |
arg, msgbuf, bufsize); |
| 1013 |
if (*format == '#') { |
| 1014 |
FETCH_SIZE; |
| 1015 |
assert(0); |
| 1016 |
if (arg == Py_None) { |
| 1017 |
STORE_SIZE(0); |
| 1018 |
} else { |
| 1019 |
STORE_SIZE(PyString_Size(arg)); |
| 1020 |
} |
| 1021 |
format++; |
| 1022 |
} |
| 1023 |
else if (*p != NULL && |
| 1024 |
(Py_ssize_t)strlen(*p) != PyString_Size(arg)) |
| 1025 |
return converterr( |
| 1026 |
"string without null bytes or None", |
| 1027 |
arg, msgbuf, bufsize); |
| 1028 |
} |
| 1029 |
break; |
| 1030 |
} |
| 1031 |
|
| 1032 |
case 'e': { |
| 1033 |
char **buffer; |
| 1034 |
const char *encoding; |
| 1035 |
PyObject *s; |
| 1036 |
Py_ssize_t size; |
| 1037 |
int recode_strings; |
| 1038 |
|
| 1039 |
|
| 1040 |
encoding = (const char *)va_arg(*p_va, const char *); |
| 1041 |
#ifdef Py_USING_UNICODE |
| 1042 |
if (encoding == NULL) |
| 1043 |
encoding = PyUnicode_GetDefaultEncoding(); |
| 1044 |
#endif |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
if (*format == 's') |
| 1051 |
recode_strings = 1; |
| 1052 |
else if (*format == 't') |
| 1053 |
recode_strings = 0; |
| 1054 |
else |
| 1055 |
return converterr( |
| 1056 |
"(unknown parser marker combination)", |
| 1057 |
arg, msgbuf, bufsize); |
| 1058 |
buffer = (char **)va_arg(*p_va, char **); |
| 1059 |
format++; |
| 1060 |
if (buffer == NULL) |
| 1061 |
return converterr("(buffer is NULL)", |
| 1062 |
arg, msgbuf, bufsize); |
| 1063 |
|
| 1064 |
|
| 1065 |
if (!recode_strings && PyString_Check(arg)) { |
| 1066 |
s = arg; |
| 1067 |
Py_INCREF(s); |
| 1068 |
} |
| 1069 |
else { |
| 1070 |
#ifdef Py_USING_UNICODE |
| 1071 |
PyObject *u; |
| 1072 |
|
| 1073 |
|
| 1074 |
u = PyUnicode_FromObject(arg); |
| 1075 |
if (u == NULL) |
| 1076 |
return converterr( |
| 1077 |
"string or unicode or text buffer", |
| 1078 |
arg, msgbuf, bufsize); |
| 1079 |
|
| 1080 |
|
| 1081 |
s = PyUnicode_AsEncodedString(u, |
| 1082 |
encoding, |
| 1083 |
NULL); |
| 1084 |
Py_DECREF(u); |
| 1085 |
if (s == NULL) |
| 1086 |
return converterr("(encoding failed)", |
| 1087 |
arg, msgbuf, bufsize); |
| 1088 |
if (!PyString_Check(s)) { |
| 1089 |
Py_DECREF(s); |
| 1090 |
return converterr( |
| 1091 |
"(encoder failed to return a string)", |
| 1092 |
arg, msgbuf, bufsize); |
| 1093 |
} |
| 1094 |
#else |
| 1095 |
return converterr("string<e>", arg, msgbuf, bufsize); |
| 1096 |
#endif |
| 1097 |
} |
| 1098 |
size = PyString_GET_SIZE(s); |
| 1099 |
|
| 1100 |
|
| 1101 |
if (*format == '#') { |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
FETCH_SIZE; |
| 1124 |
|
| 1125 |
format++; |
| 1126 |
if (q == NULL && q2 == NULL) { |
| 1127 |
Py_DECREF(s); |
| 1128 |
return converterr( |
| 1129 |
"(buffer_len is NULL)", |
| 1130 |
arg, msgbuf, bufsize); |
| 1131 |
} |
| 1132 |
if (*buffer == NULL) { |
| 1133 |
*buffer = PyMem_NEW(char, size + 1); |
| 1134 |
if (*buffer == NULL) { |
| 1135 |
Py_DECREF(s); |
| 1136 |
return converterr( |
| 1137 |
"(memory error)", |
| 1138 |
arg, msgbuf, bufsize); |
| 1139 |
} |
| 1140 |
if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
| 1141 |
Py_DECREF(s); |
| 1142 |
return converterr( |
| 1143 |
"(cleanup problem)", |
| 1144 |
arg, msgbuf, bufsize); |
| 1145 |
} |
| 1146 |
} else { |
| 1147 |
if (size + 1 > BUFFER_LEN) { |
| 1148 |
Py_DECREF(s); |
| 1149 |
PyErr_Format(PyExc_TypeError, |
| 1150 |
"encoded string too long " |
| 1151 |
"(%zd, maximum length %zd)", |
| 1152 |
(Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1)); |
| 1153 |
return ""; |
| 1154 |
} |
| 1155 |
} |
| 1156 |
memcpy(*buffer, |
| 1157 |
PyString_AS_STRING(s), |
| 1158 |
size + 1); |
| 1159 |
|
| 1160 |
if (flags & FLAG_SIZE_T) { |
| 1161 |
*q2 = size; |
| 1162 |
} |
| 1163 |
else { |
| 1164 |
if (INT_MAX < size) { |
| 1165 |
Py_DECREF(s); |
| 1166 |
PyErr_SetString(PyExc_OverflowError, |
| 1167 |
"size does not fit in an int"); |
| 1168 |
return converterr("", arg, msgbuf, bufsize); |
| 1169 |
} |
| 1170 |
*q = (int)size; |
| 1171 |
} |
| 1172 |
} else { |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
if ((Py_ssize_t)strlen(PyString_AS_STRING(s)) |
| 1187 |
!= size) { |
| 1188 |
Py_DECREF(s); |
| 1189 |
return converterr( |
| 1190 |
"encoded string without null bytes", |
| 1191 |
arg, msgbuf, bufsize); |
| 1192 |
} |
| 1193 |
*buffer = PyMem_NEW(char, size + 1); |
| 1194 |
if (*buffer == NULL) { |
| 1195 |
Py_DECREF(s); |
| 1196 |
return converterr("(memory error)", |
| 1197 |
arg, msgbuf, bufsize); |
| 1198 |
} |
| 1199 |
if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
| 1200 |
Py_DECREF(s); |
| 1201 |
return converterr("(cleanup problem)", |
| 1202 |
arg, msgbuf, bufsize); |
| 1203 |
} |
| 1204 |
memcpy(*buffer, |
| 1205 |
PyString_AS_STRING(s), |
| 1206 |
size + 1); |
| 1207 |
} |
| 1208 |
Py_DECREF(s); |
| 1209 |
break; |
| 1210 |
} |
| 1211 |
|
| 1212 |
#ifdef Py_USING_UNICODE |
| 1213 |
case 'u': { |
| 1214 |
if (*format == '#') { |
| 1215 |
void **p = (void **)va_arg(*p_va, char **); |
| 1216 |
FETCH_SIZE; |
| 1217 |
if (PyUnicode_Check(arg)) { |
| 1218 |
*p = PyUnicode_AS_UNICODE(arg); |
| 1219 |
STORE_SIZE(PyUnicode_GET_SIZE(arg)); |
| 1220 |
} |
| 1221 |
else { |
| 1222 |
return converterr("cannot convert raw buffers", |
| 1223 |
arg, msgbuf, bufsize); |
| 1224 |
} |
| 1225 |
format++; |
| 1226 |
} else { |
| 1227 |
Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); |
| 1228 |
if (PyUnicode_Check(arg)) { |
| 1229 |
*p = PyUnicode_AS_UNICODE(arg); |
| 1230 |
if (_ustrlen(*p) != (size_t)PyUnicode_GET_SIZE(arg)) { |
| 1231 |
return converterr( |
| 1232 |
"unicode without null characters", |
| 1233 |
arg, msgbuf, bufsize); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
else |
| 1237 |
return converterr("unicode", arg, msgbuf, bufsize); |
| 1238 |
} |
| 1239 |
break; |
| 1240 |
} |
| 1241 |
#endif |
| 1242 |
|
| 1243 |
case 'S': { |
| 1244 |
PyObject **p = va_arg(*p_va, PyObject **); |
| 1245 |
if (PyString_Check(arg)) |
| 1246 |
*p = arg; |
| 1247 |
else |
| 1248 |
return converterr("string", arg, msgbuf, bufsize); |
| 1249 |
break; |
| 1250 |
} |
| 1251 |
|
| 1252 |
#ifdef Py_USING_UNICODE |
| 1253 |
case 'U': { |
| 1254 |
PyObject **p = va_arg(*p_va, PyObject **); |
| 1255 |
if (PyUnicode_Check(arg)) |
| 1256 |
*p = arg; |
| 1257 |
else |
| 1258 |
return converterr("unicode", arg, msgbuf, bufsize); |
| 1259 |
break; |
| 1260 |
} |
| 1261 |
#endif |
| 1262 |
|
| 1263 |
case 'O': { |
| 1264 |
PyTypeObject *type; |
| 1265 |
PyObject **p; |
| 1266 |
if (*format == '!') { |
| 1267 |
type = va_arg(*p_va, PyTypeObject*); |
| 1268 |
p = va_arg(*p_va, PyObject **); |
| 1269 |
format++; |
| 1270 |
if (PyType_IsSubtype(arg->ob_type, type)) |
| 1271 |
*p = arg; |
| 1272 |
else |
| 1273 |
return converterr(type->tp_name, arg, msgbuf, bufsize); |
| 1274 |
|
| 1275 |
} |
| 1276 |
else if (*format == '?') { |
| 1277 |
inquiry pred = va_arg(*p_va, inquiry); |
| 1278 |
p = va_arg(*p_va, PyObject **); |
| 1279 |
format++; |
| 1280 |
if ((*pred)(arg)) |
| 1281 |
*p = arg; |
| 1282 |
else |
| 1283 |
return converterr("(unspecified)", |
| 1284 |
arg, msgbuf, bufsize); |
| 1285 |
|
| 1286 |
} |
| 1287 |
else if (*format == '&') { |
| 1288 |
typedef int (*converter)(PyObject *, void *); |
| 1289 |
converter convert = va_arg(*p_va, converter); |
| 1290 |
void *addr = va_arg(*p_va, void *); |
| 1291 |
format++; |
| 1292 |
if (! (*convert)(arg, addr)) |
| 1293 |
return converterr("(unspecified)", |
| 1294 |
arg, msgbuf, bufsize); |
| 1295 |
} |
| 1296 |
else { |
| 1297 |
p = va_arg(*p_va, PyObject **); |
| 1298 |
*p = arg; |
| 1299 |
} |
| 1300 |
break; |
| 1301 |
} |
| 1302 |
|
| 1303 |
case 'w': { |
| 1304 |
void **p = va_arg(*p_va, void **); |
| 1305 |
void *res; |
| 1306 |
PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 1307 |
Py_ssize_t count; |
| 1308 |
|
| 1309 |
if (pb && pb->bf_releasebuffer && *format != '*') |
| 1310 |
|
| 1311 |
|
| 1312 |
return converterr("pinned buffer", arg, msgbuf, bufsize); |
| 1313 |
|
| 1314 |
if (pb && pb->bf_getbuffer && *format == '*') { |
| 1315 |
|
| 1316 |
|
| 1317 |
format++; |
| 1318 |
if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { |
| 1319 |
PyErr_Clear(); |
| 1320 |
return converterr("read-write buffer", arg, msgbuf, bufsize); |
| 1321 |
} |
| 1322 |
if (addcleanup(p, freelist, cleanup_buffer)) { |
| 1323 |
return converterr( |
| 1324 |
"(cleanup problem)", |
| 1325 |
arg, msgbuf, bufsize); |
| 1326 |
} |
| 1327 |
if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) |
| 1328 |
return converterr("contiguous buffer", arg, msgbuf, bufsize); |
| 1329 |
break; |
| 1330 |
} |
| 1331 |
|
| 1332 |
if (pb == NULL || |
| 1333 |
pb->bf_getwritebuffer == NULL || |
| 1334 |
pb->bf_getsegcount == NULL) |
| 1335 |
return converterr("read-write buffer", arg, msgbuf, bufsize); |
| 1336 |
if ((*pb->bf_getsegcount)(arg, NULL) != 1) |
| 1337 |
return converterr("single-segment read-write buffer", |
| 1338 |
arg, msgbuf, bufsize); |
| 1339 |
if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0) |
| 1340 |
return converterr("(unspecified)", arg, msgbuf, bufsize); |
| 1341 |
if (*format == '*') { |
| 1342 |
PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0); |
| 1343 |
format++; |
| 1344 |
} |
| 1345 |
else { |
| 1346 |
*p = res; |
| 1347 |
if (*format == '#') { |
| 1348 |
FETCH_SIZE; |
| 1349 |
STORE_SIZE(count); |
| 1350 |
format++; |
| 1351 |
} |
| 1352 |
} |
| 1353 |
break; |
| 1354 |
} |
| 1355 |
|
| 1356 |
case 't': { |
| 1357 |
char **p = va_arg(*p_va, char **); |
| 1358 |
PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 1359 |
Py_ssize_t count; |
| 1360 |
|
| 1361 |
if (*format++ != '#') |
| 1362 |
return converterr( |
| 1363 |
"invalid use of 't' format character", |
| 1364 |
arg, msgbuf, bufsize); |
| 1365 |
if (!PyType_HasFeature(arg->ob_type, |
| 1366 |
Py_TPFLAGS_HAVE_GETCHARBUFFER) || |
| 1367 |
pb == NULL || pb->bf_getcharbuffer == NULL || |
| 1368 |
pb->bf_getsegcount == NULL) |
| 1369 |
return converterr( |
| 1370 |
"string or read-only character buffer", |
| 1371 |
arg, msgbuf, bufsize); |
| 1372 |
|
| 1373 |
if (pb->bf_getsegcount(arg, NULL) != 1) |
| 1374 |
return converterr( |
| 1375 |
"string or single-segment read-only buffer", |
| 1376 |
arg, msgbuf, bufsize); |
| 1377 |
|
| 1378 |
if (pb->bf_releasebuffer) |
| 1379 |
return converterr( |
| 1380 |
"string or pinned buffer", |
| 1381 |
arg, msgbuf, bufsize); |
| 1382 |
|
| 1383 |
count = pb->bf_getcharbuffer(arg, 0, p); |
| 1384 |
if (count < 0) |
| 1385 |
return converterr("(unspecified)", arg, msgbuf, bufsize); |
| 1386 |
{ |
| 1387 |
FETCH_SIZE; |
| 1388 |
STORE_SIZE(count); |
| 1389 |
} |
| 1390 |
break; |
| 1391 |
} |
| 1392 |
|
| 1393 |
default: |
| 1394 |
return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize); |
| 1395 |
|
| 1396 |
} |
| 1397 |
|
| 1398 |
*p_format = format; |
| 1399 |
return NULL; |
| 1400 |
} |
| 1401 |
|
| 1402 |
static Py_ssize_t |
| 1403 |
convertbuffer(PyObject *arg, void **p, char **errmsg) |
| 1404 |
{ |
| 1405 |
PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 1406 |
Py_ssize_t count; |
| 1407 |
if (pb == NULL || |
| 1408 |
pb->bf_getreadbuffer == NULL || |
| 1409 |
pb->bf_getsegcount == NULL || |
| 1410 |
pb->bf_releasebuffer != NULL) { |
| 1411 |
*errmsg = "string or read-only buffer"; |
| 1412 |
return -1; |
| 1413 |
} |
| 1414 |
if ((*pb->bf_getsegcount)(arg, NULL) != 1) { |
| 1415 |
*errmsg = "string or single-segment read-only buffer"; |
| 1416 |
return -1; |
| 1417 |
} |
| 1418 |
if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) { |
| 1419 |
*errmsg = "(unspecified)"; |
| 1420 |
} |
| 1421 |
return count; |
| 1422 |
} |
| 1423 |
|
| 1424 |
static int |
| 1425 |
getbuffer(PyObject *arg, Py_buffer *view, char **errmsg) |
| 1426 |
{ |
| 1427 |
void *buf; |
| 1428 |
Py_ssize_t count; |
| 1429 |
PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 1430 |
if (pb == NULL) { |
| 1431 |
*errmsg = "string or buffer"; |
| 1432 |
return -1; |
| 1433 |
} |
| 1434 |
if (pb->bf_getbuffer) { |
| 1435 |
if (pb->bf_getbuffer(arg, view, 0) < 0) { |
| 1436 |
*errmsg = "convertible to a buffer"; |
| 1437 |
return -1; |
| 1438 |
} |
| 1439 |
if (!PyBuffer_IsContiguous(view, 'C')) { |
| 1440 |
*errmsg = "contiguous buffer"; |
| 1441 |
return -1; |
| 1442 |
} |
| 1443 |
return 0; |
| 1444 |
} |
| 1445 |
|
| 1446 |
count = convertbuffer(arg, &buf, errmsg); |
| 1447 |
if (count < 0) { |
| 1448 |
*errmsg = "convertible to a buffer"; |
| 1449 |
return count; |
| 1450 |
} |
| 1451 |
PyBuffer_FillInfo(view, arg, buf, count, 1, 0); |
| 1452 |
return 0; |
| 1453 |
} |
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
int |
| 1460 |
PyArg_ParseTupleAndKeywords(PyObject *args, |
| 1461 |
PyObject *keywords, |
| 1462 |
const char *format, |
| 1463 |
char **kwlist, ...) |
| 1464 |
{ |
| 1465 |
int retval; |
| 1466 |
va_list va; |
| 1467 |
|
| 1468 |
if ((args == NULL || !PyTuple_Check(args)) || |
| 1469 |
(keywords != NULL && !PyDict_Check(keywords)) || |
| 1470 |
format == NULL || |
| 1471 |
kwlist == NULL) |
| 1472 |
{ |
| 1473 |
PyErr_BadInternalCall(); |
| 1474 |
return 0; |
| 1475 |
} |
| 1476 |
|
| 1477 |
va_start(va, kwlist); |
| 1478 |
retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0); |
| 1479 |
va_end(va); |
| 1480 |
return retval; |
| 1481 |
} |
| 1482 |
|
| 1483 |
int |
| 1484 |
_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args, |
| 1485 |
PyObject *keywords, |
| 1486 |
const char *format, |
| 1487 |
char **kwlist, ...) |
| 1488 |
{ |
| 1489 |
int retval; |
| 1490 |
va_list va; |
| 1491 |
|
| 1492 |
if ((args == NULL || !PyTuple_Check(args)) || |
| 1493 |
(keywords != NULL && !PyDict_Check(keywords)) || |
| 1494 |
format == NULL || |
| 1495 |
kwlist == NULL) |
| 1496 |
{ |
| 1497 |
PyErr_BadInternalCall(); |
| 1498 |
return 0; |
| 1499 |
} |
| 1500 |
|
| 1501 |
va_start(va, kwlist); |
| 1502 |
retval = vgetargskeywords(args, keywords, format, |
| 1503 |
kwlist, &va, FLAG_SIZE_T); |
| 1504 |
va_end(va); |
| 1505 |
return retval; |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
int |
| 1510 |
PyArg_VaParseTupleAndKeywords(PyObject *args, |
| 1511 |
PyObject *keywords, |
| 1512 |
const char *format, |
| 1513 |
char **kwlist, va_list va) |
| 1514 |
{ |
| 1515 |
int retval; |
| 1516 |
va_list lva; |
| 1517 |
|
| 1518 |
if ((args == NULL || !PyTuple_Check(args)) || |
| 1519 |
(keywords != NULL && !PyDict_Check(keywords)) || |
| 1520 |
format == NULL || |
| 1521 |
kwlist == NULL) |
| 1522 |
{ |
| 1523 |
PyErr_BadInternalCall(); |
| 1524 |
return 0; |
| 1525 |
} |
| 1526 |
|
| 1527 |
#ifdef VA_LIST_IS_ARRAY |
| 1528 |
memcpy(lva, va, sizeof(va_list)); |
| 1529 |
#else |
| 1530 |
#ifdef __va_copy |
| 1531 |
__va_copy(lva, va); |
| 1532 |
#else |
| 1533 |
lva = va; |
| 1534 |
#endif |
| 1535 |
#endif |
| 1536 |
|
| 1537 |
retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0); |
| 1538 |
return retval; |
| 1539 |
} |
| 1540 |
|
| 1541 |
int |
| 1542 |
_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args, |
| 1543 |
PyObject *keywords, |
| 1544 |
const char *format, |
| 1545 |
char **kwlist, va_list va) |
| 1546 |
{ |
| 1547 |
int retval; |
| 1548 |
va_list lva; |
| 1549 |
|
| 1550 |
if ((args == NULL || !PyTuple_Check(args)) || |
| 1551 |
(keywords != NULL && !PyDict_Check(keywords)) || |
| 1552 |
format == NULL || |
| 1553 |
kwlist == NULL) |
| 1554 |
{ |
| 1555 |
PyErr_BadInternalCall(); |
| 1556 |
return 0; |
| 1557 |
} |
| 1558 |
|
| 1559 |
#ifdef VA_LIST_IS_ARRAY |
| 1560 |
memcpy(lva, va, sizeof(va_list)); |
| 1561 |
#else |
| 1562 |
#ifdef __va_copy |
| 1563 |
__va_copy(lva, va); |
| 1564 |
#else |
| 1565 |
lva = va; |
| 1566 |
#endif |
| 1567 |
#endif |
| 1568 |
|
| 1569 |
retval = vgetargskeywords(args, keywords, format, |
| 1570 |
kwlist, &lva, FLAG_SIZE_T); |
| 1571 |
return retval; |
| 1572 |
} |
| 1573 |
|
| 1574 |
#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') |
| 1575 |
|
| 1576 |
static int |
| 1577 |
vgetargskeywords(PyObject *args, PyObject *keywords, const char *format, |
| 1578 |
char **kwlist, va_list *p_va, int flags) |
| 1579 |
{ |
| 1580 |
char msgbuf[512]; |
| 1581 |
int levels[32]; |
| 1582 |
const char *fname, *msg, *custom_msg, *keyword; |
| 1583 |
int min = INT_MAX; |
| 1584 |
int i, len, nargs, nkeywords; |
| 1585 |
PyObject *freelist = NULL, *current_arg; |
| 1586 |
|
| 1587 |
assert(args != NULL && PyTuple_Check(args)); |
| 1588 |
assert(keywords == NULL || PyDict_Check(keywords)); |
| 1589 |
assert(format != NULL); |
| 1590 |
assert(kwlist != NULL); |
| 1591 |
assert(p_va != NULL); |
| 1592 |
|
| 1593 |
|
| 1594 |
fname = strchr(format, ':'); |
| 1595 |
if (fname) { |
| 1596 |
fname++; |
| 1597 |
custom_msg = NULL; |
| 1598 |
} |
| 1599 |
else { |
| 1600 |
custom_msg = strchr(format,';'); |
| 1601 |
if (custom_msg) |
| 1602 |
custom_msg++; |
| 1603 |
} |
| 1604 |
|
| 1605 |
|
| 1606 |
for (len=0; kwlist[len]; len++) |
| 1607 |
continue; |
| 1608 |
|
| 1609 |
nargs = PyTuple_GET_SIZE(args); |
| 1610 |
nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords); |
| 1611 |
if (nargs + nkeywords > len) { |
| 1612 |
PyErr_Format(PyExc_TypeError, "%s%s takes at most %d " |
| 1613 |
"argument%s (%d given)", |
| 1614 |
(fname == NULL) ? "function" : fname, |
| 1615 |
(fname == NULL) ? "" : "()", |
| 1616 |
len, |
| 1617 |
(len == 1) ? "" : "s", |
| 1618 |
nargs + nkeywords); |
| 1619 |
return 0; |
| 1620 |
} |
| 1621 |
|
| 1622 |
|
| 1623 |
for (i = 0; i < len; i++) { |
| 1624 |
keyword = kwlist[i]; |
| 1625 |
if (*format == '|') { |
| 1626 |
min = i; |
| 1627 |
format++; |
| 1628 |
} |
| 1629 |
if (IS_END_OF_FORMAT(*format)) { |
| 1630 |
PyErr_Format(PyExc_RuntimeError, |
| 1631 |
"More keyword list entries (%d) than " |
| 1632 |
"format specifiers (%d)", len, i); |
| 1633 |
return cleanreturn(0, freelist); |
| 1634 |
} |
| 1635 |
current_arg = NULL; |
| 1636 |
if (nkeywords) { |
| 1637 |
current_arg = PyDict_GetItemString(keywords, keyword); |
| 1638 |
} |
| 1639 |
if (current_arg) { |
| 1640 |
--nkeywords; |
| 1641 |
if (i < nargs) { |
| 1642 |
|
| 1643 |
PyErr_Format(PyExc_TypeError, |
| 1644 |
"Argument given by name ('%s') " |
| 1645 |
"and position (%d)", |
| 1646 |
keyword, i+1); |
| 1647 |
return cleanreturn(0, freelist); |
| 1648 |
} |
| 1649 |
} |
| 1650 |
else if (nkeywords && PyErr_Occurred()) |
| 1651 |
return cleanreturn(0, freelist); |
| 1652 |
else if (i < nargs) |
| 1653 |
current_arg = PyTuple_GET_ITEM(args, i); |
| 1654 |
|
| 1655 |
if (current_arg) { |
| 1656 |
msg = convertitem(current_arg, &format, p_va, flags, |
| 1657 |
levels, msgbuf, sizeof(msgbuf), &freelist); |
| 1658 |
if (msg) { |
| 1659 |
seterror(i+1, msg, levels, fname, custom_msg); |
| 1660 |
return cleanreturn(0, freelist); |
| 1661 |
} |
| 1662 |
continue; |
| 1663 |
} |
| 1664 |
|
| 1665 |
if (i < min) { |
| 1666 |
PyErr_Format(PyExc_TypeError, "Required argument " |
| 1667 |
"'%s' (pos %d) not found", |
| 1668 |
keyword, i+1); |
| 1669 |
return cleanreturn(0, freelist); |
| 1670 |
} |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
if (!nkeywords) |
| 1676 |
return cleanreturn(1, freelist); |
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
msg = skipitem(&format, p_va, flags); |
| 1681 |
if (msg) { |
| 1682 |
PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg, |
| 1683 |
format); |
| 1684 |
return cleanreturn(0, freelist); |
| 1685 |
} |
| 1686 |
} |
| 1687 |
|
| 1688 |
if (!IS_END_OF_FORMAT(*format) && *format != '|') { |
| 1689 |
PyErr_Format(PyExc_RuntimeError, |
| 1690 |
"more argument specifiers than keyword list entries " |
| 1691 |
"(remaining format:'%s')", format); |
| 1692 |
return cleanreturn(0, freelist); |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
if (nkeywords > 0) { |
| 1697 |
PyObject *key, *value; |
| 1698 |
Py_ssize_t pos = 0; |
| 1699 |
while (PyDict_Next(keywords, &pos, &key, &value)) { |
| 1700 |
int match = 0; |
| 1701 |
char *ks; |
| 1702 |
if (!PyString_Check(key)) { |
| 1703 |
PyErr_SetString(PyExc_TypeError, |
| 1704 |
"keywords must be strings"); |
| 1705 |
return cleanreturn(0, freelist); |
| 1706 |
} |
| 1707 |
ks = PyString_AsString(key); |
| 1708 |
for (i = 0; i < len; i++) { |
| 1709 |
if (!strcmp(ks, kwlist[i])) { |
| 1710 |
match = 1; |
| 1711 |
break; |
| 1712 |
} |
| 1713 |
} |
| 1714 |
if (!match) { |
| 1715 |
PyErr_Format(PyExc_TypeError, |
| 1716 |
"'%s' is an invalid keyword " |
| 1717 |
"argument for this function", |
| 1718 |
ks); |
| 1719 |
return cleanreturn(0, freelist); |
| 1720 |
} |
| 1721 |
} |
| 1722 |
} |
| 1723 |
|
| 1724 |
return cleanreturn(1, freelist); |
| 1725 |
} |
| 1726 |
|
| 1727 |
|
| 1728 |
static char * |
| 1729 |
skipitem(const char **p_format, va_list *p_va, int flags) |
| 1730 |
{ |
| 1731 |
const char *format = *p_format; |
| 1732 |
char c = *format++; |
| 1733 |
|
| 1734 |
switch (c) { |
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
case 'b': |
| 1740 |
case 'B': |
| 1741 |
case 'h': |
| 1742 |
case 'H': |
| 1743 |
case 'i': |
| 1744 |
case 'I': |
| 1745 |
case 'l': |
| 1746 |
case 'k': |
| 1747 |
#ifdef HAVE_LONG_LONG |
| 1748 |
case 'L': |
| 1749 |
case 'K': |
| 1750 |
#endif |
| 1751 |
case 'f': |
| 1752 |
case 'd': |
| 1753 |
#ifndef WITHOUT_COMPLEX |
| 1754 |
case 'D': |
| 1755 |
#endif |
| 1756 |
case 'c': |
| 1757 |
{ |
| 1758 |
(void) va_arg(*p_va, void *); |
| 1759 |
break; |
| 1760 |
} |
| 1761 |
|
| 1762 |
case 'n': |
| 1763 |
{ |
| 1764 |
(void) va_arg(*p_va, Py_ssize_t *); |
| 1765 |
break; |
| 1766 |
} |
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
case 'e': |
| 1771 |
{ |
| 1772 |
(void) va_arg(*p_va, const char *); |
| 1773 |
if (!(*format == 's' || *format == 't')) |
| 1774 |
|
| 1775 |
goto err; |
| 1776 |
format++; |
| 1777 |
|
| 1778 |
} |
| 1779 |
|
| 1780 |
case 's': |
| 1781 |
case 'z': |
| 1782 |
#ifdef Py_USING_UNICODE |
| 1783 |
case 'u': |
| 1784 |
#endif |
| 1785 |
case 't': |
| 1786 |
case 'w': |
| 1787 |
{ |
| 1788 |
(void) va_arg(*p_va, char **); |
| 1789 |
if (*format == '#') { |
| 1790 |
if (flags & FLAG_SIZE_T) |
| 1791 |
(void) va_arg(*p_va, Py_ssize_t *); |
| 1792 |
else |
| 1793 |
(void) va_arg(*p_va, int *); |
| 1794 |
format++; |
| 1795 |
} else if ((c == 's' || c == 'z' || c == 'w') && *format == '*') { |
| 1796 |
format++; |
| 1797 |
} |
| 1798 |
break; |
| 1799 |
} |
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
case 'S': |
| 1804 |
#ifdef Py_USING_UNICODE |
| 1805 |
case 'U': |
| 1806 |
#endif |
| 1807 |
{ |
| 1808 |
(void) va_arg(*p_va, PyObject **); |
| 1809 |
break; |
| 1810 |
} |
| 1811 |
|
| 1812 |
case 'O': |
| 1813 |
{ |
| 1814 |
if (*format == '!') { |
| 1815 |
format++; |
| 1816 |
(void) va_arg(*p_va, PyTypeObject*); |
| 1817 |
(void) va_arg(*p_va, PyObject **); |
| 1818 |
} |
| 1819 |
else if (*format == '&') { |
| 1820 |
typedef int (*converter)(PyObject *, void *); |
| 1821 |
(void) va_arg(*p_va, converter); |
| 1822 |
(void) va_arg(*p_va, void *); |
| 1823 |
format++; |
| 1824 |
} |
| 1825 |
else { |
| 1826 |
(void) va_arg(*p_va, PyObject **); |
| 1827 |
} |
| 1828 |
break; |
| 1829 |
} |
| 1830 |
|
| 1831 |
case '(': |
| 1832 |
{ |
| 1833 |
char *msg; |
| 1834 |
for (;;) { |
| 1835 |
if (*format==')') |
| 1836 |
break; |
| 1837 |
if (IS_END_OF_FORMAT(*format)) |
| 1838 |
return "Unmatched left paren in format " |
| 1839 |
"string"; |
| 1840 |
msg = skipitem(&format, p_va, flags); |
| 1841 |
if (msg) |
| 1842 |
return msg; |
| 1843 |
} |
| 1844 |
format++; |
| 1845 |
break; |
| 1846 |
} |
| 1847 |
|
| 1848 |
case ')': |
| 1849 |
return "Unmatched right paren in format string"; |
| 1850 |
|
| 1851 |
default: |
| 1852 |
err: |
| 1853 |
return "impossible<bad format char>"; |
| 1854 |
|
| 1855 |
} |
| 1856 |
|
| 1857 |
*p_format = format; |
| 1858 |
return NULL; |
| 1859 |
} |
| 1860 |
|
| 1861 |
|
| 1862 |
int |
| 1863 |
PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) |
| 1864 |
{ |
| 1865 |
Py_ssize_t i, l; |
| 1866 |
PyObject **o; |
| 1867 |
va_list vargs; |
| 1868 |
|
| 1869 |
#ifdef HAVE_STDARG_PROTOTYPES |
| 1870 |
va_start(vargs, max); |
| 1871 |
#else |
| 1872 |
va_start(vargs); |
| 1873 |
#endif |
| 1874 |
|
| 1875 |
assert(min >= 0); |
| 1876 |
assert(min <= max); |
| 1877 |
if (!PyTuple_Check(args)) { |
| 1878 |
va_end(vargs); |
| 1879 |
PyErr_SetString(PyExc_SystemError, |
| 1880 |
"PyArg_UnpackTuple() argument list is not a tuple"); |
| 1881 |
return 0; |
| 1882 |
} |
| 1883 |
l = PyTuple_GET_SIZE(args); |
| 1884 |
if (l < min) { |
| 1885 |
if (name != NULL) |
| 1886 |
PyErr_Format( |
| 1887 |
PyExc_TypeError, |
| 1888 |
"%s expected %s%zd arguments, got %zd", |
| 1889 |
name, (min == max ? "" : "at least "), min, l); |
| 1890 |
else |
| 1891 |
PyErr_Format( |
| 1892 |
PyExc_TypeError, |
| 1893 |
"unpacked tuple should have %s%zd elements," |
| 1894 |
" but has %zd", |
| 1895 |
(min == max ? "" : "at least "), min, l); |
| 1896 |
va_end(vargs); |
| 1897 |
return 0; |
| 1898 |
} |
| 1899 |
if (l > max) { |
| 1900 |
if (name != NULL) |
| 1901 |
PyErr_Format( |
| 1902 |
PyExc_TypeError, |
| 1903 |
"%s expected %s%zd arguments, got %zd", |
| 1904 |
name, (min == max ? "" : "at most "), max, l); |
| 1905 |
else |
| 1906 |
PyErr_Format( |
| 1907 |
PyExc_TypeError, |
| 1908 |
"unpacked tuple should have %s%zd elements," |
| 1909 |
" but has %zd", |
| 1910 |
(min == max ? "" : "at most "), max, l); |
| 1911 |
va_end(vargs); |
| 1912 |
return 0; |
| 1913 |
} |
| 1914 |
for (i = 0; i < l; i++) { |
| 1915 |
o = va_arg(vargs, PyObject **); |
| 1916 |
*o = PyTuple_GET_ITEM(args, i); |
| 1917 |
} |
| 1918 |
va_end(vargs); |
| 1919 |
return 1; |
| 1920 |
} |
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
int |
| 1929 |
_PyArg_NoKeywords(const char *funcname, PyObject *kw) |
| 1930 |
{ |
| 1931 |
if (kw == NULL) |
| 1932 |
return 1; |
| 1933 |
if (!PyDict_CheckExact(kw)) { |
| 1934 |
PyErr_BadInternalCall(); |
| 1935 |
return 0; |
| 1936 |
} |
| 1937 |
if (PyDict_Size(kw) == 0) |
| 1938 |
return 1; |
| 1939 |
|
| 1940 |
PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments", |
| 1941 |
funcname); |
| 1942 |
return 0; |
| 1943 |
} |
| 1944 |
#ifdef __cplusplus |
| 1945 |
}; |
| 1946 |
#endif |