| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
he |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
#include "hpy.h" |
| 139 |
#include <limits.h> |
| 140 |
#include <stdio.h> |
| 141 |
|
| 142 |
#define _BREAK_IF_OPTIONAL(current_arg) if (HPy_IsNull(current_arg)) break; |
| 143 |
#define _ERR_STRING_MAX_LENGTH 512 |
| 144 |
|
| 145 |
|
| 146 |
static const char * |
| 147 |
parse_err_fmt(const char *fmt, const char **err_fmt) |
| 148 |
{ |
| 149 |
const char *fmt1 = fmt; |
| 150 |
|
| 151 |
for (; *fmt1 != 0; fmt1++) { |
| 152 |
if (*fmt1 == ':' || *fmt1 == ';') { |
| 153 |
*err_fmt = fmt1; |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
return fmt1; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
static void |
| 162 |
set_error(HPyContext *ctx, HPy exc, const char *err_fmt, const char *msg) { |
| 163 |
char err_buf[_ERR_STRING_MAX_LENGTH]; |
| 164 |
if (err_fmt == NULL) { |
| 165 |
snprintf(err_buf, _ERR_STRING_MAX_LENGTH, "function %.256s", msg); |
| 166 |
} |
| 167 |
else if (*err_fmt == ':') { |
| 168 |
snprintf(err_buf, _ERR_STRING_MAX_LENGTH, "%.200s() %.256s", err_fmt + 1, msg); |
| 169 |
} |
| 170 |
else { |
| 171 |
snprintf(err_buf, _ERR_STRING_MAX_LENGTH, "%s", err_fmt + 1); |
| 172 |
} |
| 173 |
HPyErr_SetString(ctx, exc, err_buf); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
static int |
| 178 |
parse_item(HPyContext *ctx, HPyTracker *ht, HPy current_arg, int current_arg_tmp, const char **fmt, va_list *vl, const char *err_fmt) |
| 179 |
{ |
| 180 |
switch (*(*fmt)++) { |
| 181 |
|
| 182 |
case 'b': { |
| 183 |
char *output = va_arg(*vl, char *); |
| 184 |
_BREAK_IF_OPTIONAL(current_arg); |
| 185 |
long value = HPyLong_AsLong(ctx, current_arg); |
| 186 |
if (value == -1 && HPyErr_Occurred(ctx)) |
| 187 |
return 0; |
| 188 |
if (value < 0) { |
| 189 |
set_error(ctx, ctx->h_OverflowError, err_fmt, |
| 190 |
"unsigned byte integer is less than minimum"); |
| 191 |
return 0; |
| 192 |
} |
| 193 |
if (value > UCHAR_MAX) { |
| 194 |
set_error(ctx, ctx->h_OverflowError, err_fmt, |
| 195 |
"unsigned byte integer is greater than maximum"); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
*output = (char) value; |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
case 'B': { |
| 203 |
|
| 204 |
char *output = va_arg(*vl, char *); |
| 205 |
_BREAK_IF_OPTIONAL(current_arg); |
| 206 |
unsigned long value = HPyLong_AsUnsignedLongMask(ctx, current_arg); |
| 207 |
if (value == (unsigned long)-1 && HPyErr_Occurred(ctx)) |
| 208 |
return 0; |
| 209 |
*output = (unsigned char) value; |
| 210 |
break; |
| 211 |
} |
| 212 |
|
| 213 |
case 'h': { |
| 214 |
short *output = va_arg(*vl, short *); |
| 215 |
_BREAK_IF_OPTIONAL(current_arg); |
| 216 |
long value = HPyLong_AsLong(ctx, current_arg); |
| 217 |
if (value == -1 && HPyErr_Occurred(ctx)) |
| 218 |
return 0; |
| 219 |
if (value < SHRT_MIN) { |
| 220 |
set_error(ctx, ctx->h_OverflowError, err_fmt, |
| 221 |
"signed short integer is less than minimum"); |
| 222 |
return 0; |
| 223 |
} |
| 224 |
if (value > SHRT_MAX) { |
| 225 |
set_error(ctx, ctx->h_OverflowError, err_fmt, |
| 226 |
"signed short integer is greater than maximum"); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
*output = (short) value; |
| 230 |
break; |
| 231 |
} |
| 232 |
|
| 233 |
case 'H': { |
| 234 |
|
| 235 |
unsigned short *output = va_arg(*vl, unsigned short *); |
| 236 |
_BREAK_IF_OPTIONAL(current_arg); |
| 237 |
unsigned long value = HPyLong_AsUnsignedLongMask(ctx, current_arg); |
| 238 |
if (value == (unsigned long)-1 && HPyErr_Occurred(ctx)) |
| 239 |
return 0; |
| 240 |
*output = (unsigned short) value; |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
case 'i': { |
| 245 |
int *output = va_arg(*vl, int *); |
| 246 |
_BREAK_IF_OPTIONAL(current_arg); |
| 247 |
long value = HPyLong_AsLong(ctx, current_arg); |
| 248 |
if (value == -1 && HPyErr_Occurred(ctx)) |
| 249 |
return 0; |
| 250 |
if (value > INT_MAX) { |
| 251 |
set_error(ctx, ctx->h_OverflowError, err_fmt, |
| 252 |
"signed integer is greater than maximum"); |
| 253 |
return 0; |
| 254 |
} |
| 255 |
if (value < INT_MIN) { |
| 256 |
set_error(ctx, ctx->h_OverflowError, err_fmt, |
| 257 |
"signed integer is less than minimum"); |
| 258 |
return 0; |
| 259 |
} |
| 260 |
*output = (int)value; |
| 261 |
break; |
| 262 |
} |
| 263 |
|
| 264 |
case 'I': { |
| 265 |
|
| 266 |
unsigned int *output = va_arg(*vl, unsigned int *); |
| 267 |
_BREAK_IF_OPTIONAL(current_arg); |
| 268 |
unsigned long value = HPyLong_AsUnsignedLongMask(ctx, current_arg); |
| 269 |
if (value == (unsigned long)-1 && HPyErr_Occurred(ctx)) |
| 270 |
return 0; |
| 271 |
*output = (unsigned int) value; |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
case 'l': { |
| 276 |
long *output = va_arg(*vl, long *); |
| 277 |
_BREAK_IF_OPTIONAL(current_arg); |
| 278 |
long value = HPyLong_AsLong(ctx, current_arg); |
| 279 |
if (value == -1 && HPyErr_Occurred(ctx)) |
| 280 |
return 0; |
| 281 |
*output = value; |
| 282 |
break; |
| 283 |
} |
| 284 |
|
| 285 |
case 'k': { |
| 286 |
unsigned long *output = va_arg(*vl, unsigned long *); |
| 287 |
_BREAK_IF_OPTIONAL(current_arg); |
| 288 |
unsigned long value = HPyLong_AsUnsignedLongMask(ctx, current_arg); |
| 289 |
if (value == (unsigned long)-1 && HPyErr_Occurred(ctx)) |
| 290 |
return 0; |
| 291 |
*output = value; |
| 292 |
break; |
| 293 |
} |
| 294 |
|
| 295 |
case 'L': { |
| 296 |
long long *output = va_arg(*vl, long long *); |
| 297 |
_BREAK_IF_OPTIONAL(current_arg); |
| 298 |
long long value = HPyLong_AsLongLong(ctx, current_arg); |
| 299 |
if (value == (long long)-1 && HPyErr_Occurred(ctx)) |
| 300 |
return 0; |
| 301 |
*output = value; |
| 302 |
break; |
| 303 |
} |
| 304 |
|
| 305 |
case 'K': { |
| 306 |
unsigned long long *output = va_arg(*vl, unsigned long long *); |
| 307 |
_BREAK_IF_OPTIONAL(current_arg); |
| 308 |
unsigned long long value = HPyLong_AsUnsignedLongLongMask(ctx, current_arg); |
| 309 |
if (value == (unsigned long long)-1 && HPyErr_Occurred(ctx)) |
| 310 |
return 0; |
| 311 |
*output = value; |
| 312 |
break; |
| 313 |
} |
| 314 |
|
| 315 |
case 'n': { |
| 316 |
HPy_ssize_t *output = va_arg(*vl, HPy_ssize_t *); |
| 317 |
_BREAK_IF_OPTIONAL(current_arg); |
| 318 |
HPy_ssize_t value = HPyLong_AsSsize_t(ctx, current_arg); |
| 319 |
if (value == (HPy_ssize_t)-1 && HPyErr_Occurred(ctx)) |
| 320 |
return 0; |
| 321 |
*output = value; |
| 322 |
break; |
| 323 |
} |
| 324 |
|
| 325 |
case 'f': { |
| 326 |
float *output = va_arg(*vl, float *); |
| 327 |
_BREAK_IF_OPTIONAL(current_arg); |
| 328 |
double value = HPyFloat_AsDouble(ctx, current_arg); |
| 329 |
if (value == -1.0 && HPyErr_Occurred(ctx)) |
| 330 |
return 0; |
| 331 |
*output = (float) value; |
| 332 |
break; |
| 333 |
} |
| 334 |
|
| 335 |
case 'd': { |
| 336 |
double* output = va_arg(*vl, double *); |
| 337 |
_BREAK_IF_OPTIONAL(current_arg); |
| 338 |
double value = HPyFloat_AsDouble(ctx, current_arg); |
| 339 |
if (value == -1.0 && HPyErr_Occurred(ctx)) |
| 340 |
return 0; |
| 341 |
*output = value; |
| 342 |
break; |
| 343 |
} |
| 344 |
|
| 345 |
case 'O': { |
| 346 |
HPy *output = va_arg(*vl, HPy *); |
| 347 |
_BREAK_IF_OPTIONAL(current_arg); |
| 348 |
if (current_arg_tmp) { |
| 349 |
*output = HPy_Dup(ctx, current_arg); |
| 350 |
HPyTracker_Add(ctx, *ht, *output); |
| 351 |
} |
| 352 |
else { |
| 353 |
*output = current_arg; |
| 354 |
} |
| 355 |
break; |
| 356 |
} |
| 357 |
|
| 358 |
case 'p': { |
| 359 |
int *output = va_arg(*vl, int *); |
| 360 |
int value = HPy_IsTrue(ctx, current_arg); |
| 361 |
if (value < 0) |
| 362 |
return 0; |
| 363 |
*output = (value > 0) ? 1 : 0; |
| 364 |
break; |
| 365 |
} |
| 366 |
|
| 367 |
case 's': { |
| 368 |
const char **output = va_arg(*vl, const char **); |
| 369 |
if (!HPyUnicode_Check(ctx, current_arg)) { |
| 370 |
set_error(ctx, ctx->h_TypeError, err_fmt, "a str is required"); |
| 371 |
return 0; |
| 372 |
} |
| 373 |
HPy_ssize_t size; |
| 374 |
const char *data = HPyUnicode_AsUTF8AndSize(ctx, current_arg, &size); |
| 375 |
if (data == NULL) { |
| 376 |
set_error(ctx, ctx->h_SystemError, err_fmt, "unicode conversion error"); |
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
HPy_ssize_t i; |
| 381 |
for (i = 0; i < size; ++i) { |
| 382 |
if (data[i] == '\0') { |
| 383 |
set_error(ctx, ctx->h_ValueError, err_fmt, "embedded null character"); |
| 384 |
return 0; |
| 385 |
} |
| 386 |
} |
| 387 |
if (data[i] != '\0') { |
| 388 |
set_error(ctx, ctx->h_SystemError, err_fmt, "missing terminating null character"); |
| 389 |
return 0; |
| 390 |
} |
| 391 |
*output = data; |
| 392 |
break; |
| 393 |
} |
| 394 |
|
| 395 |
default: { |
| 396 |
set_error(ctx, ctx->h_SystemError, err_fmt, "unknown arg format code"); |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
} |
| 401 |
|
| 402 |
return 1; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
HPyAPI_HELPER int |
| 466 |
HPyArg_Parse(HPyContext *ctx, HPyTracker *ht, const HPy *args, size_t nargs, const char *fmt, ...) |
| 467 |
{ |
| 468 |
const char *fmt1 = fmt; |
| 469 |
const char *err_fmt = NULL; |
| 470 |
const char *fmt_end = NULL; |
| 471 |
|
| 472 |
int optional = 0; |
| 473 |
size_t i = 0; |
| 474 |
HPy current_arg; |
| 475 |
|
| 476 |
fmt_end = parse_err_fmt(fmt, &err_fmt); |
| 477 |
|
| 478 |
if (ht != NULL) { |
| 479 |
*ht = HPyTracker_New(ctx, 0); |
| 480 |
if (HPy_IsNull(*ht)) { |
| 481 |
return 0; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
va_list vl; |
| 486 |
va_start(vl, fmt); |
| 487 |
|
| 488 |
while (fmt1 != fmt_end) { |
| 489 |
if (*fmt1 == '|') { |
| 490 |
optional = 1; |
| 491 |
fmt1++; |
| 492 |
continue; |
| 493 |
} |
| 494 |
current_arg = HPy_NULL; |
| 495 |
if (i < nargs) { |
| 496 |
current_arg = args[i]; |
| 497 |
} |
| 498 |
if (!HPy_IsNull(current_arg) || optional) { |
| 499 |
if (!parse_item(ctx, ht, current_arg, 0, &fmt1, &vl, err_fmt)) { |
| 500 |
goto error; |
| 501 |
} |
| 502 |
} |
| 503 |
else { |
| 504 |
set_error(ctx, ctx->h_TypeError, err_fmt, |
| 505 |
"required positional argument missing"); |
| 506 |
goto error; |
| 507 |
} |
| 508 |
i++; |
| 509 |
} |
| 510 |
if (i < nargs) { |
| 511 |
set_error(ctx, ctx->h_TypeError, err_fmt, |
| 512 |
"mismatched args (too many arguments for fmt)"); |
| 513 |
goto error; |
| 514 |
} |
| 515 |
|
| 516 |
va_end(vl); |
| 517 |
return 1; |
| 518 |
|
| 519 |
error: |
| 520 |
va_end(vl); |
| 521 |
if (ht != NULL) { |
| 522 |
HPyTracker_Close(ctx, *ht); |
| 523 |
} |
| 524 |
return 0; |
| 525 |
} |
| 526 |
|
| 527 |
static HPy |
| 528 |
find_keyword(HPyContext *ctx, HPy kwnames, HPy_ssize_t n_kwnames, const HPy *args, const char *key) |
| 529 |
{ |
| 530 |
for (HPy_ssize_t i = 0; i < n_kwnames; i++) { |
| 531 |
HPy kwname = HPy_GetItem_i(ctx, kwnames, i); |
| 532 |
HPy h_key = HPyUnicode_FromString(ctx, key); |
| 533 |
int eq = HPy_RichCompareBool(ctx, kwname, h_key, HPy_EQ); |
| 534 |
HPy_Close(ctx, h_key); |
| 535 |
HPy_Close(ctx, kwname); |
| 536 |
if (eq) { |
| 537 |
return args[i]; |
| 538 |
} |
| 539 |
} |
| 540 |
return HPy_NULL; |
| 541 |
} |
| 542 |
|
| 543 |
static int |
| 544 |
parse_keywords(HPyContext *ctx, HPyTracker *ht, const HPy *args, |
| 545 |
size_t nargs, HPy kw, int kwnames, const char *fmt, |
| 546 |
const char *keywords[], va_list *p_va) |
| 547 |
{ |
| 548 |
const char *fmt1 = fmt; |
| 549 |
const char *err_fmt = NULL; |
| 550 |
const char *fmt_end = NULL; |
| 551 |
|
| 552 |
int optional = 0; |
| 553 |
int keyword_only = 0; |
| 554 |
size_t i = 0; |
| 555 |
size_t nkw = 0; |
| 556 |
HPy_ssize_t n_kwnames = 0; |
| 557 |
HPy current_arg; |
| 558 |
int current_arg_needs_closing = 0; |
| 559 |
|
| 560 |
assert(p_va != NULL); |
| 561 |
|
| 562 |
fmt_end = parse_err_fmt(fmt, &err_fmt); |
| 563 |
|
| 564 |
|
| 565 |
while (keywords[nkw] != NULL && !*keywords[nkw]) { |
| 566 |
nkw++; |
| 567 |
} |
| 568 |
|
| 569 |
while (keywords[nkw] != NULL) { |
| 570 |
if (!*keywords[nkw]) { |
| 571 |
set_error(ctx, ctx->h_SystemError, err_fmt, |
| 572 |
"empty keyword parameter name"); |
| 573 |
return 0; |
| 574 |
} |
| 575 |
nkw++; |
| 576 |
} |
| 577 |
|
| 578 |
if (ht != NULL) { |
| 579 |
*ht = HPyTracker_New(ctx, 0); |
| 580 |
if (HPy_IsNull(*ht)) { |
| 581 |
return 0; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
if (kwnames && !HPy_IsNull(kw)) { |
| 586 |
n_kwnames = HPy_Length(ctx, kw); |
| 587 |
if (n_kwnames < 0) { |
| 588 |
set_error(ctx, ctx->h_SystemError, err_fmt, |
| 589 |
"could not retrieve length of keyword names tuple"); |
| 590 |
return 0; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
while (fmt1 != fmt_end) { |
| 595 |
if (*fmt1 == '|') { |
| 596 |
optional = 1; |
| 597 |
fmt1++; |
| 598 |
continue; |
| 599 |
} |
| 600 |
if (*fmt1 == '$') { |
| 601 |
optional = 1; |
| 602 |
keyword_only = 1; |
| 603 |
fmt1++; |
| 604 |
continue; |
| 605 |
} |
| 606 |
if (*fmt1 == 'O' && ht == NULL) { |
| 607 |
set_error(ctx, ctx->h_SystemError, err_fmt, |
| 608 |
"HPyArg_ParseKeywords cannot use the format character 'O' unless" |
| 609 |
" an HPyTracker is provided. Please supply an HPyTracker."); |
| 610 |
goto error; |
| 611 |
} |
| 612 |
if (i >= nkw) { |
| 613 |
set_error(ctx, ctx->h_TypeError, err_fmt, |
| 614 |
"mismatched args (too few keywords for fmt)"); |
| 615 |
goto error; |
| 616 |
} |
| 617 |
current_arg = HPy_NULL; |
| 618 |
if (i < nargs) { |
| 619 |
if (keyword_only) { |
| 620 |
set_error(ctx, ctx->h_TypeError, err_fmt, |
| 621 |
"keyword only argument passed as positional argument"); |
| 622 |
goto error; |
| 623 |
} |
| 624 |
current_arg = args[i]; |
| 625 |
} |
| 626 |
else if (!HPy_IsNull(kw) && *keywords[i]) { |
| 627 |
if (kwnames) { |
| 628 |
current_arg = find_keyword(ctx, kw, n_kwnames, args+nargs, |
| 629 |
keywords[i]); |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
if (HPy_IsNull(current_arg)) { |
| 637 |
HPyErr_Clear(ctx); |
| 638 |
} |
| 639 |
} else { |
| 640 |
current_arg = HPy_GetItem_s(ctx, kw, keywords[i]); |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
if (!HPy_IsNull(current_arg)) { |
| 646 |
current_arg_needs_closing = 1; |
| 647 |
} else { |
| 648 |
HPyErr_Clear(ctx); |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
if (!HPy_IsNull(current_arg) || optional) { |
| 653 |
if (!parse_item(ctx, ht, current_arg, 1, &fmt1, p_va, err_fmt)) { |
| 654 |
goto error; |
| 655 |
} |
| 656 |
} |
| 657 |
else { |
| 658 |
set_error(ctx, ctx->h_TypeError, err_fmt, |
| 659 |
"no value for required argument"); |
| 660 |
goto error; |
| 661 |
} |
| 662 |
if (current_arg_needs_closing) { |
| 663 |
HPy_Close(ctx, current_arg); |
| 664 |
current_arg_needs_closing = 0; |
| 665 |
} |
| 666 |
i++; |
| 667 |
} |
| 668 |
if (i != nkw) { |
| 669 |
set_error(ctx, ctx->h_TypeError, err_fmt, |
| 670 |
"mismatched args (too many keywords for fmt)"); |
| 671 |
goto error; |
| 672 |
} |
| 673 |
return 1; |
| 674 |
|
| 675 |
error: |
| 676 |
if (ht != NULL) { |
| 677 |
HPyTracker_Close(ctx, *ht); |
| 678 |
} |
| 679 |
if (current_arg_needs_closing) { |
| 680 |
HPy_Close(ctx, current_arg); |
| 681 |
} |
| 682 |
return 0; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
HPyAPI_HELPER int |
| 755 |
HPyArg_ParseKeywords(HPyContext *ctx, HPyTracker *ht, const HPy *args, |
| 756 |
size_t nargs, HPy kwnames, const char *fmt, |
| 757 |
const char *keywords[], ...) |
| 758 |
{ |
| 759 |
int retval; |
| 760 |
va_list va; |
| 761 |
|
| 762 |
va_start(va, keywords); |
| 763 |
retval = parse_keywords(ctx, ht, args, nargs, kwnames, 1, fmt, keywords, &va); |
| 764 |
va_end(va); |
| 765 |
return retval; |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
HPyAPI_HELPER int |
| 808 |
HPyArg_ParseKeywordsDict(HPyContext *ctx, HPyTracker *ht, const HPy *args, |
| 809 |
HPy_ssize_t nargs, HPy kw, const char *fmt, |
| 810 |
const char *keywords[], ...) |
| 811 |
{ |
| 812 |
int retval; |
| 813 |
va_list va; |
| 814 |
|
| 815 |
va_start(va, keywords); |
| 816 |
retval = parse_keywords(ctx, ht, args, nargs, kw, 0, fmt, keywords, &va); |
| 817 |
va_end(va); |
| 818 |
return retval; |
| 819 |
} |