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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
|
"""
NOTE: this tests are also meant to be run as PyPy "applevel" tests.
This means that global imports will NOT be visible inside the test
functions. In particular, you have to "import pytest" inside the test in order
to be able to use e.g. pytest.raises (which on PyPy will be implemented by a
"fake pytest module")
"""
from .support import HPyTest
class TestParseItem(HPyTest):
def unsigned_long_bits(self):
""" Return the number of bits in an unsigned long. """
# XXX: Copied from test_hpylong.py
import struct
unsigned_long_bytes = len(struct.pack('l', 0))
return 8 * unsigned_long_bytes
def make_parse_item(self, fmt, type, hpy_converter):
mod = self.make_module("""
#ifndef _MSC_VER
__attribute__((unused))
#endif
static inline
HPy char_to_hpybytes(HPyContext *ctx, char a) {{
return HPyBytes_FromStringAndSize(ctx, &a, 1);
}}
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{{
{type} a;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "{fmt}", &a))
return HPy_NULL;
return {hpy_converter}(ctx, a);
}}
@EXPORT(f)
@INIT
""".format(fmt=fmt, type=type, hpy_converter=hpy_converter))
return mod
def test_b(self):
import pytest
mod = self.make_parse_item("b", "char", "char_to_hpybytes")
assert mod.f(0) == b"\x00"
assert mod.f(1) == b"\x01"
assert mod.f(255) == b"\xff"
with pytest.raises(OverflowError) as err:
mod.f(256)
assert str(err.value) == (
"function unsigned byte integer is greater than maximum"
)
with pytest.raises(OverflowError) as err:
mod.f(-1)
assert str(err.value) == (
"function unsigned byte integer is less than minimum"
)
def test_s(self):
import pytest
mod = self.make_parse_item("s", "const char*", "HPyUnicode_FromString")
assert mod.f("hello HPy") == "hello HPy"
with pytest.raises(ValueError) as err:
mod.f(b"hello\0HPy".decode('utf-8'))
assert str(err.value) == (
"function embedded null character"
)
with pytest.raises(TypeError) as err:
mod.f(b"hello HPy")
assert str(err.value) == (
"function a str is required"
)
def test_upper_b(self):
mod = self.make_parse_item("B", "char", "char_to_hpybytes")
assert mod.f(0) == b"\x00"
assert mod.f(1) == b"\x01"
assert mod.f(2**8 - 1) == b"\xff"
assert mod.f(2**8) == b"\x00"
assert mod.f(-1) == b"\xff"
def test_h(self):
import pytest
mod = self.make_parse_item("h", "short", "HPyLong_FromLong")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == -1
assert mod.f(2**15 - 1) == 2**15 - 1
assert mod.f(-2**15) == -2**15
with pytest.raises(OverflowError) as err:
mod.f(2**15)
assert str(err.value) == (
"function signed short integer is greater than maximum"
)
with pytest.raises(OverflowError) as err:
mod.f(-2**15 - 1)
assert str(err.value) == (
"function signed short integer is less than minimum"
)
def test_upper_h_short(self):
mod = self.make_parse_item("H", "short", "HPyLong_FromLong")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == -1
assert mod.f(2**15 - 1) == 2**15 - 1
assert mod.f(-2**15) == -2**15
assert mod.f(2**16 - 1) == -1
assert mod.f(-2**16 + 1) == 1
assert mod.f(2**16) == 0
assert mod.f(-2**16) == 0
def test_upper_h_unsigned_short(self):
mod = self.make_parse_item(
"H", "unsigned short", "HPyLong_FromUnsignedLong"
)
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == 2**16 - 1
assert mod.f(2**16 - 1) == 2**16 - 1
assert mod.f(-2**16 + 1) == 1
assert mod.f(2**16) == 0
assert mod.f(-2**16) == 0
def test_i(self):
import pytest
mod = self.make_parse_item("i", "int", "HPyLong_FromLong")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == -1
assert mod.f(2**31 - 1) == 2**31 - 1
assert mod.f(-2**31) == -2**31
with pytest.raises(OverflowError) as err:
mod.f(2**31)
assert str(err.value) in (
"function signed integer is greater than maximum",
"Python int too large to convert to C long", # where sizeof(long) == 4
)
with pytest.raises(OverflowError) as err:
mod.f(-2**31 - 1)
assert str(err.value) in (
"function signed integer is less than minimum",
"Python int too large to convert to C long", # where sizeof(long) == 4
)
def test_upper_i_signed(self):
mod = self.make_parse_item("I", "int", "HPyLong_FromLong")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == -1
assert mod.f(2**31 - 1) == 2**31 - 1
assert mod.f(-2**31) == -2**31
assert mod.f(2**32 - 1) == -1
assert mod.f(-2**32 + 1) == 1
assert mod.f(2**32) == 0
assert mod.f(-2**32) == 0
def test_upper_i_unsigned(self):
mod = self.make_parse_item(
"I", "unsigned int", "HPyLong_FromUnsignedLong"
)
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == 2**32 - 1
assert mod.f(2**32 - 1) == 2**32 - 1
assert mod.f(-2**32 + 1) == 1
assert mod.f(2**32) == 0
assert mod.f(-2**32) == 0
def test_l(self):
import pytest
LONG_BITS = self.unsigned_long_bits() - 1
mod = self.make_parse_item("l", "long", "HPyLong_FromLong")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == -1
assert mod.f(2**LONG_BITS - 1) == 2**LONG_BITS - 1
assert mod.f(-2**LONG_BITS) == -2**LONG_BITS
with pytest.raises(OverflowError):
mod.f(2**LONG_BITS)
with pytest.raises(OverflowError):
mod.f(-2**LONG_BITS - 1)
def test_k_signed(self):
LONG_BITS = self.unsigned_long_bits() - 1
mod = self.make_parse_item("k", "long", "HPyLong_FromLong")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == -1
assert mod.f(2**LONG_BITS - 1) == 2**LONG_BITS - 1
assert mod.f(-2**LONG_BITS) == -2**LONG_BITS
assert mod.f(2**(LONG_BITS + 1) - 1) == -1
assert mod.f(-2**(LONG_BITS + 1) + 1) == 1
assert mod.f(2**(LONG_BITS + 1)) == 0
assert mod.f(-2**(LONG_BITS + 1)) == 0
def test_k_unsigned(self):
ULONG_BITS = self.unsigned_long_bits()
mod = self.make_parse_item(
"k", "unsigned long", "HPyLong_FromUnsignedLong"
)
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == 2**ULONG_BITS - 1
assert mod.f(2**ULONG_BITS - 1) == 2**ULONG_BITS - 1
assert mod.f(-2**ULONG_BITS + 1) == 1
assert mod.f(2**ULONG_BITS) == 0
assert mod.f(-2**ULONG_BITS) == 0
def test_upper_l(self):
import pytest
mod = self.make_parse_item("L", "long long", "HPyLong_FromLongLong")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == -1
assert mod.f(2**63 - 1) == 2**63 - 1
assert mod.f(-2**63) == -2**63
with pytest.raises(OverflowError):
mod.f(2**63)
with pytest.raises(OverflowError):
mod.f(-2**63 - 1)
def test_upper_k_signed(self):
mod = self.make_parse_item("K", "long long", "HPyLong_FromLongLong")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == -1
assert mod.f(2**63 - 1) == 2**63 - 1
assert mod.f(-2**63) == -2**63
assert mod.f(2**64 - 1) == -1
assert mod.f(-2**64 + 1) == 1
assert mod.f(2**64) == 0
assert mod.f(-2**64) == 0
def test_upper_k_unsigned(self):
mod = self.make_parse_item(
"K", "unsigned long long", "HPyLong_FromUnsignedLongLong"
)
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == 2**64 - 1
assert mod.f(2**64 - 1) == 2**64 - 1
assert mod.f(-2**64 + 1) == 1
assert mod.f(2**64) == 0
assert mod.f(-2**64) == 0
def test_n(self):
import pytest
mod = self.make_parse_item("n", "HPy_ssize_t", "HPyLong_FromSsize_t")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == -1
assert mod.f(2**63 - 1) == 2**63 - 1
assert mod.f(-2**63) == -2**63
with pytest.raises(OverflowError):
mod.f(2**63)
with pytest.raises(OverflowError):
mod.f(-2**63 - 1)
def test_f(self):
import pytest
mod = self.make_parse_item("f", "float", "HPyFloat_FromDouble")
assert mod.f(1.) == 1.
assert mod.f(-2) == -2.
with pytest.raises(TypeError):
mod.f("x")
def test_d(self):
import pytest
mod = self.make_parse_item("d", "double", "HPyFloat_FromDouble")
assert mod.f(1.) == 1.
assert mod.f(-2) == -2.
with pytest.raises(TypeError):
mod.f("x")
def test_upper_o(self):
mod = self.make_parse_item("O", "HPy", "HPy_Dup")
assert mod.f("a") == "a"
assert mod.f(5) == 5
def test_p(self):
mod = self.make_parse_item("p", "int", "HPyLong_FromLong")
assert mod.f(0) == 0
assert mod.f(1) == 1
assert mod.f(-1) == 1
assert mod.f(False) == 0
assert mod.f(True) == 1
assert mod.f([]) == 0
assert mod.f([0]) == 1
assert mod.f("") == 0
assert mod.f("0") == 1
class TestArgParse(HPyTest):
def make_two_arg_add(self, fmt="OO"):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{{
HPy a;
HPy b = HPy_NULL;
HPy res;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "{fmt}", &a, &b))
return HPy_NULL;
if (HPy_IsNull(b)) {{
b = HPyLong_FromLong(ctx, 5);
}} else {{
b = HPy_Dup(ctx, b);
}}
res = HPy_Add(ctx, a, b);
HPy_Close(ctx, b);
return res;
}}
@EXPORT(f)
@INIT
""".format(fmt=fmt))
return mod
def test_many_int_arguments(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{
long a, b, c, d, e;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "lllll",
&a, &b, &c, &d, &e))
return HPy_NULL;
return HPyLong_FromLong(ctx,
10000*a + 1000*b + 100*c + 10*d + e);
}
@EXPORT(f)
@INIT
""")
assert mod.f(4, 5, 6, 7, 8) == 45678
def test_many_handle_arguments(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{
HPy a, b;
if (!HPyArg_Parse(ctx, NULL, args, nargs, "OO", &a, &b))
return HPy_NULL;
return HPy_Add(ctx, a, b);
}
@EXPORT(f)
@INIT
""")
assert mod.f("a", "b") == "ab"
def test_supplying_hpy_tracker(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs)
{
HPy a, b, result;
HPyTracker ht;
if (!HPyArg_Parse(ctx, &ht, args, nargs, "OO", &a, &b))
return HPy_NULL;
result = HPy_Add(ctx, a, b);
HPyTracker_Close(ctx, ht);
return result;
}
@EXPORT(f)
@INIT
""")
assert mod.f("a", "b") == "ab"
def test_unsupported_fmt(self):
import pytest
mod = self.make_two_arg_add(fmt="ZZ:two_add")
with pytest.raises(SystemError) as exc:
mod.f("a")
assert str(exc.value) == "two_add() unknown arg format code"
def test_too_few_args(self):
import pytest
mod = self.make_two_arg_add("OO:two_add")
with pytest.raises(TypeError) as exc:
mod.f()
assert str(exc.value) == "two_add() required positional argument missing"
def test_too_many_args(self):
import pytest
mod = self.make_two_arg_add("OO:two_add")
with pytest.raises(TypeError) as exc:
mod.f(1, 2, 3)
assert str(exc.value) == "two_add() mismatched args (too many arguments for fmt)"
def test_optional_args(self):
mod = self.make_two_arg_add(fmt="O|O")
assert mod.f(1) == 6
assert mod.f(3, 4) == 7
def test_keyword_only_args_fails(self):
import pytest
mod = self.make_two_arg_add(fmt="O$O:two_add")
with pytest.raises(SystemError) as exc:
mod.f(1, 2)
assert str(exc.value) == "two_add() unknown arg format code"
def test_error_default_message(self):
import pytest
mod = self.make_two_arg_add(fmt="OOO")
with pytest.raises(TypeError) as exc:
mod.f(1, 2)
assert str(exc.value) == "function required positional argument missing"
def test_error_with_function_name(self):
import pytest
mod = self.make_two_arg_add(fmt="OOO:my_func")
with pytest.raises(TypeError) as exc:
mod.f(1, 2)
assert str(exc.value) == "my_func() required positional argument missing"
def test_error_with_overridden_message(self):
import pytest
mod = self.make_two_arg_add(fmt="OOO;my-error-message")
with pytest.raises(TypeError) as exc:
mod.f(1, 2)
assert str(exc.value) == "my-error-message"
class TestArgParseKeywords(HPyTest):
def make_two_arg_add(self, fmt="O+O+"):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_KEYWORDS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs, HPy kwnames)
{{
HPy a, b, result;
HPyTracker ht;
static const char *kwlist[] = {{ "a", "b", NULL }};
if (!HPyArg_ParseKeywords(ctx, &ht, args, nargs, kwnames,
"{fmt}", kwlist, &a, &b)) {{
return HPy_NULL;
}}
result = HPy_Add(ctx, a, b);
HPyTracker_Close(ctx, ht);
return result;
}}
@EXPORT(f)
@INIT
""".format(fmt=fmt))
return mod
def test_handle_two_arguments(self):
mod = self.make_two_arg_add("OO")
assert mod.f("x", b="y") == "xy"
def test_handle_reordered_arguments(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_KEYWORDS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs, HPy kwnames)
{
HPy a, b, result;
HPyTracker ht;
static const char *kwlist[] = { "a", "b", NULL };
if (!HPyArg_ParseKeywords(ctx, &ht, args, nargs, kwnames, "OO",
kwlist, &a, &b)) {
return HPy_NULL;
}
result = HPy_Add(ctx, a, b);
HPyTracker_Close(ctx, ht);
return result;
}
@EXPORT(f)
@INIT
""")
assert mod.f(b="y", a="x") == "xy"
def test_handle_optional_arguments(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_KEYWORDS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs, HPy kwnames)
{
HPy a;
HPy b = HPy_NULL;
HPyTracker ht;
HPy res;
static const char *kwlist[] = { "a", "b", NULL };
if (!HPyArg_ParseKeywords(ctx, &ht, args, nargs, kwnames, "O|O",
kwlist, &a, &b)) {
return HPy_NULL;
}
if (HPy_IsNull(b)) {
b = HPyLong_FromLong(ctx, 5);
HPyTracker_Add(ctx, ht, b);
}
res = HPy_Add(ctx, a, b);
HPyTracker_Close(ctx, ht);
return res;
}
@EXPORT(f)
@INIT
""")
assert mod.f(a=3, b=2) == 5
assert mod.f(3, 2) == 5
assert mod.f(a=3) == 8
assert mod.f(3) == 8
def test_unsupported_fmt(self):
import pytest
mod = self.make_two_arg_add(fmt="ZZ:two_add")
with pytest.raises(SystemError) as exc:
mod.f("a")
assert str(exc.value) == "two_add() unknown arg format code"
def test_missing_required_argument(self):
import pytest
mod = self.make_two_arg_add(fmt="OO:add_two")
with pytest.raises(TypeError) as exc:
mod.f(1)
assert str(exc.value) == "add_two() no value for required argument"
def test_mismatched_args_too_few_keywords(self):
import pytest
mod = self.make_two_arg_add(fmt="OOO:add_two")
with pytest.raises(TypeError) as exc:
mod.f(1, 2)
assert str(exc.value) == "add_two() mismatched args (too few keywords for fmt)"
def test_mismatched_args_too_many_keywords(self):
import pytest
mod = self.make_two_arg_add(fmt="O:add_two")
with pytest.raises(TypeError) as exc:
mod.f(1, 2)
assert str(exc.value) == "add_two() mismatched args (too many keywords for fmt)"
def test_blank_keyword_argument_exception(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_KEYWORDS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs, HPy kwnames)
{
long a, b, c;
static const char *kwlist[] = { "", "b", "", NULL };
if (!HPyArg_ParseKeywords(ctx, NULL, args, nargs, kwnames,
"lll", kwlist, &a, &b, &c))
return HPy_NULL;
return HPy_Dup(ctx, ctx->h_None);
}
@EXPORT(f)
@INIT
""")
with pytest.raises(SystemError) as exc:
mod.f()
assert str(exc.value) == "function empty keyword parameter name"
def test_positional_only_argument(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_KEYWORDS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs, HPy kwnames)
{
HPy a;
HPy b = HPy_NULL;
HPyTracker ht;
HPy res;
static const char *kwlist[] = { "", "b", NULL };
if (!HPyArg_ParseKeywords(ctx, &ht, args, nargs, kwnames,
"O|O", kwlist, &a, &b)) {
return HPy_NULL;
}
if (HPy_IsNull(b)) {
b = HPyLong_FromLong(ctx, 5);
HPyTracker_Add(ctx, ht, b);
}
res = HPy_Add(ctx, a, b);
HPyTracker_Close(ctx, ht);
return res;
}
@EXPORT(f)
@INIT
""")
assert mod.f(1, b=2) == 3
assert mod.f(1, 2) == 3
assert mod.f(1) == 6
with pytest.raises(TypeError) as exc:
mod.f(a=1, b=2)
assert str(exc.value) == "function no value for required argument"
def test_keyword_only_argument(self):
import pytest
mod = self.make_two_arg_add(fmt="O$O")
assert mod.f(1, b=2) == 3
assert mod.f(a=1, b=2) == 3
with pytest.raises(TypeError) as exc:
mod.f(1, 2)
assert str(exc.value) == (
"function keyword only argument passed as positional argument")
def test_error_default_message(self):
import pytest
mod = self.make_two_arg_add(fmt="OOO")
with pytest.raises(TypeError) as exc:
mod.f(1, 2)
assert str(exc.value) == "function mismatched args (too few keywords for fmt)"
def test_error_with_function_name(self):
import pytest
mod = self.make_two_arg_add(fmt="OOO:my_func")
with pytest.raises(TypeError) as exc:
mod.f(1, 2)
assert str(exc.value) == "my_func() mismatched args (too few keywords for fmt)"
def test_error_with_overridden_message(self):
import pytest
mod = self.make_two_arg_add(fmt="OOO;my-error-message")
with pytest.raises(TypeError) as exc:
mod.f(1, 2)
assert str(exc.value) == "my-error-message"
def test_keywords_dict(self):
import pytest
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_KEYWORDS)
static HPy f_impl(HPyContext *ctx, HPy self,
const HPy *args, size_t nargs, HPy kwnames)
{
HPy args_tuple, kwdict;
HPy a, b = HPy_NULL, res;
HPyTracker ht;
HPy_ssize_t n_args_tuple, i;
HPy *args_arr;
int status;
static const char *kwlist[] = { "a", "b", NULL };
if (nargs != 2) {
HPyErr_SetString(ctx, ctx->h_SystemError,
"expected exactly two args");
return HPy_NULL;
}
args_tuple = args[0];
kwdict = args[1];
n_args_tuple = HPy_Length(ctx, args_tuple);
args_arr = (HPy *)malloc(n_args_tuple * sizeof(HPy));
for (i=0; i < n_args_tuple; i++)
args_arr[i] = HPy_GetItem_i(ctx, args_tuple, i);
status = HPyArg_ParseKeywordsDict(ctx, &ht, args_arr,
n_args_tuple, kwdict, "O|O", kwlist, &a, &b);
for (i=0; i < n_args_tuple; i++)
HPy_Close(ctx, args_arr[i]);
free(args_arr);
if (!status) {
return HPy_NULL;
}
if (HPy_IsNull(b)) {
b = HPyLong_FromLong(ctx, 5);
HPyTracker_Add(ctx, ht, b);
}
res = HPy_Add(ctx, a, b);
HPyTracker_Close(ctx, ht);
return res;
}
@EXPORT(f)
@INIT
""")
assert mod.f(tuple(), dict(a=3, b=2)) == 5
assert mod.f((3, 2), {}) == 5
assert mod.f(tuple(), dict(a=3)) == 8
assert mod.f((3,), {}) == 8
with pytest.raises(TypeError):
mod.f(tuple(), {})
|