| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "Python.h" |
| 5 |
#ifdef PYPY_VERSION |
| 6 |
#define _PyTuple_ITEMS PySequence_Fast_ITEMS |
| 7 |
#else |
| 8 |
#include "pycore_tupleobject.h" |
| 9 |
#endif |
| 10 |
|
| 11 |
#include <ctype.h> |
| 12 |
#include <float.h> |
| 13 |
|
| 14 |
|
| 15 |
#ifdef __cplusplus |
| 16 |
extern "C" { |
| 17 |
#endif |
| 18 |
int PyArg_Parse(PyObject *, const char *, ...); |
| 19 |
int PyArg_ParseTuple(PyObject *, const char *, ...); |
| 20 |
int PyArg_VaParse(PyObject *, const char *, va_list); |
| 21 |
|
| 22 |
int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, |
| 23 |
const char *, char **, ...); |
| 24 |
int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *, |
| 25 |
const char *, char **, va_list); |
| 26 |
|
| 27 |
int _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *, |
| 28 |
struct _PyArg_Parser *, ...); |
| 29 |
int _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *, |
| 30 |
struct _PyArg_Parser *, va_list); |
| 31 |
|
| 32 |
#ifdef HAVE_DECLSPEC_DLL |
| 33 |
|
| 34 |
PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...); |
| 35 |
PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, |
| 36 |
const char *format, ...); |
| 37 |
PyAPI_FUNC(int) _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, |
| 38 |
PyObject *kwnames, |
| 39 |
struct _PyArg_Parser *parser, ...); |
| 40 |
PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...); |
| 41 |
PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
| 42 |
const char *, char **, ...); |
| 43 |
PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...); |
| 44 |
PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list); |
| 45 |
PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
| 46 |
const char *, char **, va_list); |
| 47 |
|
| 48 |
PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *, |
| 49 |
struct _PyArg_Parser *, ...); |
| 50 |
PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *, |
| 51 |
struct _PyArg_Parser *, va_list); |
| 52 |
#endif |
| 53 |
|
| 54 |
#define FLAG_COMPAT 1 |
| 55 |
#define FLAG_SIZE_T 2 |
| 56 |
|
| 57 |
typedef int (*destr_t)(PyObject *, void *); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
typedef struct { |
| 65 |
void *item; |
| 66 |
destr_t destructor; |
| 67 |
} freelistentry_t; |
| 68 |
|
| 69 |
typedef struct { |
| 70 |
freelistentry_t *entries; |
| 71 |
int first_available; |
| 72 |
int entries_malloced; |
| 73 |
} freelist_t; |
| 74 |
|
| 75 |
#define STATIC_FREELIST_ENTRIES 8 |
| 76 |
|
| 77 |
|
| 78 |
static int vgetargs1_impl(PyObject *args, PyObject *const *stack, Py_ssize_t nargs, |
| 79 |
const char *format, va_list *p_va, int flags); |
| 80 |
static int vgetargs1(PyObject *, const char *, va_list *, int); |
| 81 |
static void seterror(Py_ssize_t, const char *, int *, const char *, const char *); |
| 82 |
static const char *convertitem(PyObject *, const char **, va_list *, int, int *, |
| 83 |
char *, size_t, freelist_t *); |
| 84 |
static const char *converttuple(PyObject *, const char **, va_list *, int, |
| 85 |
int *, char *, size_t, int, freelist_t *); |
| 86 |
static const char *convertsimple(PyObject *, const char **, va_list *, int, |
| 87 |
char *, size_t, freelist_t *); |
| 88 |
static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **); |
| 89 |
static int getbuffer(PyObject *, Py_buffer *, const char**); |
| 90 |
|
| 91 |
static int vgetargskeywords(PyObject *, PyObject *, |
| 92 |
const char *, char **, va_list *, int); |
| 93 |
static int vgetargskeywordsfast(PyObject *, PyObject *, |
| 94 |
struct _PyArg_Parser *, va_list *, int); |
| 95 |
static int vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, |
| 96 |
PyObject *keywords, PyObject *kwnames, |
| 97 |
struct _PyArg_Parser *parser, |
| 98 |
va_list *p_va, int flags); |
| 99 |
static const char *skipitem(const char **, va_list *, int); |
| 100 |
|
| 101 |
int |
| 102 |
PyArg_Parse(PyObject *args, const char *format, ...) |
| 103 |
{ |
| 104 |
int retval; |
| 105 |
va_list va; |
| 106 |
|
| 107 |
va_start(va, format); |
| 108 |
retval = vgetargs1(args, format, &va, FLAG_COMPAT); |
| 109 |
va_end(va); |
| 110 |
return retval; |
| 111 |
} |
| 112 |
|
| 113 |
PyAPI_FUNC(int) |
| 114 |
_PyArg_Parse_SizeT(PyObject *args, const char *format, ...) |
| 115 |
{ |
| 116 |
int retval; |
| 117 |
va_list va; |
| 118 |
|
| 119 |
va_start(va, format); |
| 120 |
retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T); |
| 121 |
va_end(va); |
| 122 |
return retval; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
int |
| 127 |
PyArg_ParseTuple(PyObject *args, const char *format, ...) |
| 128 |
{ |
| 129 |
int retval; |
| 130 |
va_list va; |
| 131 |
|
| 132 |
va_start(va, format); |
| 133 |
retval = vgetargs1(args, format, &va, 0); |
| 134 |
va_end(va); |
| 135 |
return retval; |
| 136 |
} |
| 137 |
|
| 138 |
PyAPI_FUNC(int) |
| 139 |
_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...) |
| 140 |
{ |
| 141 |
int retval; |
| 142 |
va_list va; |
| 143 |
|
| 144 |
va_start(va, format); |
| 145 |
retval = vgetargs1(args, format, &va, FLAG_SIZE_T); |
| 146 |
va_end(va); |
| 147 |
return retval; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
int |
| 152 |
_PyArg_ParseStack(PyObject *const *args, Py_ssize_t nargs, const char *format, ...) |
| 153 |
{ |
| 154 |
int retval; |
| 155 |
va_list va; |
| 156 |
|
| 157 |
va_start(va, format); |
| 158 |
retval = vgetargs1_impl(NULL, args, nargs, format, &va, 0); |
| 159 |
va_end(va); |
| 160 |
return retval; |
| 161 |
} |
| 162 |
|
| 163 |
PyAPI_FUNC(int) |
| 164 |
_PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, const char *format, ...) |
| 165 |
{ |
| 166 |
int retval; |
| 167 |
va_list va; |
| 168 |
|
| 169 |
va_start(va, format); |
| 170 |
retval = vgetargs1_impl(NULL, args, nargs, format, &va, FLAG_SIZE_T); |
| 171 |
va_end(va); |
| 172 |
return retval; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
int |
| 177 |
PyArg_VaParse(PyObject *args, const char *format, va_list va) |
| 178 |
{ |
| 179 |
va_list lva; |
| 180 |
int retval; |
| 181 |
|
| 182 |
va_copy(lva, va); |
| 183 |
|
| 184 |
retval = vgetargs1(args, format, &lva, 0); |
| 185 |
va_end(lva); |
| 186 |
return retval; |
| 187 |
} |
| 188 |
|
| 189 |
PyAPI_FUNC(int) |
| 190 |
_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va) |
| 191 |
{ |
| 192 |
va_list lva; |
| 193 |
int retval; |
| 194 |
|
| 195 |
va_copy(lva, va); |
| 196 |
|
| 197 |
retval = vgetargs1(args, format, &lva, FLAG_SIZE_T); |
| 198 |
va_end(lva); |
| 199 |
return retval; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
static int |
| 206 |
cleanup_ptr(PyObject *self, void *ptr) |
| 207 |
{ |
| 208 |
void **pptr = (void **)ptr; |
| 209 |
PyMem_Free(*pptr); |
| 210 |
*pptr = NULL; |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
static int |
| 215 |
cleanup_buffer(PyObject *self, void *ptr) |
| 216 |
{ |
| 217 |
Py_buffer *buf = (Py_buffer *)ptr; |
| 218 |
if (buf) { |
| 219 |
PyBuffer_Release(buf); |
| 220 |
} |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
static int |
| 225 |
addcleanup(void *ptr, freelist_t *freelist, destr_t destructor) |
| 226 |
{ |
| 227 |
int index; |
| 228 |
|
| 229 |
index = freelist->first_available; |
| 230 |
freelist->first_available += 1; |
| 231 |
|
| 232 |
freelist->entries[index].item = ptr; |
| 233 |
freelist->entries[index].destructor = destructor; |
| 234 |
|
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
static int |
| 239 |
cleanreturn(int retval, freelist_t *freelist) |
| 240 |
{ |
| 241 |
int index; |
| 242 |
|
| 243 |
if (retval == 0) { |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
for (index = 0; index < freelist->first_available; ++index) { |
| 248 |
freelist->entries[index].destructor(NULL, |
| 249 |
freelist->entries[index].item); |
| 250 |
} |
| 251 |
} |
| 252 |
if (freelist->entries_malloced) |
| 253 |
PyMem_Free(freelist->entries); |
| 254 |
return retval; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
static int |
| 259 |
vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs, const char *format, |
| 260 |
va_list *p_va, int flags) |
| 261 |
{ |
| 262 |
char msgbuf[256]; |
| 263 |
int levels[32]; |
| 264 |
const char *fname = NULL; |
| 265 |
const char *message = NULL; |
| 266 |
int min = -1; |
| 267 |
int max = 0; |
| 268 |
int level = 0; |
| 269 |
int endfmt = 0; |
| 270 |
const char *formatsave = format; |
| 271 |
Py_ssize_t i; |
| 272 |
const char *msg; |
| 273 |
int compat = flags & FLAG_COMPAT; |
| 274 |
freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
| 275 |
freelist_t freelist; |
| 276 |
|
| 277 |
assert(nargs == 0 || stack != NULL); |
| 278 |
|
| 279 |
freelist.entries = static_entries; |
| 280 |
freelist.first_available = 0; |
| 281 |
freelist.entries_malloced = 0; |
| 282 |
|
| 283 |
flags = flags & ~FLAG_COMPAT; |
| 284 |
|
| 285 |
while (endfmt == 0) { |
| 286 |
int c = *format++; |
| 287 |
switch (c) { |
| 288 |
case '(': |
| 289 |
if (level == 0) |
| 290 |
max++; |
| 291 |
level++; |
| 292 |
if (level >= 30) |
| 293 |
Py_FatalError("too many tuple nesting levels " |
| 294 |
"in argument format string"); |
| 295 |
break; |
| 296 |
case ')': |
| 297 |
if (level == 0) |
| 298 |
Py_FatalError("excess ')' in getargs format"); |
| 299 |
else |
| 300 |
level--; |
| 301 |
break; |
| 302 |
case '\0': |
| 303 |
endfmt = 1; |
| 304 |
break; |
| 305 |
case ':': |
| 306 |
fname = format; |
| 307 |
endfmt = 1; |
| 308 |
break; |
| 309 |
case ';': |
| 310 |
message = format; |
| 311 |
endfmt = 1; |
| 312 |
break; |
| 313 |
case '|': |
| 314 |
if (level == 0) |
| 315 |
min = max; |
| 316 |
break; |
| 317 |
default: |
| 318 |
if (level == 0) { |
| 319 |
if (isalpha(c)) |
| 320 |
if (c != 'e') |
| 321 |
max++; |
| 322 |
} |
| 323 |
break; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if (level != 0) |
| 328 |
Py_FatalError( "missing ')' in getargs format"); |
| 329 |
|
| 330 |
if (min < 0) |
| 331 |
min = max; |
| 332 |
|
| 333 |
format = formatsave; |
| 334 |
|
| 335 |
if (max > STATIC_FREELIST_ENTRIES) { |
| 336 |
freelist.entries = PyMem_NEW(freelistentry_t, max); |
| 337 |
if (freelist.entries == NULL) { |
| 338 |
PyErr_NoMemory(); |
| 339 |
return 0; |
| 340 |
} |
| 341 |
freelist.entries_malloced = 1; |
| 342 |
} |
| 343 |
|
| 344 |
if (compat) { |
| 345 |
if (max == 0) { |
| 346 |
if (compat_args == NULL) |
| 347 |
return 1; |
| 348 |
PyErr_Format(PyExc_TypeError, |
| 349 |
"%.200s%s takes no arguments", |
| 350 |
fname==NULL ? "function" : fname, |
| 351 |
fname==NULL ? "" : "()"); |
| 352 |
return cleanreturn(0, &freelist); |
| 353 |
} |
| 354 |
else if (min == 1 && max == 1) { |
| 355 |
if (compat_args == NULL) { |
| 356 |
PyErr_Format(PyExc_TypeError, |
| 357 |
"%.200s%s takes at least one argument", |
| 358 |
fname==NULL ? "function" : fname, |
| 359 |
fname==NULL ? "" : "()"); |
| 360 |
return cleanreturn(0, &freelist); |
| 361 |
} |
| 362 |
msg = convertitem(compat_args, &format, p_va, flags, levels, |
| 363 |
msgbuf, sizeof(msgbuf), &freelist); |
| 364 |
if (msg == NULL) |
| 365 |
return cleanreturn(1, &freelist); |
| 366 |
seterror(levels[0], msg, levels+1, fname, message); |
| 367 |
return cleanreturn(0, &freelist); |
| 368 |
} |
| 369 |
else { |
| 370 |
PyErr_SetString(PyExc_SystemError, |
| 371 |
"old style getargs format uses new features"); |
| 372 |
return cleanreturn(0, &freelist); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
if (nargs < min || max < nargs) { |
| 377 |
if (message == NULL) |
| 378 |
PyErr_Format(PyExc_TypeError, |
| 379 |
"%.150s%s takes %s %d argument%s (%zd given)", |
| 380 |
fname==NULL ? "function" : fname, |
| 381 |
fname==NULL ? "" : "()", |
| 382 |
min==max ? "exactly" |
| 383 |
: nargs < min ? "at least" : "at most", |
| 384 |
nargs < min ? min : max, |
| 385 |
(nargs < min ? min : max) == 1 ? "" : "s", |
| 386 |
nargs); |
| 387 |
else |
| 388 |
PyErr_SetString(PyExc_TypeError, message); |
| 389 |
return cleanreturn(0, &freelist); |
| 390 |
} |
| 391 |
|
| 392 |
for (i = 0; i < nargs; i++) { |
| 393 |
if (*format == '|') |
| 394 |
format++; |
| 395 |
msg = convertitem(stack[i], &format, p_va, |
| 396 |
flags, levels, msgbuf, |
| 397 |
sizeof(msgbuf), &freelist); |
| 398 |
if (msg) { |
| 399 |
seterror(i+1, msg, levels, fname, message); |
| 400 |
return cleanreturn(0, &freelist); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
if (*format != '\0' && !isalpha(*format) && |
| 405 |
*format != '(' && |
| 406 |
*format != '|' && *format != ':' && *format != ';') { |
| 407 |
PyErr_Format(PyExc_SystemError, |
| 408 |
"bad format string: %.200s", formatsave); |
| 409 |
return cleanreturn(0, &freelist); |
| 410 |
} |
| 411 |
|
| 412 |
return cleanreturn(1, &freelist); |
| 413 |
} |
| 414 |
|
| 415 |
static int |
| 416 |
vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags) |
| 417 |
{ |
| 418 |
PyObject **stack; |
| 419 |
Py_ssize_t nargs; |
| 420 |
|
| 421 |
if (!(flags & FLAG_COMPAT)) { |
| 422 |
assert(args != NULL); |
| 423 |
|
| 424 |
if (!PyTuple_Check(args)) { |
| 425 |
PyErr_SetString(PyExc_SystemError, |
| 426 |
"new style getargs format but argument is not a tuple"); |
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
stack = _PyTuple_ITEMS(args); |
| 431 |
nargs = PyTuple_GET_SIZE(args); |
| 432 |
} |
| 433 |
else { |
| 434 |
stack = NULL; |
| 435 |
nargs = 0; |
| 436 |
} |
| 437 |
|
| 438 |
return vgetargs1_impl(args, stack, nargs, format, p_va, flags); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
static void |
| 443 |
seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname, |
| 444 |
const char *message) |
| 445 |
{ |
| 446 |
char buf[512]; |
| 447 |
int i; |
| 448 |
char *p = buf; |
| 449 |
|
| 450 |
if (PyErr_Occurred()) |
| 451 |
return; |
| 452 |
else if (message == NULL) { |
| 453 |
if (fname != NULL) { |
| 454 |
PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname); |
| 455 |
p += strlen(p); |
| 456 |
} |
| 457 |
if (iarg != 0) { |
| 458 |
PyOS_snprintf(p, sizeof(buf) - (p - buf), |
| 459 |
"argument %zd", iarg); |
| 460 |
i = 0; |
| 461 |
p += strlen(p); |
| 462 |
while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) { |
| 463 |
PyOS_snprintf(p, sizeof(buf) - (p - buf), |
| 464 |
", item %d", levels[i]-1); |
| 465 |
p += strlen(p); |
| 466 |
i++; |
| 467 |
} |
| 468 |
} |
| 469 |
else { |
| 470 |
PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument"); |
| 471 |
p += strlen(p); |
| 472 |
} |
| 473 |
PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg); |
| 474 |
message = buf; |
| 475 |
} |
| 476 |
if (msg[0] == '(') { |
| 477 |
PyErr_SetString(PyExc_SystemError, message); |
| 478 |
} |
| 479 |
else { |
| 480 |
PyErr_SetString(PyExc_TypeError, message); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
static const char * |
| 504 |
converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
| 505 |
int *levels, char *msgbuf, size_t bufsize, int toplevel, |
| 506 |
freelist_t *freelist) |
| 507 |
{ |
| 508 |
int level = 0; |
| 509 |
int n = 0; |
| 510 |
const char *format = *p_format; |
| 511 |
int i; |
| 512 |
Py_ssize_t len; |
| 513 |
|
| 514 |
for (;;) { |
| 515 |
int c = *format++; |
| 516 |
if (c == '(') { |
| 517 |
if (level == 0) |
| 518 |
n++; |
| 519 |
level++; |
| 520 |
} |
| 521 |
else if (c == ')') { |
| 522 |
if (level == 0) |
| 523 |
break; |
| 524 |
level--; |
| 525 |
} |
| 526 |
else if (c == ':' || c == ';' || c == '\0') |
| 527 |
break; |
| 528 |
else if (level == 0 && isalpha(c) && c != 'e') |
| 529 |
n++; |
| 530 |
} |
| 531 |
|
| 532 |
if (!PySequence_Check(arg) || PyBytes_Check(arg)) { |
| 533 |
levels[0] = 0; |
| 534 |
PyOS_snprintf(msgbuf, bufsize, |
| 535 |
toplevel ? "expected %d arguments, not %.50s" : |
| 536 |
"must be %d-item sequence, not %.50s", |
| 537 |
n, |
| 538 |
arg == Py_None ? "None" : Py_TYPE(arg)->tp_name); |
| 539 |
return msgbuf; |
| 540 |
} |
| 541 |
|
| 542 |
len = PySequence_Size(arg); |
| 543 |
if (len != n) { |
| 544 |
levels[0] = 0; |
| 545 |
if (toplevel) { |
| 546 |
PyOS_snprintf(msgbuf, bufsize, |
| 547 |
"expected %d argument%s, not %zd", |
| 548 |
n, |
| 549 |
n == 1 ? "" : "s", |
| 550 |
len); |
| 551 |
} |
| 552 |
else { |
| 553 |
PyOS_snprintf(msgbuf, bufsize, |
| 554 |
"must be sequence of length %d, not %zd", |
| 555 |
n, len); |
| 556 |
} |
| 557 |
return msgbuf; |
| 558 |
} |
| 559 |
|
| 560 |
format = *p_format; |
| 561 |
for (i = 0; i < n; i++) { |
| 562 |
const char *msg; |
| 563 |
PyObject *item; |
| 564 |
item = PySequence_GetItem(arg, i); |
| 565 |
if (item == NULL) { |
| 566 |
PyErr_Clear(); |
| 567 |
levels[0] = i+1; |
| 568 |
levels[1] = 0; |
| 569 |
strncpy(msgbuf, "is not retrievable", bufsize); |
| 570 |
return msgbuf; |
| 571 |
} |
| 572 |
msg = convertitem(item, &format, p_va, flags, levels+1, |
| 573 |
msgbuf, bufsize, freelist); |
| 574 |
|
| 575 |
Py_XDECREF(item); |
| 576 |
if (msg != NULL) { |
| 577 |
levels[0] = i+1; |
| 578 |
return msg; |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
*p_format = format; |
| 583 |
return NULL; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
static const char * |
| 590 |
convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
| 591 |
int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist) |
| 592 |
{ |
| 593 |
const char *msg; |
| 594 |
const char *format = *p_format; |
| 595 |
|
| 596 |
if (*format == '(' ) { |
| 597 |
format++; |
| 598 |
msg = converttuple(arg, &format, p_va, flags, levels, msgbuf, |
| 599 |
bufsize, 0, freelist); |
| 600 |
if (msg == NULL) |
| 601 |
format++; |
| 602 |
} |
| 603 |
else { |
| 604 |
msg = convertsimple(arg, &format, p_va, flags, |
| 605 |
msgbuf, bufsize, freelist); |
| 606 |
if (msg != NULL) |
| 607 |
levels[0] = 0; |
| 608 |
} |
| 609 |
if (msg == NULL) |
| 610 |
*p_format = format; |
| 611 |
return msg; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
void |
| 621 |
_PyArg_BadArgument(const char *fname, const char *displayname, |
| 622 |
const char *expected, PyObject *arg) |
| 623 |
{ |
| 624 |
PyErr_Format(PyExc_TypeError, |
| 625 |
"%.200s() %.200s must be %.50s, not %.50s", |
| 626 |
fname, displayname, expected, |
| 627 |
arg == Py_None ? "None" : Py_TYPE(arg)->tp_name); |
| 628 |
} |
| 629 |
|
| 630 |
static const char * |
| 631 |
converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize) |
| 632 |
{ |
| 633 |
assert(expected != NULL); |
| 634 |
assert(arg != NULL); |
| 635 |
if (expected[0] == '(') { |
| 636 |
PyOS_snprintf(msgbuf, bufsize, |
| 637 |
"%.100s", expected); |
| 638 |
} |
| 639 |
else { |
| 640 |
PyOS_snprintf(msgbuf, bufsize, |
| 641 |
"must be %.50s, not %.50s", expected, |
| 642 |
arg == Py_None ? "None" : Py_TYPE(arg)->tp_name); |
| 643 |
} |
| 644 |
return msgbuf; |
| 645 |
} |
| 646 |
|
| 647 |
#define CONV_UNICODE "(unicode conversion error)" |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
static const char * |
| 659 |
convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
| 660 |
char *msgbuf, size_t bufsize, freelist_t *freelist) |
| 661 |
{ |
| 662 |
#define RETURN_ERR_OCCURRED return msgbuf |
| 663 |
|
| 664 |
#define REQUIRE_PY_SSIZE_T_CLEAN \ |
| 665 |
if (!(flags & FLAG_SIZE_T)) { \ |
| 666 |
PyErr_SetString(PyExc_SystemError, \ |
| 667 |
"PY_SSIZE_T_CLEAN macro must be defined for '#' formats"); \ |
| 668 |
RETURN_ERR_OCCURRED; \ |
| 669 |
} |
| 670 |
|
| 671 |
const char *format = *p_format; |
| 672 |
char c = *format++; |
| 673 |
const char *sarg; |
| 674 |
|
| 675 |
switch (c) { |
| 676 |
|
| 677 |
case 'b': { |
| 678 |
unsigned char *p = va_arg(*p_va, unsigned char *); |
| 679 |
long ival = PyLong_AsLong(arg); |
| 680 |
if (ival == -1 && PyErr_Occurred()) |
| 681 |
RETURN_ERR_OCCURRED; |
| 682 |
else if (ival < 0) { |
| 683 |
PyErr_SetString(PyExc_OverflowError, |
| 684 |
"unsigned byte integer is less than minimum"); |
| 685 |
RETURN_ERR_OCCURRED; |
| 686 |
} |
| 687 |
else if (ival > UCHAR_MAX) { |
| 688 |
PyErr_SetString(PyExc_OverflowError, |
| 689 |
"unsigned byte integer is greater than maximum"); |
| 690 |
RETURN_ERR_OCCURRED; |
| 691 |
} |
| 692 |
else |
| 693 |
*p = (unsigned char) ival; |
| 694 |
break; |
| 695 |
} |
| 696 |
|
| 697 |
case 'B': { |
| 698 |
|
| 699 |
unsigned char *p = va_arg(*p_va, unsigned char *); |
| 700 |
unsigned long ival = PyLong_AsUnsignedLongMask(arg); |
| 701 |
if (ival == (unsigned long)-1 && PyErr_Occurred()) |
| 702 |
RETURN_ERR_OCCURRED; |
| 703 |
else |
| 704 |
*p = (unsigned char) ival; |
| 705 |
break; |
| 706 |
} |
| 707 |
|
| 708 |
case 'h': { |
| 709 |
short *p = va_arg(*p_va, short *); |
| 710 |
long ival = PyLong_AsLong(arg); |
| 711 |
if (ival == -1 && PyErr_Occurred()) |
| 712 |
RETURN_ERR_OCCURRED; |
| 713 |
else if (ival < SHRT_MIN) { |
| 714 |
PyErr_SetString(PyExc_OverflowError, |
| 715 |
"signed short integer is less than minimum"); |
| 716 |
RETURN_ERR_OCCURRED; |
| 717 |
} |
| 718 |
else if (ival > SHRT_MAX) { |
| 719 |
PyErr_SetString(PyExc_OverflowError, |
| 720 |
"signed short integer is greater than maximum"); |
| 721 |
RETURN_ERR_OCCURRED; |
| 722 |
} |
| 723 |
else |
| 724 |
*p = (short) ival; |
| 725 |
break; |
| 726 |
} |
| 727 |
|
| 728 |
case 'H': { |
| 729 |
|
| 730 |
unsigned short *p = va_arg(*p_va, unsigned short *); |
| 731 |
unsigned long ival = PyLong_AsUnsignedLongMask(arg); |
| 732 |
if (ival == (unsigned long)-1 && PyErr_Occurred()) |
| 733 |
RETURN_ERR_OCCURRED; |
| 734 |
else |
| 735 |
*p = (unsigned short) ival; |
| 736 |
break; |
| 737 |
} |
| 738 |
|
| 739 |
case 'i': { |
| 740 |
int *p = va_arg(*p_va, int *); |
| 741 |
long ival = PyLong_AsLong(arg); |
| 742 |
if (ival == -1 && PyErr_Occurred()) |
| 743 |
RETURN_ERR_OCCURRED; |
| 744 |
else if (ival > INT_MAX) { |
| 745 |
PyErr_SetString(PyExc_OverflowError, |
| 746 |
"signed integer is greater than maximum"); |
| 747 |
RETURN_ERR_OCCURRED; |
| 748 |
} |
| 749 |
else if (ival < INT_MIN) { |
| 750 |
PyErr_SetString(PyExc_OverflowError, |
| 751 |
"signed integer is less than minimum"); |
| 752 |
RETURN_ERR_OCCURRED; |
| 753 |
} |
| 754 |
else |
| 755 |
*p = ival; |
| 756 |
break; |
| 757 |
} |
| 758 |
|
| 759 |
case 'I': { |
| 760 |
|
| 761 |
unsigned int *p = va_arg(*p_va, unsigned int *); |
| 762 |
unsigned long ival = PyLong_AsUnsignedLongMask(arg); |
| 763 |
if (ival == (unsigned long)-1 && PyErr_Occurred()) |
| 764 |
RETURN_ERR_OCCURRED; |
| 765 |
else |
| 766 |
*p = (unsigned int) ival; |
| 767 |
break; |
| 768 |
} |
| 769 |
|
| 770 |
case 'n': |
| 771 |
{ |
| 772 |
PyObject *iobj; |
| 773 |
Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); |
| 774 |
Py_ssize_t ival = -1; |
| 775 |
iobj = PyNumber_Index(arg); |
| 776 |
if (iobj != NULL) { |
| 777 |
ival = PyLong_AsSsize_t(iobj); |
| 778 |
Py_DECREF(iobj); |
| 779 |
} |
| 780 |
if (ival == -1 && PyErr_Occurred()) |
| 781 |
RETURN_ERR_OCCURRED; |
| 782 |
*p = ival; |
| 783 |
break; |
| 784 |
} |
| 785 |
case 'l': { |
| 786 |
long *p = va_arg(*p_va, long *); |
| 787 |
long ival = PyLong_AsLong(arg); |
| 788 |
if (ival == -1 && PyErr_Occurred()) |
| 789 |
RETURN_ERR_OCCURRED; |
| 790 |
else |
| 791 |
*p = ival; |
| 792 |
break; |
| 793 |
} |
| 794 |
|
| 795 |
case 'k': { |
| 796 |
unsigned long *p = va_arg(*p_va, unsigned long *); |
| 797 |
unsigned long ival; |
| 798 |
if (PyLong_Check(arg)) |
| 799 |
ival = PyLong_AsUnsignedLongMask(arg); |
| 800 |
else |
| 801 |
return converterr("int", arg, msgbuf, bufsize); |
| 802 |
*p = ival; |
| 803 |
break; |
| 804 |
} |
| 805 |
|
| 806 |
case 'L': { |
| 807 |
long long *p = va_arg( *p_va, long long * ); |
| 808 |
long long ival = PyLong_AsLongLong(arg); |
| 809 |
if (ival == (long long)-1 && PyErr_Occurred()) |
| 810 |
RETURN_ERR_OCCURRED; |
| 811 |
else |
| 812 |
*p = ival; |
| 813 |
break; |
| 814 |
} |
| 815 |
|
| 816 |
case 'K': { |
| 817 |
unsigned long long *p = va_arg(*p_va, unsigned long long *); |
| 818 |
unsigned long long ival; |
| 819 |
if (PyLong_Check(arg)) |
| 820 |
ival = PyLong_AsUnsignedLongLongMask(arg); |
| 821 |
else |
| 822 |
return converterr("int", arg, msgbuf, bufsize); |
| 823 |
*p = ival; |
| 824 |
break; |
| 825 |
} |
| 826 |
|
| 827 |
case 'f': { |
| 828 |
float *p = va_arg(*p_va, float *); |
| 829 |
double dval = PyFloat_AsDouble(arg); |
| 830 |
if (dval == -1.0 && PyErr_Occurred()) |
| 831 |
RETURN_ERR_OCCURRED; |
| 832 |
else |
| 833 |
*p = (float) dval; |
| 834 |
break; |
| 835 |
} |
| 836 |
|
| 837 |
case 'd': { |
| 838 |
double *p = va_arg(*p_va, double *); |
| 839 |
double dval = PyFloat_AsDouble(arg); |
| 840 |
if (dval == -1.0 && PyErr_Occurred()) |
| 841 |
RETURN_ERR_OCCURRED; |
| 842 |
else |
| 843 |
*p = dval; |
| 844 |
break; |
| 845 |
} |
| 846 |
|
| 847 |
case 'D': { |
| 848 |
Py_complex *p = va_arg(*p_va, Py_complex *); |
| 849 |
Py_complex cval; |
| 850 |
cval = PyComplex_AsCComplex(arg); |
| 851 |
if (PyErr_Occurred()) |
| 852 |
RETURN_ERR_OCCURRED; |
| 853 |
else |
| 854 |
*p = cval; |
| 855 |
break; |
| 856 |
} |
| 857 |
|
| 858 |
case 'c': { |
| 859 |
char *p = va_arg(*p_va, char *); |
| 860 |
if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1) |
| 861 |
*p = PyBytes_AS_STRING(arg)[0]; |
| 862 |
else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1) |
| 863 |
*p = PyByteArray_AS_STRING(arg)[0]; |
| 864 |
else |
| 865 |
return converterr("a byte string of length 1", arg, msgbuf, bufsize); |
| 866 |
break; |
| 867 |
} |
| 868 |
|
| 869 |
case 'C': { |
| 870 |
int *p = va_arg(*p_va, int *); |
| 871 |
int kind; |
| 872 |
const void *data; |
| 873 |
|
| 874 |
if (!PyUnicode_Check(arg)) |
| 875 |
return converterr("a unicode character", arg, msgbuf, bufsize); |
| 876 |
|
| 877 |
if (PyUnicode_READY(arg)) |
| 878 |
RETURN_ERR_OCCURRED; |
| 879 |
|
| 880 |
if (PyUnicode_GET_LENGTH(arg) != 1) |
| 881 |
return converterr("a unicode character", arg, msgbuf, bufsize); |
| 882 |
|
| 883 |
kind = PyUnicode_KIND(arg); |
| 884 |
data = PyUnicode_DATA(arg); |
| 885 |
*p = PyUnicode_READ(kind, data, 0); |
| 886 |
break; |
| 887 |
} |
| 888 |
|
| 889 |
case 'p': { |
| 890 |
int *p = va_arg(*p_va, int *); |
| 891 |
int val = PyObject_IsTrue(arg); |
| 892 |
if (val > 0) |
| 893 |
*p = 1; |
| 894 |
else if (val == 0) |
| 895 |
*p = 0; |
| 896 |
else |
| 897 |
RETURN_ERR_OCCURRED; |
| 898 |
break; |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
case 'y': { |
| 905 |
void **p = (void **)va_arg(*p_va, char **); |
| 906 |
const char *buf; |
| 907 |
Py_ssize_t count; |
| 908 |
if (*format == '*') { |
| 909 |
if (getbuffer(arg, (Py_buffer*)p, &buf) < 0) |
| 910 |
return converterr(buf, arg, msgbuf, bufsize); |
| 911 |
format++; |
| 912 |
if (addcleanup(p, freelist, cleanup_buffer)) { |
| 913 |
return converterr( |
| 914 |
"(cleanup problem)", |
| 915 |
arg, msgbuf, bufsize); |
| 916 |
} |
| 917 |
break; |
| 918 |
} |
| 919 |
count = convertbuffer(arg, (const void **)p, &buf); |
| 920 |
if (count < 0) |
| 921 |
return converterr(buf, arg, msgbuf, bufsize); |
| 922 |
if (*format == '#') { |
| 923 |
REQUIRE_PY_SSIZE_T_CLEAN; |
| 924 |
Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*); |
| 925 |
*psize = count; |
| 926 |
format++; |
| 927 |
} else { |
| 928 |
if (strlen(*p) != (size_t)count) { |
| 929 |
PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
| 930 |
RETURN_ERR_OCCURRED; |
| 931 |
} |
| 932 |
} |
| 933 |
break; |
| 934 |
} |
| 935 |
|
| 936 |
case 's': |
| 937 |
case 'z': |
| 938 |
{ |
| 939 |
if (*format == '*') { |
| 940 |
|
| 941 |
Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *); |
| 942 |
|
| 943 |
if (c == 'z' && arg == Py_None) |
| 944 |
PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0); |
| 945 |
else if (PyUnicode_Check(arg)) { |
| 946 |
Py_ssize_t len; |
| 947 |
sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 948 |
if (sarg == NULL) |
| 949 |
return converterr(CONV_UNICODE, |
| 950 |
arg, msgbuf, bufsize); |
| 951 |
PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0); |
| 952 |
} |
| 953 |
else { |
| 954 |
const 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 |
|
| 966 |
const void **p = (const void **)va_arg(*p_va, const char **); |
| 967 |
REQUIRE_PY_SSIZE_T_CLEAN; |
| 968 |
Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*); |
| 969 |
|
| 970 |
if (c == 'z' && arg == Py_None) { |
| 971 |
*p = NULL; |
| 972 |
*psize = 0; |
| 973 |
} |
| 974 |
else if (PyUnicode_Check(arg)) { |
| 975 |
Py_ssize_t len; |
| 976 |
sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 977 |
if (sarg == NULL) |
| 978 |
return converterr(CONV_UNICODE, |
| 979 |
arg, msgbuf, bufsize); |
| 980 |
*p = sarg; |
| 981 |
*psize = len; |
| 982 |
} |
| 983 |
else { |
| 984 |
|
| 985 |
const char *buf; |
| 986 |
Py_ssize_t count = convertbuffer(arg, p, &buf); |
| 987 |
if (count < 0) |
| 988 |
return converterr(buf, arg, msgbuf, bufsize); |
| 989 |
*psize = count; |
| 990 |
} |
| 991 |
format++; |
| 992 |
} else { |
| 993 |
|
| 994 |
const char **p = va_arg(*p_va, const char **); |
| 995 |
Py_ssize_t len; |
| 996 |
sarg = NULL; |
| 997 |
|
| 998 |
if (c == 'z' && arg == Py_None) |
| 999 |
*p = NULL; |
| 1000 |
else if (PyUnicode_Check(arg)) { |
| 1001 |
sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 1002 |
if (sarg == NULL) |
| 1003 |
return converterr(CONV_UNICODE, |
| 1004 |
arg, msgbuf, bufsize); |
| 1005 |
if (strlen(sarg) != (size_t)len) { |
| 1006 |
PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1007 |
RETURN_ERR_OCCURRED; |
| 1008 |
} |
| 1009 |
*p = sarg; |
| 1010 |
} |
| 1011 |
else |
| 1012 |
return converterr(c == 'z' ? "str or None" : "str", |
| 1013 |
arg, msgbuf, bufsize); |
| 1014 |
} |
| 1015 |
break; |
| 1016 |
} |
| 1017 |
|
| 1018 |
case 'u': |
| 1019 |
case 'Z': |
| 1020 |
{ |
| 1021 |
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 1022 |
"getargs: The '%c' format is deprecated. Use 'U' instead.", c)) { |
| 1023 |
RETURN_ERR_OCCURRED; |
| 1024 |
} |
| 1025 |
Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); |
| 1026 |
|
| 1027 |
if (*format == '#') { |
| 1028 |
|
| 1029 |
REQUIRE_PY_SSIZE_T_CLEAN; |
| 1030 |
Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*); |
| 1031 |
|
| 1032 |
if (c == 'Z' && arg == Py_None) { |
| 1033 |
*p = NULL; |
| 1034 |
*psize = 0; |
| 1035 |
} |
| 1036 |
else if (PyUnicode_Check(arg)) { |
| 1037 |
Py_ssize_t len; |
| 1038 |
*p = PyUnicode_AsUnicodeAndSize(arg, &len); |
| 1039 |
if (*p == NULL) |
| 1040 |
RETURN_ERR_OCCURRED; |
| 1041 |
*psize = len; |
| 1042 |
} |
| 1043 |
else |
| 1044 |
return converterr(c == 'Z' ? "str or None" : "str", |
| 1045 |
arg, msgbuf, bufsize); |
| 1046 |
format++; |
| 1047 |
} else { |
| 1048 |
|
| 1049 |
if (c == 'Z' && arg == Py_None) |
| 1050 |
*p = NULL; |
| 1051 |
else if (PyUnicode_Check(arg)) { |
| 1052 |
Py_ssize_t len; |
| 1053 |
*p = PyUnicode_AsUnicodeAndSize(arg, &len); |
| 1054 |
if (*p == NULL) |
| 1055 |
RETURN_ERR_OCCURRED; |
| 1056 |
if (wcslen(*p) != (size_t)len) { |
| 1057 |
PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1058 |
RETURN_ERR_OCCURRED; |
| 1059 |
} |
| 1060 |
} else |
| 1061 |
return converterr(c == 'Z' ? "str or None" : "str", |
| 1062 |
arg, msgbuf, bufsize); |
| 1063 |
} |
| 1064 |
break; |
| 1065 |
} |
| 1066 |
|
| 1067 |
case 'e': { |
| 1068 |
char **buffer; |
| 1069 |
const char *encoding; |
| 1070 |
PyObject *s; |
| 1071 |
int recode_strings; |
| 1072 |
Py_ssize_t size; |
| 1073 |
const char *ptr; |
| 1074 |
|
| 1075 |
|
| 1076 |
encoding = (const char *)va_arg(*p_va, const char *); |
| 1077 |
if (encoding == NULL) |
| 1078 |
encoding = PyUnicode_GetDefaultEncoding(); |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
if (*format == 's') |
| 1085 |
recode_strings = 1; |
| 1086 |
else if (*format == 't') |
| 1087 |
recode_strings = 0; |
| 1088 |
else |
| 1089 |
return converterr( |
| 1090 |
"(unknown parser marker combination)", |
| 1091 |
arg, msgbuf, bufsize); |
| 1092 |
buffer = (char **)va_arg(*p_va, char **); |
| 1093 |
format++; |
| 1094 |
if (buffer == NULL) |
| 1095 |
return converterr("(buffer is NULL)", |
| 1096 |
arg, msgbuf, bufsize); |
| 1097 |
|
| 1098 |
|
| 1099 |
if (!recode_strings && |
| 1100 |
(PyBytes_Check(arg) || PyByteArray_Check(arg))) { |
| 1101 |
s = arg; |
| 1102 |
Py_INCREF(s); |
| 1103 |
if (PyBytes_Check(arg)) { |
| 1104 |
size = PyBytes_GET_SIZE(s); |
| 1105 |
ptr = PyBytes_AS_STRING(s); |
| 1106 |
} |
| 1107 |
else { |
| 1108 |
size = PyByteArray_GET_SIZE(s); |
| 1109 |
ptr = PyByteArray_AS_STRING(s); |
| 1110 |
} |
| 1111 |
} |
| 1112 |
else if (PyUnicode_Check(arg)) { |
| 1113 |
|
| 1114 |
s = PyUnicode_AsEncodedString(arg, |
| 1115 |
encoding, |
| 1116 |
NULL); |
| 1117 |
if (s == NULL) |
| 1118 |
return converterr("(encoding failed)", |
| 1119 |
arg, msgbuf, bufsize); |
| 1120 |
assert(PyBytes_Check(s)); |
| 1121 |
size = PyBytes_GET_SIZE(s); |
| 1122 |
ptr = PyBytes_AS_STRING(s); |
| 1123 |
if (ptr == NULL) |
| 1124 |
ptr = ""; |
| 1125 |
} |
| 1126 |
else { |
| 1127 |
return converterr( |
| 1128 |
recode_strings ? "str" : "str, bytes or bytearray", |
| 1129 |
arg, msgbuf, bufsize); |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
if (*format == '#') { |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
REQUIRE_PY_SSIZE_T_CLEAN; |
| 1156 |
Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*); |
| 1157 |
|
| 1158 |
format++; |
| 1159 |
if (psize == NULL) { |
| 1160 |
Py_DECREF(s); |
| 1161 |
return converterr( |
| 1162 |
"(buffer_len is NULL)", |
| 1163 |
arg, msgbuf, bufsize); |
| 1164 |
} |
| 1165 |
if (*buffer == NULL) { |
| 1166 |
*buffer = PyMem_NEW(char, size + 1); |
| 1167 |
if (*buffer == NULL) { |
| 1168 |
Py_DECREF(s); |
| 1169 |
PyErr_NoMemory(); |
| 1170 |
RETURN_ERR_OCCURRED; |
| 1171 |
} |
| 1172 |
if (addcleanup(buffer, freelist, cleanup_ptr)) { |
| 1173 |
Py_DECREF(s); |
| 1174 |
return converterr( |
| 1175 |
"(cleanup problem)", |
| 1176 |
arg, msgbuf, bufsize); |
| 1177 |
} |
| 1178 |
} else { |
| 1179 |
if (size + 1 > *psize) { |
| 1180 |
Py_DECREF(s); |
| 1181 |
PyErr_Format(PyExc_ValueError, |
| 1182 |
"encoded string too long " |
| 1183 |
"(%zd, maximum length %zd)", |
| 1184 |
(Py_ssize_t)size, (Py_ssize_t)(*psize - 1)); |
| 1185 |
RETURN_ERR_OCCURRED; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
memcpy(*buffer, ptr, size+1); |
| 1189 |
|
| 1190 |
*psize = size; |
| 1191 |
} |
| 1192 |
else { |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
if ((Py_ssize_t)strlen(ptr) != size) { |
| 1207 |
Py_DECREF(s); |
| 1208 |
return converterr( |
| 1209 |
"encoded string without null bytes", |
| 1210 |
arg, msgbuf, bufsize); |
| 1211 |
} |
| 1212 |
*buffer = PyMem_NEW(char, size + 1); |
| 1213 |
if (*buffer == NULL) { |
| 1214 |
Py_DECREF(s); |
| 1215 |
PyErr_NoMemory(); |
| 1216 |
RETURN_ERR_OCCURRED; |
| 1217 |
} |
| 1218 |
if (addcleanup(buffer, freelist, cleanup_ptr)) { |
| 1219 |
Py_DECREF(s); |
| 1220 |
return converterr("(cleanup problem)", |
| 1221 |
arg, msgbuf, bufsize); |
| 1222 |
} |
| 1223 |
memcpy(*buffer, ptr, size+1); |
| 1224 |
} |
| 1225 |
Py_DECREF(s); |
| 1226 |
break; |
| 1227 |
} |
| 1228 |
|
| 1229 |
case 'S': { |
| 1230 |
PyObject **p = va_arg(*p_va, PyObject **); |
| 1231 |
if (PyBytes_Check(arg)) |
| 1232 |
*p = arg; |
| 1233 |
else |
| 1234 |
return converterr("bytes", arg, msgbuf, bufsize); |
| 1235 |
break; |
| 1236 |
} |
| 1237 |
|
| 1238 |
case 'Y': { |
| 1239 |
PyObject **p = va_arg(*p_va, PyObject **); |
| 1240 |
if (PyByteArray_Check(arg)) |
| 1241 |
*p = arg; |
| 1242 |
else |
| 1243 |
return converterr("bytearray", arg, msgbuf, bufsize); |
| 1244 |
break; |
| 1245 |
} |
| 1246 |
|
| 1247 |
case 'U': { |
| 1248 |
PyObject **p = va_arg(*p_va, PyObject **); |
| 1249 |
if (PyUnicode_Check(arg)) { |
| 1250 |
if (PyUnicode_READY(arg) == -1) |
| 1251 |
RETURN_ERR_OCCURRED; |
| 1252 |
*p = arg; |
| 1253 |
} |
| 1254 |
else |
| 1255 |
return converterr("str", arg, msgbuf, bufsize); |
| 1256 |
break; |
| 1257 |
} |
| 1258 |
|
| 1259 |
case 'O': { |
| 1260 |
PyTypeObject *type; |
| 1261 |
PyObject **p; |
| 1262 |
if (*format == '!') { |
| 1263 |
type = va_arg(*p_va, PyTypeObject*); |
| 1264 |
p = va_arg(*p_va, PyObject **); |
| 1265 |
format++; |
| 1266 |
if (PyType_IsSubtype(Py_TYPE(arg), type)) |
| 1267 |
*p = arg; |
| 1268 |
else |
| 1269 |
return converterr(type->tp_name, arg, msgbuf, bufsize); |
| 1270 |
|
| 1271 |
} |
| 1272 |
else if (*format == '&') { |
| 1273 |
typedef int (*converter)(PyObject *, void *); |
| 1274 |
converter convert = va_arg(*p_va, converter); |
| 1275 |
void *addr = va_arg(*p_va, void *); |
| 1276 |
int res; |
| 1277 |
format++; |
| 1278 |
if (! (res = (*convert)(arg, addr))) |
| 1279 |
return converterr("(unspecified)", |
| 1280 |
arg, msgbuf, bufsize); |
| 1281 |
if (res == Py_CLEANUP_SUPPORTED && |
| 1282 |
addcleanup(addr, freelist, convert) == -1) |
| 1283 |
return converterr("(cleanup problem)", |
| 1284 |
arg, msgbuf, bufsize); |
| 1285 |
} |
| 1286 |
else { |
| 1287 |
p = va_arg(*p_va, PyObject **); |
| 1288 |
*p = arg; |
| 1289 |
} |
| 1290 |
break; |
| 1291 |
} |
| 1292 |
|
| 1293 |
|
| 1294 |
case 'w': { |
| 1295 |
void **p = va_arg(*p_va, void **); |
| 1296 |
|
| 1297 |
if (*format != '*') |
| 1298 |
return converterr( |
| 1299 |
"(invalid use of 'w' format character)", |
| 1300 |
arg, msgbuf, bufsize); |
| 1301 |
format++; |
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { |
| 1306 |
PyErr_Clear(); |
| 1307 |
return converterr("read-write bytes-like object", |
| 1308 |
arg, msgbuf, bufsize); |
| 1309 |
} |
| 1310 |
if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) { |
| 1311 |
PyBuffer_Release((Py_buffer*)p); |
| 1312 |
return converterr("contiguous buffer", arg, msgbuf, bufsize); |
| 1313 |
} |
| 1314 |
if (addcleanup(p, freelist, cleanup_buffer)) { |
| 1315 |
return converterr( |
| 1316 |
"(cleanup problem)", |
| 1317 |
arg, msgbuf, bufsize); |
| 1318 |
} |
| 1319 |
break; |
| 1320 |
} |
| 1321 |
|
| 1322 |
default: |
| 1323 |
return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize); |
| 1324 |
|
| 1325 |
} |
| 1326 |
|
| 1327 |
*p_format = format; |
| 1328 |
return NULL; |
| 1329 |
|
| 1330 |
#undef REQUIRE_PY_SSIZE_T_CLEAN |
| 1331 |
#undef RETURN_ERR_OCCURRED |
| 1332 |
} |
| 1333 |
|
| 1334 |
static Py_ssize_t |
| 1335 |
convertbuffer(PyObject *arg, const void **p, const char **errmsg) |
| 1336 |
{ |
| 1337 |
PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer; |
| 1338 |
Py_ssize_t count; |
| 1339 |
Py_buffer view; |
| 1340 |
|
| 1341 |
*errmsg = NULL; |
| 1342 |
*p = NULL; |
| 1343 |
if (pb != NULL && pb->bf_releasebuffer != NULL) { |
| 1344 |
*errmsg = "read-only bytes-like object"; |
| 1345 |
return -1; |
| 1346 |
} |
| 1347 |
|
| 1348 |
if (getbuffer(arg, &view, errmsg) < 0) |
| 1349 |
return -1; |
| 1350 |
count = view.len; |
| 1351 |
*p = view.buf; |
| 1352 |
PyBuffer_Release(&view); |
| 1353 |
return count; |
| 1354 |
} |
| 1355 |
|
| 1356 |
static int |
| 1357 |
getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg) |
| 1358 |
{ |
| 1359 |
if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) { |
| 1360 |
*errmsg = "bytes-like object"; |
| 1361 |
return -1; |
| 1362 |
} |
| 1363 |
if (!PyBuffer_IsContiguous(view, 'C')) { |
| 1364 |
PyBuffer_Release(view); |
| 1365 |
*errmsg = "contiguous buffer"; |
| 1366 |
return -1; |
| 1367 |
} |
| 1368 |
return 0; |
| 1369 |
} |
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
int |
| 1376 |
PyArg_ParseTupleAndKeywords(PyObject *args, |
| 1377 |
PyObject *keywords, |
| 1378 |
const char *format, |
| 1379 |
char **kwlist, ...) |
| 1380 |
{ |
| 1381 |
int retval; |
| 1382 |
va_list va; |
| 1383 |
|
| 1384 |
if ((args == NULL || !PyTuple_Check(args)) || |
| 1385 |
(keywords != NULL && !PyDict_Check(keywords)) || |
| 1386 |
format == NULL || |
| 1387 |
kwlist == NULL) |
| 1388 |
{ |
| 1389 |
PyErr_BadInternalCall(); |
| 1390 |
return 0; |
| 1391 |
} |
| 1392 |
|
| 1393 |
va_start(va, kwlist); |
| 1394 |
retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0); |
| 1395 |
va_end(va); |
| 1396 |
return retval; |
| 1397 |
} |
| 1398 |
|
| 1399 |
PyAPI_FUNC(int) |
| 1400 |
_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args, |
| 1401 |
PyObject *keywords, |
| 1402 |
const char *format, |
| 1403 |
char **kwlist, ...) |
| 1404 |
{ |
| 1405 |
int retval; |
| 1406 |
va_list va; |
| 1407 |
|
| 1408 |
if ((args == NULL || !PyTuple_Check(args)) || |
| 1409 |
(keywords != NULL && !PyDict_Check(keywords)) || |
| 1410 |
format == NULL || |
| 1411 |
kwlist == NULL) |
| 1412 |
{ |
| 1413 |
PyErr_BadInternalCall(); |
| 1414 |
return 0; |
| 1415 |
} |
| 1416 |
|
| 1417 |
va_start(va, kwlist); |
| 1418 |
retval = vgetargskeywords(args, keywords, format, |
| 1419 |
kwlist, &va, FLAG_SIZE_T); |
| 1420 |
va_end(va); |
| 1421 |
return retval; |
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
int |
| 1426 |
PyArg_VaParseTupleAndKeywords(PyObject *args, |
| 1427 |
PyObject *keywords, |
| 1428 |
const char *format, |
| 1429 |
char **kwlist, va_list va) |
| 1430 |
{ |
| 1431 |
int retval; |
| 1432 |
va_list lva; |
| 1433 |
|
| 1434 |
if ((args == NULL || !PyTuple_Check(args)) || |
| 1435 |
(keywords != NULL && !PyDict_Check(keywords)) || |
| 1436 |
format == NULL || |
| 1437 |
kwlist == NULL) |
| 1438 |
{ |
| 1439 |
PyErr_BadInternalCall(); |
| 1440 |
return 0; |
| 1441 |
} |
| 1442 |
|
| 1443 |
va_copy(lva, va); |
| 1444 |
|
| 1445 |
retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0); |
| 1446 |
va_end(lva); |
| 1447 |
return retval; |
| 1448 |
} |
| 1449 |
|
| 1450 |
PyAPI_FUNC(int) |
| 1451 |
_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args, |
| 1452 |
PyObject *keywords, |
| 1453 |
const char *format, |
| 1454 |
char **kwlist, va_list va) |
| 1455 |
{ |
| 1456 |
int retval; |
| 1457 |
va_list lva; |
| 1458 |
|
| 1459 |
if ((args == NULL || !PyTuple_Check(args)) || |
| 1460 |
(keywords != NULL && !PyDict_Check(keywords)) || |
| 1461 |
format == NULL || |
| 1462 |
kwlist == NULL) |
| 1463 |
{ |
| 1464 |
PyErr_BadInternalCall(); |
| 1465 |
return 0; |
| 1466 |
} |
| 1467 |
|
| 1468 |
va_copy(lva, va); |
| 1469 |
|
| 1470 |
retval = vgetargskeywords(args, keywords, format, |
| 1471 |
kwlist, &lva, FLAG_SIZE_T); |
| 1472 |
va_end(lva); |
| 1473 |
return retval; |
| 1474 |
} |
| 1475 |
|
| 1476 |
PyAPI_FUNC(int) |
| 1477 |
_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, |
| 1478 |
struct _PyArg_Parser *parser, ...) |
| 1479 |
{ |
| 1480 |
int retval; |
| 1481 |
va_list va; |
| 1482 |
|
| 1483 |
va_start(va, parser); |
| 1484 |
retval = vgetargskeywordsfast(args, keywords, parser, &va, 0); |
| 1485 |
va_end(va); |
| 1486 |
return retval; |
| 1487 |
} |
| 1488 |
|
| 1489 |
PyAPI_FUNC(int) |
| 1490 |
_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, |
| 1491 |
struct _PyArg_Parser *parser, ...) |
| 1492 |
{ |
| 1493 |
int retval; |
| 1494 |
va_list va; |
| 1495 |
|
| 1496 |
va_start(va, parser); |
| 1497 |
retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T); |
| 1498 |
va_end(va); |
| 1499 |
return retval; |
| 1500 |
} |
| 1501 |
|
| 1502 |
PyAPI_FUNC(int) |
| 1503 |
_PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
| 1504 |
struct _PyArg_Parser *parser, ...) |
| 1505 |
{ |
| 1506 |
int retval; |
| 1507 |
va_list va; |
| 1508 |
|
| 1509 |
va_start(va, parser); |
| 1510 |
retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0); |
| 1511 |
va_end(va); |
| 1512 |
return retval; |
| 1513 |
} |
| 1514 |
|
| 1515 |
PyAPI_FUNC(int) |
| 1516 |
_PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
| 1517 |
struct _PyArg_Parser *parser, ...) |
| 1518 |
{ |
| 1519 |
int retval; |
| 1520 |
va_list va; |
| 1521 |
|
| 1522 |
va_start(va, parser); |
| 1523 |
retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T); |
| 1524 |
va_end(va); |
| 1525 |
return retval; |
| 1526 |
} |
| 1527 |
|
| 1528 |
|
| 1529 |
PyAPI_FUNC(int) |
| 1530 |
_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, |
| 1531 |
struct _PyArg_Parser *parser, va_list va) |
| 1532 |
{ |
| 1533 |
int retval; |
| 1534 |
va_list lva; |
| 1535 |
|
| 1536 |
va_copy(lva, va); |
| 1537 |
|
| 1538 |
retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0); |
| 1539 |
va_end(lva); |
| 1540 |
return retval; |
| 1541 |
} |
| 1542 |
|
| 1543 |
PyAPI_FUNC(int) |
| 1544 |
_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, |
| 1545 |
struct _PyArg_Parser *parser, va_list va) |
| 1546 |
{ |
| 1547 |
int retval; |
| 1548 |
va_list lva; |
| 1549 |
|
| 1550 |
va_copy(lva, va); |
| 1551 |
|
| 1552 |
retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T); |
| 1553 |
va_end(lva); |
| 1554 |
return retval; |
| 1555 |
} |
| 1556 |
|
| 1557 |
static void |
| 1558 |
error_unexpected_keyword_arg(PyObject *kwargs, PyObject *kwnames, PyObject *kwtuple, const char *fname) |
| 1559 |
{ |
| 1560 |
|
| 1561 |
Py_ssize_t j = 0; |
| 1562 |
while (1) { |
| 1563 |
PyObject *keyword; |
| 1564 |
if (kwargs != NULL) { |
| 1565 |
if (!PyDict_Next(kwargs, &j, &keyword, NULL)) |
| 1566 |
break; |
| 1567 |
} |
| 1568 |
else { |
| 1569 |
if (j >= PyTuple_GET_SIZE(kwnames)) |
| 1570 |
break; |
| 1571 |
keyword = PyTuple_GET_ITEM(kwnames, j); |
| 1572 |
j++; |
| 1573 |
} |
| 1574 |
if (!PyUnicode_Check(keyword)) { |
| 1575 |
PyErr_SetString(PyExc_TypeError, |
| 1576 |
"keywords must be strings"); |
| 1577 |
return; |
| 1578 |
} |
| 1579 |
|
| 1580 |
int match = PySequence_Contains(kwtuple, keyword); |
| 1581 |
if (match <= 0) { |
| 1582 |
if (!match) { |
| 1583 |
PyErr_Format(PyExc_TypeError, |
| 1584 |
"'%S' is an invalid keyword " |
| 1585 |
"argument for %.200s%s", |
| 1586 |
keyword, |
| 1587 |
(fname == NULL) ? "this function" : fname, |
| 1588 |
(fname == NULL) ? "" : "()"); |
| 1589 |
} |
| 1590 |
return; |
| 1591 |
} |
| 1592 |
} |
| 1593 |
|
| 1594 |
|
| 1595 |
PyErr_Format(PyExc_TypeError, |
| 1596 |
"invalid keyword argument for %.200s%s", |
| 1597 |
(fname == NULL) ? "this function" : fname, |
| 1598 |
(fname == NULL) ? "" : "()"); |
| 1599 |
} |
| 1600 |
|
| 1601 |
int |
| 1602 |
PyArg_ValidateKeywordArguments(PyObject *kwargs) |
| 1603 |
{ |
| 1604 |
if (!PyDict_Check(kwargs)) { |
| 1605 |
PyErr_BadInternalCall(); |
| 1606 |
return 0; |
| 1607 |
} |
| 1608 |
if (!_PyDict_HasOnlyStringKeys(kwargs)) { |
| 1609 |
PyErr_SetString(PyExc_TypeError, |
| 1610 |
"keywords must be strings"); |
| 1611 |
return 0; |
| 1612 |
} |
| 1613 |
return 1; |
| 1614 |
} |
| 1615 |
|
| 1616 |
#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') |
| 1617 |
|
| 1618 |
static int |
| 1619 |
vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, |
| 1620 |
char **kwlist, va_list *p_va, int flags) |
| 1621 |
{ |
| 1622 |
char msgbuf[512]; |
| 1623 |
int levels[32]; |
| 1624 |
const char *fname, *msg, *custom_msg; |
| 1625 |
int min = INT_MAX; |
| 1626 |
int max = INT_MAX; |
| 1627 |
int i, pos, len; |
| 1628 |
int skip = 0; |
| 1629 |
Py_ssize_t nargs, nkwargs; |
| 1630 |
PyObject *current_arg; |
| 1631 |
freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
| 1632 |
freelist_t freelist; |
| 1633 |
|
| 1634 |
freelist.entries = static_entries; |
| 1635 |
freelist.first_available = 0; |
| 1636 |
freelist.entries_malloced = 0; |
| 1637 |
|
| 1638 |
assert(args != NULL && PyTuple_Check(args)); |
| 1639 |
assert(kwargs == NULL || PyDict_Check(kwargs)); |
| 1640 |
assert(format != NULL); |
| 1641 |
assert(kwlist != NULL); |
| 1642 |
assert(p_va != NULL); |
| 1643 |
|
| 1644 |
|
| 1645 |
fname = strchr(format, ':'); |
| 1646 |
if (fname) { |
| 1647 |
fname++; |
| 1648 |
custom_msg = NULL; |
| 1649 |
} |
| 1650 |
else { |
| 1651 |
custom_msg = strchr(format,';'); |
| 1652 |
if (custom_msg) |
| 1653 |
custom_msg++; |
| 1654 |
} |
| 1655 |
|
| 1656 |
|
| 1657 |
for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) { |
| 1658 |
} |
| 1659 |
|
| 1660 |
for (len = pos; kwlist[len]; len++) { |
| 1661 |
if (!*kwlist[len]) { |
| 1662 |
PyErr_SetString(PyExc_SystemError, |
| 1663 |
"Empty keyword parameter name"); |
| 1664 |
return cleanreturn(0, &freelist); |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
if (len > STATIC_FREELIST_ENTRIES) { |
| 1669 |
freelist.entries = PyMem_NEW(freelistentry_t, len); |
| 1670 |
if (freelist.entries == NULL) { |
| 1671 |
PyErr_NoMemory(); |
| 1672 |
return 0; |
| 1673 |
} |
| 1674 |
freelist.entries_malloced = 1; |
| 1675 |
} |
| 1676 |
|
| 1677 |
nargs = PyTuple_GET_SIZE(args); |
| 1678 |
nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs); |
| 1679 |
if (nargs + nkwargs > len) { |
| 1680 |
|
| 1681 |
|
| 1682 |
PyErr_Format(PyExc_TypeError, |
| 1683 |
"%.200s%s takes at most %d %sargument%s (%zd given)", |
| 1684 |
(fname == NULL) ? "function" : fname, |
| 1685 |
(fname == NULL) ? "" : "()", |
| 1686 |
len, |
| 1687 |
(nargs == 0) ? "keyword " : "", |
| 1688 |
(len == 1) ? "" : "s", |
| 1689 |
nargs + nkwargs); |
| 1690 |
return cleanreturn(0, &freelist); |
| 1691 |
} |
| 1692 |
|
| 1693 |
|
| 1694 |
for (i = 0; i < len; i++) { |
| 1695 |
if (*format == '|') { |
| 1696 |
if (min != INT_MAX) { |
| 1697 |
PyErr_SetString(PyExc_SystemError, |
| 1698 |
"Invalid format string (| specified twice)"); |
| 1699 |
return cleanreturn(0, &freelist); |
| 1700 |
} |
| 1701 |
|
| 1702 |
min = i; |
| 1703 |
format++; |
| 1704 |
|
| 1705 |
if (max != INT_MAX) { |
| 1706 |
PyErr_SetString(PyExc_SystemError, |
| 1707 |
"Invalid format string ($ before |)"); |
| 1708 |
return cleanreturn(0, &freelist); |
| 1709 |
} |
| 1710 |
} |
| 1711 |
if (*format == '$') { |
| 1712 |
if (max != INT_MAX) { |
| 1713 |
PyErr_SetString(PyExc_SystemError, |
| 1714 |
"Invalid format string ($ specified twice)"); |
| 1715 |
return cleanreturn(0, &freelist); |
| 1716 |
} |
| 1717 |
|
| 1718 |
max = i; |
| 1719 |
format++; |
| 1720 |
|
| 1721 |
if (max < pos) { |
| 1722 |
PyErr_SetString(PyExc_SystemError, |
| 1723 |
"Empty parameter name after $"); |
| 1724 |
return cleanreturn(0, &freelist); |
| 1725 |
} |
| 1726 |
if (skip) { |
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
break; |
| 1731 |
} |
| 1732 |
if (max < nargs) { |
| 1733 |
if (max == 0) { |
| 1734 |
PyErr_Format(PyExc_TypeError, |
| 1735 |
"%.200s%s takes no positional arguments", |
| 1736 |
(fname == NULL) ? "function" : fname, |
| 1737 |
(fname == NULL) ? "" : "()"); |
| 1738 |
} |
| 1739 |
else { |
| 1740 |
PyErr_Format(PyExc_TypeError, |
| 1741 |
"%.200s%s takes %s %d positional argument%s" |
| 1742 |
" (%zd given)", |
| 1743 |
(fname == NULL) ? "function" : fname, |
| 1744 |
(fname == NULL) ? "" : "()", |
| 1745 |
(min != INT_MAX) ? "at most" : "exactly", |
| 1746 |
max, |
| 1747 |
max == 1 ? "" : "s", |
| 1748 |
nargs); |
| 1749 |
} |
| 1750 |
return cleanreturn(0, &freelist); |
| 1751 |
} |
| 1752 |
} |
| 1753 |
if (IS_END_OF_FORMAT(*format)) { |
| 1754 |
PyErr_Format(PyExc_SystemError, |
| 1755 |
"More keyword list entries (%d) than " |
| 1756 |
"format specifiers (%d)", len, i); |
| 1757 |
return cleanreturn(0, &freelist); |
| 1758 |
} |
| 1759 |
if (!skip) { |
| 1760 |
if (i < nargs) { |
| 1761 |
current_arg = PyTuple_GET_ITEM(args, i); |
| 1762 |
} |
| 1763 |
else if (nkwargs && i >= pos) { |
| 1764 |
current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]); |
| 1765 |
if (current_arg) { |
| 1766 |
--nkwargs; |
| 1767 |
} |
| 1768 |
else if (PyErr_Occurred()) { |
| 1769 |
return cleanreturn(0, &freelist); |
| 1770 |
} |
| 1771 |
} |
| 1772 |
else { |
| 1773 |
current_arg = NULL; |
| 1774 |
} |
| 1775 |
|
| 1776 |
if (current_arg) { |
| 1777 |
msg = convertitem(current_arg, &format, p_va, flags, |
| 1778 |
levels, msgbuf, sizeof(msgbuf), &freelist); |
| 1779 |
if (msg) { |
| 1780 |
seterror(i+1, msg, levels, fname, custom_msg); |
| 1781 |
return cleanreturn(0, &freelist); |
| 1782 |
} |
| 1783 |
continue; |
| 1784 |
} |
| 1785 |
|
| 1786 |
if (i < min) { |
| 1787 |
if (i < pos) { |
| 1788 |
assert (min == INT_MAX); |
| 1789 |
assert (max == INT_MAX); |
| 1790 |
skip = 1; |
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
} |
| 1796 |
else { |
| 1797 |
PyErr_Format(PyExc_TypeError, "%.200s%s missing required " |
| 1798 |
"argument '%s' (pos %d)", |
| 1799 |
(fname == NULL) ? "function" : fname, |
| 1800 |
(fname == NULL) ? "" : "()", |
| 1801 |
kwlist[i], i+1); |
| 1802 |
return cleanreturn(0, &freelist); |
| 1803 |
} |
| 1804 |
} |
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
if (!nkwargs && !skip) { |
| 1810 |
return cleanreturn(1, &freelist); |
| 1811 |
} |
| 1812 |
} |
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
msg = skipitem(&format, p_va, flags); |
| 1817 |
if (msg) { |
| 1818 |
PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, |
| 1819 |
format); |
| 1820 |
return cleanreturn(0, &freelist); |
| 1821 |
} |
| 1822 |
} |
| 1823 |
|
| 1824 |
if (skip) { |
| 1825 |
PyErr_Format(PyExc_TypeError, |
| 1826 |
"%.200s%s takes %s %d positional argument%s" |
| 1827 |
" (%zd given)", |
| 1828 |
(fname == NULL) ? "function" : fname, |
| 1829 |
(fname == NULL) ? "" : "()", |
| 1830 |
(Py_MIN(pos, min) < i) ? "at least" : "exactly", |
| 1831 |
Py_MIN(pos, min), |
| 1832 |
Py_MIN(pos, min) == 1 ? "" : "s", |
| 1833 |
nargs); |
| 1834 |
return cleanreturn(0, &freelist); |
| 1835 |
} |
| 1836 |
|
| 1837 |
if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { |
| 1838 |
PyErr_Format(PyExc_SystemError, |
| 1839 |
"more argument specifiers than keyword list entries " |
| 1840 |
"(remaining format:'%s')", format); |
| 1841 |
return cleanreturn(0, &freelist); |
| 1842 |
} |
| 1843 |
|
| 1844 |
if (nkwargs > 0) { |
| 1845 |
PyObject *key; |
| 1846 |
Py_ssize_t j; |
| 1847 |
|
| 1848 |
for (i = pos; i < nargs; i++) { |
| 1849 |
current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]); |
| 1850 |
if (current_arg) { |
| 1851 |
|
| 1852 |
PyErr_Format(PyExc_TypeError, |
| 1853 |
"argument for %.200s%s given by name ('%s') " |
| 1854 |
"and position (%d)", |
| 1855 |
(fname == NULL) ? "function" : fname, |
| 1856 |
(fname == NULL) ? "" : "()", |
| 1857 |
kwlist[i], i+1); |
| 1858 |
return cleanreturn(0, &freelist); |
| 1859 |
} |
| 1860 |
else if (PyErr_Occurred()) { |
| 1861 |
return cleanreturn(0, &freelist); |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
j = 0; |
| 1866 |
while (PyDict_Next(kwargs, &j, &key, NULL)) { |
| 1867 |
int match = 0; |
| 1868 |
if (!PyUnicode_Check(key)) { |
| 1869 |
PyErr_SetString(PyExc_TypeError, |
| 1870 |
"keywords must be strings"); |
| 1871 |
return cleanreturn(0, &freelist); |
| 1872 |
} |
| 1873 |
for (i = pos; i < len; i++) { |
| 1874 |
if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) { |
| 1875 |
match = 1; |
| 1876 |
break; |
| 1877 |
} |
| 1878 |
} |
| 1879 |
if (!match) { |
| 1880 |
PyErr_Format(PyExc_TypeError, |
| 1881 |
"'%U' is an invalid keyword " |
| 1882 |
"argument for %.200s%s", |
| 1883 |
key, |
| 1884 |
(fname == NULL) ? "this function" : fname, |
| 1885 |
(fname == NULL) ? "" : "()"); |
| 1886 |
return cleanreturn(0, &freelist); |
| 1887 |
} |
| 1888 |
} |
| 1889 |
|
| 1890 |
|
| 1891 |
PyErr_Format(PyExc_TypeError, |
| 1892 |
"invalid keyword argument for %.200s%s", |
| 1893 |
(fname == NULL) ? "this function" : fname, |
| 1894 |
(fname == NULL) ? "" : "()"); |
| 1895 |
return cleanreturn(0, &freelist); |
| 1896 |
} |
| 1897 |
|
| 1898 |
return cleanreturn(1, &freelist); |
| 1899 |
} |
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
static struct _PyArg_Parser *static_arg_parsers = NULL; |
| 1904 |
|
| 1905 |
static int |
| 1906 |
parser_init(struct _PyArg_Parser *parser) |
| 1907 |
{ |
| 1908 |
const char * const *keywords; |
| 1909 |
const char *format, *msg; |
| 1910 |
int i, len, min, max, nkw; |
| 1911 |
PyObject *kwtuple; |
| 1912 |
|
| 1913 |
assert(parser->keywords != NULL); |
| 1914 |
if (parser->kwtuple != NULL) { |
| 1915 |
return 1; |
| 1916 |
} |
| 1917 |
|
| 1918 |
keywords = parser->keywords; |
| 1919 |
|
| 1920 |
for (i = 0; keywords[i] && !*keywords[i]; i++) { |
| 1921 |
} |
| 1922 |
parser->pos = i; |
| 1923 |
|
| 1924 |
for (; keywords[i]; i++) { |
| 1925 |
if (!*keywords[i]) { |
| 1926 |
PyErr_SetString(PyExc_SystemError, |
| 1927 |
"Empty keyword parameter name"); |
| 1928 |
return 0; |
| 1929 |
} |
| 1930 |
} |
| 1931 |
len = i; |
| 1932 |
|
| 1933 |
format = parser->format; |
| 1934 |
if (format) { |
| 1935 |
|
| 1936 |
parser->fname = strchr(parser->format, ':'); |
| 1937 |
if (parser->fname) { |
| 1938 |
parser->fname++; |
| 1939 |
parser->custom_msg = NULL; |
| 1940 |
} |
| 1941 |
else { |
| 1942 |
parser->custom_msg = strchr(parser->format,';'); |
| 1943 |
if (parser->custom_msg) |
| 1944 |
parser->custom_msg++; |
| 1945 |
} |
| 1946 |
|
| 1947 |
min = max = INT_MAX; |
| 1948 |
for (i = 0; i < len; i++) { |
| 1949 |
if (*format == '|') { |
| 1950 |
if (min != INT_MAX) { |
| 1951 |
PyErr_SetString(PyExc_SystemError, |
| 1952 |
"Invalid format string (| specified twice)"); |
| 1953 |
return 0; |
| 1954 |
} |
| 1955 |
if (max != INT_MAX) { |
| 1956 |
PyErr_SetString(PyExc_SystemError, |
| 1957 |
"Invalid format string ($ before |)"); |
| 1958 |
return 0; |
| 1959 |
} |
| 1960 |
min = i; |
| 1961 |
format++; |
| 1962 |
} |
| 1963 |
if (*format == '$') { |
| 1964 |
if (max != INT_MAX) { |
| 1965 |
PyErr_SetString(PyExc_SystemError, |
| 1966 |
"Invalid format string ($ specified twice)"); |
| 1967 |
return 0; |
| 1968 |
} |
| 1969 |
if (i < parser->pos) { |
| 1970 |
PyErr_SetString(PyExc_SystemError, |
| 1971 |
"Empty parameter name after $"); |
| 1972 |
return 0; |
| 1973 |
} |
| 1974 |
max = i; |
| 1975 |
format++; |
| 1976 |
} |
| 1977 |
if (IS_END_OF_FORMAT(*format)) { |
| 1978 |
PyErr_Format(PyExc_SystemError, |
| 1979 |
"More keyword list entries (%d) than " |
| 1980 |
"format specifiers (%d)", len, i); |
| 1981 |
return 0; |
| 1982 |
} |
| 1983 |
|
| 1984 |
msg = skipitem(&format, NULL, 0); |
| 1985 |
if (msg) { |
| 1986 |
PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, |
| 1987 |
format); |
| 1988 |
return 0; |
| 1989 |
} |
| 1990 |
} |
| 1991 |
parser->min = Py_MIN(min, len); |
| 1992 |
parser->max = Py_MIN(max, len); |
| 1993 |
|
| 1994 |
if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { |
| 1995 |
PyErr_Format(PyExc_SystemError, |
| 1996 |
"more argument specifiers than keyword list entries " |
| 1997 |
"(remaining format:'%s')", format); |
| 1998 |
return 0; |
| 1999 |
} |
| 2000 |
} |
| 2001 |
|
| 2002 |
nkw = len - parser->pos; |
| 2003 |
kwtuple = PyTuple_New(nkw); |
| 2004 |
if (kwtuple == NULL) { |
| 2005 |
return 0; |
| 2006 |
} |
| 2007 |
keywords = parser->keywords + parser->pos; |
| 2008 |
for (i = 0; i < nkw; i++) { |
| 2009 |
PyObject *str = PyUnicode_FromString(keywords[i]); |
| 2010 |
if (str == NULL) { |
| 2011 |
Py_DECREF(kwtuple); |
| 2012 |
return 0; |
| 2013 |
} |
| 2014 |
PyUnicode_InternInPlace(&str); |
| 2015 |
PyTuple_SET_ITEM(kwtuple, i, str); |
| 2016 |
} |
| 2017 |
parser->kwtuple = kwtuple; |
| 2018 |
|
| 2019 |
assert(parser->next == NULL); |
| 2020 |
parser->next = static_arg_parsers; |
| 2021 |
static_arg_parsers = parser; |
| 2022 |
return 1; |
| 2023 |
} |
| 2024 |
|
| 2025 |
static void |
| 2026 |
parser_clear(struct _PyArg_Parser *parser) |
| 2027 |
{ |
| 2028 |
Py_CLEAR(parser->kwtuple); |
| 2029 |
} |
| 2030 |
|
| 2031 |
static PyObject* |
| 2032 |
find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key) |
| 2033 |
{ |
| 2034 |
Py_ssize_t i, nkwargs; |
| 2035 |
|
| 2036 |
nkwargs = PyTuple_GET_SIZE(kwnames); |
| 2037 |
for (i = 0; i < nkwargs; i++) { |
| 2038 |
PyObject *kwname = PyTuple_GET_ITEM(kwnames, i); |
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
if (kwname == key) { |
| 2043 |
return kwstack[i]; |
| 2044 |
} |
| 2045 |
} |
| 2046 |
|
| 2047 |
for (i = 0; i < nkwargs; i++) { |
| 2048 |
PyObject *kwname = PyTuple_GET_ITEM(kwnames, i); |
| 2049 |
assert(PyUnicode_Check(kwname)); |
| 2050 |
if (_PyUnicode_EQ(kwname, key)) { |
| 2051 |
return kwstack[i]; |
| 2052 |
} |
| 2053 |
} |
| 2054 |
return NULL; |
| 2055 |
} |
| 2056 |
|
| 2057 |
static int |
| 2058 |
vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, |
| 2059 |
PyObject *kwargs, PyObject *kwnames, |
| 2060 |
struct _PyArg_Parser *parser, |
| 2061 |
va_list *p_va, int flags) |
| 2062 |
{ |
| 2063 |
PyObject *kwtuple; |
| 2064 |
char msgbuf[512]; |
| 2065 |
int levels[32]; |
| 2066 |
const char *format; |
| 2067 |
const char *msg; |
| 2068 |
PyObject *keyword; |
| 2069 |
int i, pos, len; |
| 2070 |
Py_ssize_t nkwargs; |
| 2071 |
PyObject *current_arg; |
| 2072 |
freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
| 2073 |
freelist_t freelist; |
| 2074 |
PyObject *const *kwstack = NULL; |
| 2075 |
|
| 2076 |
freelist.entries = static_entries; |
| 2077 |
freelist.first_available = 0; |
| 2078 |
freelist.entries_malloced = 0; |
| 2079 |
|
| 2080 |
assert(kwargs == NULL || PyDict_Check(kwargs)); |
| 2081 |
assert(kwargs == NULL || kwnames == NULL); |
| 2082 |
assert(p_va != NULL); |
| 2083 |
|
| 2084 |
if (parser == NULL) { |
| 2085 |
PyErr_BadInternalCall(); |
| 2086 |
return 0; |
| 2087 |
} |
| 2088 |
|
| 2089 |
if (kwnames != NULL && !PyTuple_Check(kwnames)) { |
| 2090 |
PyErr_BadInternalCall(); |
| 2091 |
return 0; |
| 2092 |
} |
| 2093 |
|
| 2094 |
if (!parser_init(parser)) { |
| 2095 |
return 0; |
| 2096 |
} |
| 2097 |
|
| 2098 |
kwtuple = parser->kwtuple; |
| 2099 |
pos = parser->pos; |
| 2100 |
len = pos + (int)PyTuple_GET_SIZE(kwtuple); |
| 2101 |
|
| 2102 |
if (len > STATIC_FREELIST_ENTRIES) { |
| 2103 |
freelist.entries = PyMem_NEW(freelistentry_t, len); |
| 2104 |
if (freelist.entries == NULL) { |
| 2105 |
PyErr_NoMemory(); |
| 2106 |
return 0; |
| 2107 |
} |
| 2108 |
freelist.entries_malloced = 1; |
| 2109 |
} |
| 2110 |
|
| 2111 |
if (kwargs != NULL) { |
| 2112 |
nkwargs = PyDict_GET_SIZE(kwargs); |
| 2113 |
} |
| 2114 |
else if (kwnames != NULL) { |
| 2115 |
nkwargs = PyTuple_GET_SIZE(kwnames); |
| 2116 |
kwstack = args + nargs; |
| 2117 |
} |
| 2118 |
else { |
| 2119 |
nkwargs = 0; |
| 2120 |
} |
| 2121 |
if (nargs + nkwargs > len) { |
| 2122 |
|
| 2123 |
|
| 2124 |
PyErr_Format(PyExc_TypeError, |
| 2125 |
"%.200s%s takes at most %d %sargument%s (%zd given)", |
| 2126 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2127 |
(parser->fname == NULL) ? "" : "()", |
| 2128 |
len, |
| 2129 |
(nargs == 0) ? "keyword " : "", |
| 2130 |
(len == 1) ? "" : "s", |
| 2131 |
nargs + nkwargs); |
| 2132 |
return cleanreturn(0, &freelist); |
| 2133 |
} |
| 2134 |
if (parser->max < nargs) { |
| 2135 |
if (parser->max == 0) { |
| 2136 |
PyErr_Format(PyExc_TypeError, |
| 2137 |
"%.200s%s takes no positional arguments", |
| 2138 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2139 |
(parser->fname == NULL) ? "" : "()"); |
| 2140 |
} |
| 2141 |
else { |
| 2142 |
PyErr_Format(PyExc_TypeError, |
| 2143 |
"%.200s%s takes %s %d positional argument%s (%zd given)", |
| 2144 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2145 |
(parser->fname == NULL) ? "" : "()", |
| 2146 |
(parser->min < parser->max) ? "at most" : "exactly", |
| 2147 |
parser->max, |
| 2148 |
parser->max == 1 ? "" : "s", |
| 2149 |
nargs); |
| 2150 |
} |
| 2151 |
return cleanreturn(0, &freelist); |
| 2152 |
} |
| 2153 |
|
| 2154 |
format = parser->format; |
| 2155 |
|
| 2156 |
for (i = 0; i < len; i++) { |
| 2157 |
if (*format == '|') { |
| 2158 |
format++; |
| 2159 |
} |
| 2160 |
if (*format == '$') { |
| 2161 |
format++; |
| 2162 |
} |
| 2163 |
assert(!IS_END_OF_FORMAT(*format)); |
| 2164 |
|
| 2165 |
if (i < nargs) { |
| 2166 |
current_arg = args[i]; |
| 2167 |
} |
| 2168 |
else if (nkwargs && i >= pos) { |
| 2169 |
keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
| 2170 |
if (kwargs != NULL) { |
| 2171 |
current_arg = PyDict_GetItemWithError(kwargs, keyword); |
| 2172 |
if (!current_arg && PyErr_Occurred()) { |
| 2173 |
return cleanreturn(0, &freelist); |
| 2174 |
} |
| 2175 |
} |
| 2176 |
else { |
| 2177 |
current_arg = find_keyword(kwnames, kwstack, keyword); |
| 2178 |
} |
| 2179 |
if (current_arg) { |
| 2180 |
--nkwargs; |
| 2181 |
} |
| 2182 |
} |
| 2183 |
else { |
| 2184 |
current_arg = NULL; |
| 2185 |
} |
| 2186 |
|
| 2187 |
if (current_arg) { |
| 2188 |
msg = convertitem(current_arg, &format, p_va, flags, |
| 2189 |
levels, msgbuf, sizeof(msgbuf), &freelist); |
| 2190 |
if (msg) { |
| 2191 |
seterror(i+1, msg, levels, parser->fname, parser->custom_msg); |
| 2192 |
return cleanreturn(0, &freelist); |
| 2193 |
} |
| 2194 |
continue; |
| 2195 |
} |
| 2196 |
|
| 2197 |
if (i < parser->min) { |
| 2198 |
|
| 2199 |
if (i < pos) { |
| 2200 |
Py_ssize_t min = Py_MIN(pos, parser->min); |
| 2201 |
PyErr_Format(PyExc_TypeError, |
| 2202 |
"%.200s%s takes %s %d positional argument%s" |
| 2203 |
" (%zd given)", |
| 2204 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2205 |
(parser->fname == NULL) ? "" : "()", |
| 2206 |
min < parser->max ? "at least" : "exactly", |
| 2207 |
min, |
| 2208 |
min == 1 ? "" : "s", |
| 2209 |
nargs); |
| 2210 |
} |
| 2211 |
else { |
| 2212 |
keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
| 2213 |
PyErr_Format(PyExc_TypeError, "%.200s%s missing required " |
| 2214 |
"argument '%U' (pos %d)", |
| 2215 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2216 |
(parser->fname == NULL) ? "" : "()", |
| 2217 |
keyword, i+1); |
| 2218 |
} |
| 2219 |
return cleanreturn(0, &freelist); |
| 2220 |
} |
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
if (!nkwargs) { |
| 2226 |
return cleanreturn(1, &freelist); |
| 2227 |
} |
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
msg = skipitem(&format, p_va, flags); |
| 2232 |
assert(msg == NULL); |
| 2233 |
} |
| 2234 |
|
| 2235 |
assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$')); |
| 2236 |
|
| 2237 |
if (nkwargs > 0) { |
| 2238 |
|
| 2239 |
for (i = pos; i < nargs; i++) { |
| 2240 |
keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
| 2241 |
if (kwargs != NULL) { |
| 2242 |
current_arg = PyDict_GetItemWithError(kwargs, keyword); |
| 2243 |
if (!current_arg && PyErr_Occurred()) { |
| 2244 |
return cleanreturn(0, &freelist); |
| 2245 |
} |
| 2246 |
} |
| 2247 |
else { |
| 2248 |
current_arg = find_keyword(kwnames, kwstack, keyword); |
| 2249 |
} |
| 2250 |
if (current_arg) { |
| 2251 |
|
| 2252 |
PyErr_Format(PyExc_TypeError, |
| 2253 |
"argument for %.200s%s given by name ('%U') " |
| 2254 |
"and position (%d)", |
| 2255 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2256 |
(parser->fname == NULL) ? "" : "()", |
| 2257 |
keyword, i+1); |
| 2258 |
return cleanreturn(0, &freelist); |
| 2259 |
} |
| 2260 |
} |
| 2261 |
|
| 2262 |
error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname); |
| 2263 |
return cleanreturn(0, &freelist); |
| 2264 |
} |
| 2265 |
|
| 2266 |
return cleanreturn(1, &freelist); |
| 2267 |
} |
| 2268 |
|
| 2269 |
static int |
| 2270 |
vgetargskeywordsfast(PyObject *args, PyObject *keywords, |
| 2271 |
struct _PyArg_Parser *parser, va_list *p_va, int flags) |
| 2272 |
{ |
| 2273 |
PyObject **stack; |
| 2274 |
Py_ssize_t nargs; |
| 2275 |
|
| 2276 |
if (args == NULL |
| 2277 |
|| !PyTuple_Check(args) |
| 2278 |
|| (keywords != NULL && !PyDict_Check(keywords))) |
| 2279 |
{ |
| 2280 |
PyErr_BadInternalCall(); |
| 2281 |
return 0; |
| 2282 |
} |
| 2283 |
|
| 2284 |
stack = _PyTuple_ITEMS(args); |
| 2285 |
nargs = PyTuple_GET_SIZE(args); |
| 2286 |
return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL, |
| 2287 |
parser, p_va, flags); |
| 2288 |
} |
| 2289 |
|
| 2290 |
|
| 2291 |
#undef _PyArg_UnpackKeywords |
| 2292 |
|
| 2293 |
PyObject * const * |
| 2294 |
_PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs, |
| 2295 |
PyObject *kwargs, PyObject *kwnames, |
| 2296 |
struct _PyArg_Parser *parser, |
| 2297 |
int minpos, int maxpos, int minkw, |
| 2298 |
PyObject **buf) |
| 2299 |
{ |
| 2300 |
PyObject *kwtuple; |
| 2301 |
PyObject *keyword; |
| 2302 |
int i, posonly, minposonly, maxargs; |
| 2303 |
int reqlimit = minkw ? maxpos + minkw : minpos; |
| 2304 |
Py_ssize_t nkwargs; |
| 2305 |
PyObject *current_arg; |
| 2306 |
PyObject * const *kwstack = NULL; |
| 2307 |
|
| 2308 |
assert(kwargs == NULL || PyDict_Check(kwargs)); |
| 2309 |
assert(kwargs == NULL || kwnames == NULL); |
| 2310 |
|
| 2311 |
if (parser == NULL) { |
| 2312 |
PyErr_BadInternalCall(); |
| 2313 |
return NULL; |
| 2314 |
} |
| 2315 |
|
| 2316 |
if (kwnames != NULL && !PyTuple_Check(kwnames)) { |
| 2317 |
PyErr_BadInternalCall(); |
| 2318 |
return NULL; |
| 2319 |
} |
| 2320 |
|
| 2321 |
if (args == NULL && nargs == 0) { |
| 2322 |
args = buf; |
| 2323 |
} |
| 2324 |
|
| 2325 |
if (!parser_init(parser)) { |
| 2326 |
return NULL; |
| 2327 |
} |
| 2328 |
|
| 2329 |
kwtuple = parser->kwtuple; |
| 2330 |
posonly = parser->pos; |
| 2331 |
minposonly = Py_MIN(posonly, minpos); |
| 2332 |
maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple); |
| 2333 |
|
| 2334 |
if (kwargs != NULL) { |
| 2335 |
nkwargs = PyDict_GET_SIZE(kwargs); |
| 2336 |
} |
| 2337 |
else if (kwnames != NULL) { |
| 2338 |
nkwargs = PyTuple_GET_SIZE(kwnames); |
| 2339 |
kwstack = args + nargs; |
| 2340 |
} |
| 2341 |
else { |
| 2342 |
nkwargs = 0; |
| 2343 |
} |
| 2344 |
if (nkwargs == 0 && minkw == 0 && minpos <= nargs && nargs <= maxpos) { |
| 2345 |
|
| 2346 |
return args; |
| 2347 |
} |
| 2348 |
if (nargs + nkwargs > maxargs) { |
| 2349 |
|
| 2350 |
|
| 2351 |
PyErr_Format(PyExc_TypeError, |
| 2352 |
"%.200s%s takes at most %d %sargument%s (%zd given)", |
| 2353 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2354 |
(parser->fname == NULL) ? "" : "()", |
| 2355 |
maxargs, |
| 2356 |
(nargs == 0) ? "keyword " : "", |
| 2357 |
(maxargs == 1) ? "" : "s", |
| 2358 |
nargs + nkwargs); |
| 2359 |
return NULL; |
| 2360 |
} |
| 2361 |
if (nargs > maxpos) { |
| 2362 |
if (maxpos == 0) { |
| 2363 |
PyErr_Format(PyExc_TypeError, |
| 2364 |
"%.200s%s takes no positional arguments", |
| 2365 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2366 |
(parser->fname == NULL) ? "" : "()"); |
| 2367 |
} |
| 2368 |
else { |
| 2369 |
PyErr_Format(PyExc_TypeError, |
| 2370 |
"%.200s%s takes %s %d positional argument%s (%zd given)", |
| 2371 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2372 |
(parser->fname == NULL) ? "" : "()", |
| 2373 |
(minpos < maxpos) ? "at most" : "exactly", |
| 2374 |
maxpos, |
| 2375 |
(maxpos == 1) ? "" : "s", |
| 2376 |
nargs); |
| 2377 |
} |
| 2378 |
return NULL; |
| 2379 |
} |
| 2380 |
if (nargs < minposonly) { |
| 2381 |
PyErr_Format(PyExc_TypeError, |
| 2382 |
"%.200s%s takes %s %d positional argument%s" |
| 2383 |
" (%zd given)", |
| 2384 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2385 |
(parser->fname == NULL) ? "" : "()", |
| 2386 |
minposonly < maxpos ? "at least" : "exactly", |
| 2387 |
minposonly, |
| 2388 |
minposonly == 1 ? "" : "s", |
| 2389 |
nargs); |
| 2390 |
return NULL; |
| 2391 |
} |
| 2392 |
|
| 2393 |
|
| 2394 |
for (i = 0; i < nargs; i++) { |
| 2395 |
buf[i] = args[i]; |
| 2396 |
} |
| 2397 |
|
| 2398 |
|
| 2399 |
for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) { |
| 2400 |
if (nkwargs) { |
| 2401 |
keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); |
| 2402 |
if (kwargs != NULL) { |
| 2403 |
current_arg = PyDict_GetItemWithError(kwargs, keyword); |
| 2404 |
if (!current_arg && PyErr_Occurred()) { |
| 2405 |
return NULL; |
| 2406 |
} |
| 2407 |
} |
| 2408 |
else { |
| 2409 |
current_arg = find_keyword(kwnames, kwstack, keyword); |
| 2410 |
} |
| 2411 |
} |
| 2412 |
else if (i >= reqlimit) { |
| 2413 |
break; |
| 2414 |
} |
| 2415 |
else { |
| 2416 |
current_arg = NULL; |
| 2417 |
} |
| 2418 |
|
| 2419 |
buf[i] = current_arg; |
| 2420 |
|
| 2421 |
if (current_arg) { |
| 2422 |
--nkwargs; |
| 2423 |
} |
| 2424 |
else if (i < minpos || (maxpos <= i && i < reqlimit)) { |
| 2425 |
|
| 2426 |
keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); |
| 2427 |
PyErr_Format(PyExc_TypeError, "%.200s%s missing required " |
| 2428 |
"argument '%U' (pos %d)", |
| 2429 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2430 |
(parser->fname == NULL) ? "" : "()", |
| 2431 |
keyword, i+1); |
| 2432 |
return NULL; |
| 2433 |
} |
| 2434 |
} |
| 2435 |
|
| 2436 |
if (nkwargs > 0) { |
| 2437 |
|
| 2438 |
for (i = posonly; i < nargs; i++) { |
| 2439 |
keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); |
| 2440 |
if (kwargs != NULL) { |
| 2441 |
current_arg = PyDict_GetItemWithError(kwargs, keyword); |
| 2442 |
if (!current_arg && PyErr_Occurred()) { |
| 2443 |
return NULL; |
| 2444 |
} |
| 2445 |
} |
| 2446 |
else { |
| 2447 |
current_arg = find_keyword(kwnames, kwstack, keyword); |
| 2448 |
} |
| 2449 |
if (current_arg) { |
| 2450 |
|
| 2451 |
PyErr_Format(PyExc_TypeError, |
| 2452 |
"argument for %.200s%s given by name ('%U') " |
| 2453 |
"and position (%d)", |
| 2454 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2455 |
(parser->fname == NULL) ? "" : "()", |
| 2456 |
keyword, i+1); |
| 2457 |
return NULL; |
| 2458 |
} |
| 2459 |
} |
| 2460 |
|
| 2461 |
error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname); |
| 2462 |
return NULL; |
| 2463 |
} |
| 2464 |
|
| 2465 |
return buf; |
| 2466 |
} |
| 2467 |
|
| 2468 |
PyObject * const * |
| 2469 |
_PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs, |
| 2470 |
PyObject *kwargs, PyObject *kwnames, |
| 2471 |
struct _PyArg_Parser *parser, |
| 2472 |
int minpos, int maxpos, int minkw, |
| 2473 |
int vararg, PyObject **buf) |
| 2474 |
{ |
| 2475 |
PyObject *kwtuple; |
| 2476 |
PyObject *keyword; |
| 2477 |
Py_ssize_t varargssize = 0; |
| 2478 |
int i, posonly, minposonly, maxargs; |
| 2479 |
int reqlimit = minkw ? maxpos + minkw : minpos; |
| 2480 |
Py_ssize_t nkwargs; |
| 2481 |
PyObject *current_arg; |
| 2482 |
PyObject * const *kwstack = NULL; |
| 2483 |
|
| 2484 |
assert(kwargs == NULL || PyDict_Check(kwargs)); |
| 2485 |
assert(kwargs == NULL || kwnames == NULL); |
| 2486 |
|
| 2487 |
if (parser == NULL) { |
| 2488 |
PyErr_BadInternalCall(); |
| 2489 |
return NULL; |
| 2490 |
} |
| 2491 |
|
| 2492 |
if (kwnames != NULL && !PyTuple_Check(kwnames)) { |
| 2493 |
PyErr_BadInternalCall(); |
| 2494 |
return NULL; |
| 2495 |
} |
| 2496 |
|
| 2497 |
if (args == NULL && nargs == 0) { |
| 2498 |
args = buf; |
| 2499 |
} |
| 2500 |
|
| 2501 |
if (!parser_init(parser)) { |
| 2502 |
return NULL; |
| 2503 |
} |
| 2504 |
|
| 2505 |
kwtuple = parser->kwtuple; |
| 2506 |
posonly = parser->pos; |
| 2507 |
minposonly = Py_MIN(posonly, minpos); |
| 2508 |
maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple); |
| 2509 |
if (kwargs != NULL) { |
| 2510 |
nkwargs = PyDict_GET_SIZE(kwargs); |
| 2511 |
} |
| 2512 |
else if (kwnames != NULL) { |
| 2513 |
nkwargs = PyTuple_GET_SIZE(kwnames); |
| 2514 |
kwstack = args + nargs; |
| 2515 |
} |
| 2516 |
else { |
| 2517 |
nkwargs = 0; |
| 2518 |
} |
| 2519 |
if (nargs < minposonly) { |
| 2520 |
PyErr_Format(PyExc_TypeError, |
| 2521 |
"%.200s%s takes %s %d positional argument%s" |
| 2522 |
" (%zd given)", |
| 2523 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2524 |
(parser->fname == NULL) ? "" : "()", |
| 2525 |
minposonly < maxpos ? "at least" : "exactly", |
| 2526 |
minposonly, |
| 2527 |
minposonly == 1 ? "" : "s", |
| 2528 |
nargs); |
| 2529 |
return NULL; |
| 2530 |
} |
| 2531 |
|
| 2532 |
|
| 2533 |
varargssize = nargs - maxpos; |
| 2534 |
if (varargssize < 0) { |
| 2535 |
varargssize = 0; |
| 2536 |
} |
| 2537 |
buf[vararg] = PyTuple_New(varargssize); |
| 2538 |
if (!buf[vararg]) { |
| 2539 |
return NULL; |
| 2540 |
} |
| 2541 |
|
| 2542 |
|
| 2543 |
for (i = 0; i < nargs; i++) { |
| 2544 |
if (i >= vararg) { |
| 2545 |
Py_INCREF(args[i]); |
| 2546 |
PyTuple_SET_ITEM(buf[vararg], i - vararg, args[i]); |
| 2547 |
continue; |
| 2548 |
} |
| 2549 |
else { |
| 2550 |
buf[i] = args[i]; |
| 2551 |
} |
| 2552 |
} |
| 2553 |
|
| 2554 |
|
| 2555 |
for (i = Py_MAX((int)nargs, posonly) - |
| 2556 |
Py_SAFE_DOWNCAST(varargssize, Py_ssize_t, int); i < maxargs; i++) { |
| 2557 |
if (nkwargs) { |
| 2558 |
keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); |
| 2559 |
if (kwargs != NULL) { |
| 2560 |
current_arg = PyDict_GetItemWithError(kwargs, keyword); |
| 2561 |
if (!current_arg && PyErr_Occurred()) { |
| 2562 |
goto exit; |
| 2563 |
} |
| 2564 |
} |
| 2565 |
else { |
| 2566 |
current_arg = find_keyword(kwnames, kwstack, keyword); |
| 2567 |
} |
| 2568 |
} |
| 2569 |
else { |
| 2570 |
current_arg = NULL; |
| 2571 |
} |
| 2572 |
|
| 2573 |
|
| 2574 |
|
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
|
| 2583 |
|
| 2584 |
|
| 2585 |
|
| 2586 |
if (nargs < vararg) { |
| 2587 |
buf[i] = current_arg; |
| 2588 |
} |
| 2589 |
else { |
| 2590 |
buf[i + 1] = current_arg; |
| 2591 |
} |
| 2592 |
|
| 2593 |
if (current_arg) { |
| 2594 |
--nkwargs; |
| 2595 |
} |
| 2596 |
else if (i < minpos || (maxpos <= i && i < reqlimit)) { |
| 2597 |
|
| 2598 |
keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); |
| 2599 |
PyErr_Format(PyExc_TypeError, "%.200s%s missing required " |
| 2600 |
"argument '%U' (pos %d)", |
| 2601 |
(parser->fname == NULL) ? "function" : parser->fname, |
| 2602 |
(parser->fname == NULL) ? "" : "()", |
| 2603 |
keyword, i+1); |
| 2604 |
goto exit; |
| 2605 |
} |
| 2606 |
} |
| 2607 |
|
| 2608 |
if (nkwargs > 0) { |
| 2609 |
error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname); |
| 2610 |
goto exit; |
| 2611 |
} |
| 2612 |
|
| 2613 |
return buf; |
| 2614 |
|
| 2615 |
exit: |
| 2616 |
Py_XDECREF(buf[vararg]); |
| 2617 |
return NULL; |
| 2618 |
} |
| 2619 |
|
| 2620 |
|
| 2621 |
static const char * |
| 2622 |
skipitem(const char **p_format, va_list *p_va, int flags) |
| 2623 |
{ |
| 2624 |
const char *format = *p_format; |
| 2625 |
char c = *format++; |
| 2626 |
|
| 2627 |
switch (c) { |
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
case 'b': |
| 2635 |
case 'B': |
| 2636 |
case 'h': |
| 2637 |
case 'H': |
| 2638 |
case 'i': |
| 2639 |
case 'I': |
| 2640 |
case 'l': |
| 2641 |
case 'k': |
| 2642 |
case 'L': |
| 2643 |
case 'K': |
| 2644 |
case 'n': |
| 2645 |
case 'f': |
| 2646 |
case 'd': |
| 2647 |
case 'D': |
| 2648 |
case 'c': |
| 2649 |
case 'C': |
| 2650 |
case 'p': |
| 2651 |
case 'S': |
| 2652 |
case 'Y': |
| 2653 |
case 'U': |
| 2654 |
{ |
| 2655 |
if (p_va != NULL) { |
| 2656 |
(void) va_arg(*p_va, void *); |
| 2657 |
} |
| 2658 |
break; |
| 2659 |
} |
| 2660 |
|
| 2661 |
|
| 2662 |
|
| 2663 |
case 'e': |
| 2664 |
{ |
| 2665 |
if (p_va != NULL) { |
| 2666 |
(void) va_arg(*p_va, const char *); |
| 2667 |
} |
| 2668 |
if (!(*format == 's' || *format == 't')) |
| 2669 |
|
| 2670 |
goto err; |
| 2671 |
format++; |
| 2672 |
} |
| 2673 |
|
| 2674 |
|
| 2675 |
case 's': |
| 2676 |
case 'z': |
| 2677 |
case 'y': |
| 2678 |
case 'u': |
| 2679 |
case 'Z': |
| 2680 |
case 'w': |
| 2681 |
{ |
| 2682 |
if (p_va != NULL) { |
| 2683 |
(void) va_arg(*p_va, char **); |
| 2684 |
} |
| 2685 |
if (*format == '#') { |
| 2686 |
if (p_va != NULL) { |
| 2687 |
if (!(flags & FLAG_SIZE_T)) { |
| 2688 |
return "PY_SSIZE_T_CLEAN macro must be defined for '#' formats"; |
| 2689 |
} |
| 2690 |
(void) va_arg(*p_va, Py_ssize_t *); |
| 2691 |
} |
| 2692 |
format++; |
| 2693 |
} else if ((c == 's' || c == 'z' || c == 'y' || c == 'w') |
| 2694 |
&& *format == '*') |
| 2695 |
{ |
| 2696 |
format++; |
| 2697 |
} |
| 2698 |
break; |
| 2699 |
} |
| 2700 |
|
| 2701 |
case 'O': |
| 2702 |
{ |
| 2703 |
if (*format == '!') { |
| 2704 |
format++; |
| 2705 |
if (p_va != NULL) { |
| 2706 |
(void) va_arg(*p_va, PyTypeObject*); |
| 2707 |
(void) va_arg(*p_va, PyObject **); |
| 2708 |
} |
| 2709 |
} |
| 2710 |
else if (*format == '&') { |
| 2711 |
typedef int (*converter)(PyObject *, void *); |
| 2712 |
if (p_va != NULL) { |
| 2713 |
(void) va_arg(*p_va, converter); |
| 2714 |
(void) va_arg(*p_va, void *); |
| 2715 |
} |
| 2716 |
format++; |
| 2717 |
} |
| 2718 |
else { |
| 2719 |
if (p_va != NULL) { |
| 2720 |
(void) va_arg(*p_va, PyObject **); |
| 2721 |
} |
| 2722 |
} |
| 2723 |
break; |
| 2724 |
} |
| 2725 |
|
| 2726 |
case '(': |
| 2727 |
{ |
| 2728 |
const char *msg; |
| 2729 |
for (;;) { |
| 2730 |
if (*format==')') |
| 2731 |
break; |
| 2732 |
if (IS_END_OF_FORMAT(*format)) |
| 2733 |
return "Unmatched left paren in format " |
| 2734 |
"string"; |
| 2735 |
msg = skipitem(&format, p_va, flags); |
| 2736 |
if (msg) |
| 2737 |
return msg; |
| 2738 |
} |
| 2739 |
format++; |
| 2740 |
break; |
| 2741 |
} |
| 2742 |
|
| 2743 |
case ')': |
| 2744 |
return "Unmatched right paren in format string"; |
| 2745 |
|
| 2746 |
default: |
| 2747 |
err: |
| 2748 |
return "impossible<bad format char>"; |
| 2749 |
|
| 2750 |
} |
| 2751 |
|
| 2752 |
*p_format = format; |
| 2753 |
return NULL; |
| 2754 |
} |
| 2755 |
|
| 2756 |
|
| 2757 |
#undef _PyArg_CheckPositional |
| 2758 |
|
| 2759 |
int |
| 2760 |
_PyArg_CheckPositional(const char *name, Py_ssize_t nargs, |
| 2761 |
Py_ssize_t min, Py_ssize_t max) |
| 2762 |
{ |
| 2763 |
assert(min >= 0); |
| 2764 |
assert(min <= max); |
| 2765 |
|
| 2766 |
if (nargs < min) { |
| 2767 |
if (name != NULL) |
| 2768 |
PyErr_Format( |
| 2769 |
PyExc_TypeError, |
| 2770 |
"%.200s expected %s%zd argument%s, got %zd", |
| 2771 |
name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); |
| 2772 |
else |
| 2773 |
PyErr_Format( |
| 2774 |
PyExc_TypeError, |
| 2775 |
"unpacked tuple should have %s%zd element%s," |
| 2776 |
" but has %zd", |
| 2777 |
(min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); |
| 2778 |
return 0; |
| 2779 |
} |
| 2780 |
|
| 2781 |
if (nargs == 0) { |
| 2782 |
return 1; |
| 2783 |
} |
| 2784 |
|
| 2785 |
if (nargs > max) { |
| 2786 |
if (name != NULL) |
| 2787 |
PyErr_Format( |
| 2788 |
PyExc_TypeError, |
| 2789 |
"%.200s expected %s%zd argument%s, got %zd", |
| 2790 |
name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); |
| 2791 |
else |
| 2792 |
PyErr_Format( |
| 2793 |
PyExc_TypeError, |
| 2794 |
"unpacked tuple should have %s%zd element%s," |
| 2795 |
" but has %zd", |
| 2796 |
(min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); |
| 2797 |
return 0; |
| 2798 |
} |
| 2799 |
|
| 2800 |
return 1; |
| 2801 |
} |
| 2802 |
|
| 2803 |
static int |
| 2804 |
unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name, |
| 2805 |
Py_ssize_t min, Py_ssize_t max, va_list vargs) |
| 2806 |
{ |
| 2807 |
Py_ssize_t i; |
| 2808 |
PyObject **o; |
| 2809 |
|
| 2810 |
if (!_PyArg_CheckPositional(name, nargs, min, max)) { |
| 2811 |
return 0; |
| 2812 |
} |
| 2813 |
|
| 2814 |
for (i = 0; i < nargs; i++) { |
| 2815 |
o = va_arg(vargs, PyObject **); |
| 2816 |
*o = args[i]; |
| 2817 |
} |
| 2818 |
return 1; |
| 2819 |
} |
| 2820 |
|
| 2821 |
int |
| 2822 |
PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) |
| 2823 |
{ |
| 2824 |
PyObject **stack; |
| 2825 |
Py_ssize_t nargs; |
| 2826 |
int retval; |
| 2827 |
va_list vargs; |
| 2828 |
|
| 2829 |
if (!PyTuple_Check(args)) { |
| 2830 |
PyErr_SetString(PyExc_SystemError, |
| 2831 |
"PyArg_UnpackTuple() argument list is not a tuple"); |
| 2832 |
return 0; |
| 2833 |
} |
| 2834 |
stack = _PyTuple_ITEMS(args); |
| 2835 |
nargs = PyTuple_GET_SIZE(args); |
| 2836 |
|
| 2837 |
#ifdef HAVE_STDARG_PROTOTYPES |
| 2838 |
va_start(vargs, max); |
| 2839 |
#else |
| 2840 |
va_start(vargs); |
| 2841 |
#endif |
| 2842 |
retval = unpack_stack(stack, nargs, name, min, max, vargs); |
| 2843 |
va_end(vargs); |
| 2844 |
return retval; |
| 2845 |
} |
| 2846 |
|
| 2847 |
int |
| 2848 |
_PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name, |
| 2849 |
Py_ssize_t min, Py_ssize_t max, ...) |
| 2850 |
{ |
| 2851 |
int retval; |
| 2852 |
va_list vargs; |
| 2853 |
|
| 2854 |
#ifdef HAVE_STDARG_PROTOTYPES |
| 2855 |
va_start(vargs, max); |
| 2856 |
#else |
| 2857 |
va_start(vargs); |
| 2858 |
#endif |
| 2859 |
retval = unpack_stack(args, nargs, name, min, max, vargs); |
| 2860 |
va_end(vargs); |
| 2861 |
return retval; |
| 2862 |
} |
| 2863 |
|
| 2864 |
|
| 2865 |
#undef _PyArg_NoKeywords |
| 2866 |
#undef _PyArg_NoKwnames |
| 2867 |
#undef _PyArg_NoPositional |
| 2868 |
|
| 2869 |
|
| 2870 |
|
| 2871 |
|
| 2872 |
|
| 2873 |
|
| 2874 |
int |
| 2875 |
_PyArg_NoKeywords(const char *funcname, PyObject *kwargs) |
| 2876 |
{ |
| 2877 |
if (kwargs == NULL) { |
| 2878 |
return 1; |
| 2879 |
} |
| 2880 |
if (!PyDict_CheckExact(kwargs)) { |
| 2881 |
PyErr_BadInternalCall(); |
| 2882 |
return 0; |
| 2883 |
} |
| 2884 |
if (PyDict_GET_SIZE(kwargs) == 0) { |
| 2885 |
return 1; |
| 2886 |
} |
| 2887 |
|
| 2888 |
PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
| 2889 |
funcname); |
| 2890 |
return 0; |
| 2891 |
} |
| 2892 |
|
| 2893 |
int |
| 2894 |
_PyArg_NoPositional(const char *funcname, PyObject *args) |
| 2895 |
{ |
| 2896 |
if (args == NULL) |
| 2897 |
return 1; |
| 2898 |
if (!PyTuple_CheckExact(args)) { |
| 2899 |
PyErr_BadInternalCall(); |
| 2900 |
return 0; |
| 2901 |
} |
| 2902 |
if (PyTuple_GET_SIZE(args) == 0) |
| 2903 |
return 1; |
| 2904 |
|
| 2905 |
PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments", |
| 2906 |
funcname); |
| 2907 |
return 0; |
| 2908 |
} |
| 2909 |
|
| 2910 |
int |
| 2911 |
_PyArg_NoKwnames(const char *funcname, PyObject *kwnames) |
| 2912 |
{ |
| 2913 |
if (kwnames == NULL) { |
| 2914 |
return 1; |
| 2915 |
} |
| 2916 |
|
| 2917 |
assert(PyTuple_CheckExact(kwnames)); |
| 2918 |
|
| 2919 |
if (PyTuple_GET_SIZE(kwnames) == 0) { |
| 2920 |
return 1; |
| 2921 |
} |
| 2922 |
|
| 2923 |
PyErr_Format(PyExc_TypeError, "%s() takes no keyword arguments", funcname); |
| 2924 |
return 0; |
| 2925 |
} |
| 2926 |
|
| 2927 |
void |
| 2928 |
_PyArg_Fini(void) |
| 2929 |
{ |
| 2930 |
struct _PyArg_Parser *tmp, *s = static_arg_parsers; |
| 2931 |
while (s) { |
| 2932 |
tmp = s->next; |
| 2933 |
s->next = NULL; |
| 2934 |
parser_clear(s); |
| 2935 |
s = tmp; |
| 2936 |
} |
| 2937 |
static_arg_parsers = NULL; |
| 2938 |
} |
| 2939 |
|
| 2940 |
#ifdef __cplusplus |
| 2941 |
}; |
| 2942 |
#endif |