Debian Codegraph @

pypy/module/_hpy_universal/test/_vendored/test_argparse.py

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