1 /*
2 * Tests for Python/getargs.c and Python/modsupport.c;
3 * APIs that parse and build arguments.
4 */
5
6 #define PY_SSIZE_T_CLEAN
7
8 #include "parts.h"
9
10 static PyObject *
11 parse_tuple_and_keywords(PyObject *self, PyObject *args)
12 {
13 PyObject *sub_args;
14 PyObject *sub_kwargs;
15 const char *sub_format;
16 PyObject *sub_keywords;
17
18 double buffers[8][4]; /* double ensures alignment where necessary */
19 PyObject *converted[8];
20 char *keywords[8 + 1]; /* space for NULL at end */
21
22 PyObject *return_value = NULL;
23
24 if (!PyArg_ParseTuple(args, "OOsO:parse_tuple_and_keywords",
25 &sub_args, &sub_kwargs, &sub_format, &sub_keywords))
26 {
27 return NULL;
28 }
29
30 if (!(PyList_CheckExact(sub_keywords) ||
31 PyTuple_CheckExact(sub_keywords)))
32 {
33 PyErr_SetString(PyExc_ValueError,
34 "parse_tuple_and_keywords: "
35 "sub_keywords must be either list or tuple");
36 return NULL;
37 }
38
39 memset(buffers, 0, sizeof(buffers));
40 memset(converted, 0, sizeof(converted));
41 memset(keywords, 0, sizeof(keywords));
42
43 Py_ssize_t size = PySequence_Fast_GET_SIZE(sub_keywords);
44 if (size > 8) {
45 PyErr_SetString(PyExc_ValueError,
46 "parse_tuple_and_keywords: too many keywords in sub_keywords");
47 goto exit;
48 }
49
50 for (Py_ssize_t i = 0; i < size; i++) {
51 PyObject *o = PySequence_Fast_GET_ITEM(sub_keywords, i);
52 if (!PyUnicode_FSConverter(o, (void *)(converted + i))) {
53 PyErr_Format(PyExc_ValueError,
54 "parse_tuple_and_keywords: "
55 "could not convert keywords[%zd] to narrow string", i);
56 goto exit;
57 }
58 keywords[i] = PyBytes_AS_STRING(converted[i]);
59 }
60
61 int result = PyArg_ParseTupleAndKeywords(sub_args, sub_kwargs,
62 sub_format, keywords,
63 buffers + 0, buffers + 1, buffers + 2, buffers + 3,
64 buffers + 4, buffers + 5, buffers + 6, buffers + 7);
65
66 if (result) {
67 return_value = Py_NewRef(Py_None);
68 }
69
70 exit:
71 size = sizeof(converted) / sizeof(converted[0]);
72 for (Py_ssize_t i = 0; i < size; i++) {
73 Py_XDECREF(converted[i]);
74 }
75 return return_value;
76 }
77
78 static PyObject *
79 get_args(PyObject *self, PyObject *args)
80 {
81 if (args == NULL) {
82 args = Py_None;
83 }
84 return Py_NewRef(args);
85 }
86
87 static PyObject *
88 get_kwargs(PyObject *self, PyObject *args, PyObject *kwargs)
89 {
90 if (kwargs == NULL) {
91 kwargs = Py_None;
92 }
93 return Py_NewRef(kwargs);
94 }
95
96 static PyObject *
97 getargs_w_star(PyObject *self, PyObject *args)
98 {
99 Py_buffer buffer;
100
101 if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer)) {
102 return NULL;
103 }
104
105 if (2 <= buffer.len) {
106 char *str = buffer.buf;
107 str[0] = '[';
108 str[buffer.len-1] = ']';
109 }
110
111 PyObject *result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
112 PyBuffer_Release(&buffer);
113 return result;
114 }
115
116 static PyObject *
117 test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored))
118 {
119 /* Test that formats can begin with '|'. See issue #4720. */
120 PyObject *dict = NULL;
121 static char *kwlist[] = {NULL};
122 PyObject *tuple = PyTuple_New(0);
123 if (!tuple) {
124 return NULL;
125 }
126 int result;
127 if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) {
128 goto done;
129 }
130 dict = PyDict_New();
131 if (!dict) {
132 goto done;
133 }
134 result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse",
135 kwlist);
136 done:
137 Py_DECREF(tuple);
138 Py_XDECREF(dict);
139 if (!result) {
140 return NULL;
141 }
142 Py_RETURN_NONE;
143 }
144
145 /* Test tuple argument processing */
146 static PyObject *
147 getargs_tuple(PyObject *self, PyObject *args)
148 {
149 int a, b, c;
150 if (!PyArg_ParseTuple(args, "i(ii)", &a, &b, &c)) {
151 return NULL;
152 }
153 return Py_BuildValue("iii", a, b, c);
154 }
155
156 /* test PyArg_ParseTupleAndKeywords */
157 static PyObject *
158 getargs_keywords(PyObject *self, PyObject *args, PyObject *kwargs)
159 {
160 static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL};
161 static const char fmt[] = "(ii)i|(i(ii))(iii)i";
162 int int_args[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
163
164 if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
165 &int_args[0], &int_args[1], &int_args[2], &int_args[3], &int_args[4],
166 &int_args[5], &int_args[6], &int_args[7], &int_args[8], &int_args[9]))
167 {
168 return NULL;
169 }
170 return Py_BuildValue("iiiiiiiiii",
171 int_args[0], int_args[1], int_args[2], int_args[3], int_args[4],
172 int_args[5], int_args[6], int_args[7], int_args[8], int_args[9]);
173 }
174
175 /* test PyArg_ParseTupleAndKeywords keyword-only arguments */
176 static PyObject *
177 getargs_keyword_only(PyObject *self, PyObject *args, PyObject *kwargs)
178 {
179 static char *keywords[] = {"required", "optional", "keyword_only", NULL};
180 int required = -1;
181 int optional = -1;
182 int keyword_only = -1;
183
184 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i$i", keywords,
185 &required, &optional, &keyword_only))
186 {
187 return NULL;
188 }
189 return Py_BuildValue("iii", required, optional, keyword_only);
190 }
191
192 /* test PyArg_ParseTupleAndKeywords positional-only arguments */
193 static PyObject *
194 getargs_positional_only_and_keywords(PyObject *self, PyObject *args,
195 PyObject *kwargs)
196 {
197 static char *keywords[] = {"", "", "keyword", NULL};
198 int required = -1;
199 int optional = -1;
200 int keyword = -1;
201
202 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|ii", keywords,
203 &required, &optional, &keyword))
204 {
205 return NULL;
206 }
207 return Py_BuildValue("iii", required, optional, keyword);
208 }
209
210 /* Functions to call PyArg_ParseTuple with integer format codes,
211 and return the result.
212 */
213 static PyObject *
214 getargs_b(PyObject *self, PyObject *args)
215 {
216 unsigned char value;
217 if (!PyArg_ParseTuple(args, "b", &value)) {
218 return NULL;
219 }
220 return PyLong_FromUnsignedLong((unsigned long)value);
221 }
222
223 static PyObject *
224 getargs_B(PyObject *self, PyObject *args)
225 {
226 unsigned char value;
227 if (!PyArg_ParseTuple(args, "B", &value)) {
228 return NULL;
229 }
230 return PyLong_FromUnsignedLong((unsigned long)value);
231 }
232
233 static PyObject *
234 getargs_h(PyObject *self, PyObject *args)
235 {
236 short value;
237 if (!PyArg_ParseTuple(args, "h", &value)) {
238 return NULL;
239 }
240 return PyLong_FromLong((long)value);
241 }
242
243 static PyObject *
244 getargs_H(PyObject *self, PyObject *args)
245 {
246 unsigned short value;
247 if (!PyArg_ParseTuple(args, "H", &value)) {
248 return NULL;
249 }
250 return PyLong_FromUnsignedLong((unsigned long)value);
251 }
252
253 static PyObject *
254 getargs_I(PyObject *self, PyObject *args)
255 {
256 unsigned int value;
257 if (!PyArg_ParseTuple(args, "I", &value)) {
258 return NULL;
259 }
260 return PyLong_FromUnsignedLong((unsigned long)value);
261 }
262
263 static PyObject *
264 getargs_k(PyObject *self, PyObject *args)
265 {
266 unsigned long value;
267 if (!PyArg_ParseTuple(args, "k", &value)) {
268 return NULL;
269 }
270 return PyLong_FromUnsignedLong(value);
271 }
272
273 static PyObject *
274 getargs_i(PyObject *self, PyObject *args)
275 {
276 int value;
277 if (!PyArg_ParseTuple(args, "i", &value)) {
278 return NULL;
279 }
280 return PyLong_FromLong((long)value);
281 }
282
283 static PyObject *
284 getargs_l(PyObject *self, PyObject *args)
285 {
286 long value;
287 if (!PyArg_ParseTuple(args, "l", &value)) {
288 return NULL;
289 }
290 return PyLong_FromLong(value);
291 }
292
293 static PyObject *
294 getargs_n(PyObject *self, PyObject *args)
295 {
296 Py_ssize_t value;
297 if (!PyArg_ParseTuple(args, "n", &value)) {
298 return NULL;
299 }
300 return PyLong_FromSsize_t(value);
301 }
302
303 static PyObject *
304 getargs_p(PyObject *self, PyObject *args)
305 {
306 int value;
307 if (!PyArg_ParseTuple(args, "p", &value)) {
308 return NULL;
309 }
310 return PyLong_FromLong(value);
311 }
312
313 static PyObject *
314 getargs_L(PyObject *self, PyObject *args)
315 {
316 long long value;
317 if (!PyArg_ParseTuple(args, "L", &value)) {
318 return NULL;
319 }
320 return PyLong_FromLongLong(value);
321 }
322
323 static PyObject *
324 getargs_K(PyObject *self, PyObject *args)
325 {
326 unsigned long long value;
327 if (!PyArg_ParseTuple(args, "K", &value)) {
328 return NULL;
329 }
330 return PyLong_FromUnsignedLongLong(value);
331 }
332
333 /* This function not only tests the 'k' getargs code, but also the
334 PyLong_AsUnsignedLongMask() function. */
335 static PyObject *
336 test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored))
337 {
338 PyObject *tuple, *num;
339 unsigned long value;
340
341 tuple = PyTuple_New(1);
342 if (tuple == NULL) {
343 return NULL;
344 }
345
346 /* a number larger than ULONG_MAX even on 64-bit platforms */
347 num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
348 if (num == NULL) {
349 return NULL;
350 }
351
352 value = PyLong_AsUnsignedLongMask(num);
353 if (value != ULONG_MAX) {
354 PyErr_SetString(PyExc_AssertionError,
355 "test_k_code: "
356 "PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
357 return NULL;
358 }
359
360 PyTuple_SET_ITEM(tuple, 0, num);
361
362 value = 0;
363 if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
364 return NULL;
365 }
366 if (value != ULONG_MAX) {
367 PyErr_SetString(PyExc_AssertionError,
368 "test_k_code: k code returned wrong value for long 0xFFF...FFF");
369 return NULL;
370 }
371
372 Py_DECREF(num);
373 num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
374 if (num == NULL) {
375 return NULL;
376 }
377
378 value = PyLong_AsUnsignedLongMask(num);
379 if (value != (unsigned long)-0x42) {
380 PyErr_SetString(PyExc_AssertionError,
381 "test_k_code: "
382 "PyLong_AsUnsignedLongMask() returned wrong value for long -0xFFF..000042");
383 return NULL;
384 }
385
386 PyTuple_SET_ITEM(tuple, 0, num);
387
388 value = 0;
389 if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
390 return NULL;
391 }
392 if (value != (unsigned long)-0x42) {
393 PyErr_SetString(PyExc_AssertionError,
394 "test_k_code: k code returned wrong value for long -0xFFF..000042");
395 return NULL;
396 }
397
398 Py_DECREF(tuple);
399 Py_RETURN_NONE;
400 }
401
402 static PyObject *
403 getargs_f(PyObject *self, PyObject *args)
404 {
405 float f;
406 if (!PyArg_ParseTuple(args, "f", &f)) {
407 return NULL;
408 }
409 return PyFloat_FromDouble(f);
410 }
411
412 static PyObject *
413 getargs_d(PyObject *self, PyObject *args)
414 {
415 double d;
416 if (!PyArg_ParseTuple(args, "d", &d)) {
417 return NULL;
418 }
419 return PyFloat_FromDouble(d);
420 }
421
422 static PyObject *
423 getargs_D(PyObject *self, PyObject *args)
424 {
425 Py_complex cval;
426 if (!PyArg_ParseTuple(args, "D", &cval)) {
427 return NULL;
428 }
429 return PyComplex_FromCComplex(cval);
430 }
431
432 static PyObject *
433 getargs_S(PyObject *self, PyObject *args)
434 {
435 PyObject *obj;
436 if (!PyArg_ParseTuple(args, "S", &obj)) {
437 return NULL;
438 }
439 return Py_NewRef(obj);
440 }
441
442 static PyObject *
443 getargs_Y(PyObject *self, PyObject *args)
444 {
445 PyObject *obj;
446 if (!PyArg_ParseTuple(args, "Y", &obj)) {
447 return NULL;
448 }
449 return Py_NewRef(obj);
450 }
451
452 static PyObject *
453 getargs_U(PyObject *self, PyObject *args)
454 {
455 PyObject *obj;
456 if (!PyArg_ParseTuple(args, "U", &obj)) {
457 return NULL;
458 }
459 return Py_NewRef(obj);
460 }
461
462 static PyObject *
463 getargs_c(PyObject *self, PyObject *args)
464 {
465 char c;
466 if (!PyArg_ParseTuple(args, "c", &c)) {
467 return NULL;
468 }
469 return PyLong_FromLong((unsigned char)c);
470 }
471
472 static PyObject *
473 getargs_C(PyObject *self, PyObject *args)
474 {
475 int c;
476 if (!PyArg_ParseTuple(args, "C", &c)) {
477 return NULL;
478 }
479 return PyLong_FromLong(c);
480 }
481
482 static PyObject *
483 getargs_s(PyObject *self, PyObject *args)
484 {
485 char *str;
486 if (!PyArg_ParseTuple(args, "s", &str)) {
487 return NULL;
488 }
489 return PyBytes_FromString(str);
490 }
491
492 static PyObject *
493 getargs_s_star(PyObject *self, PyObject *args)
494 {
495 Py_buffer buffer;
496 PyObject *bytes;
497 if (!PyArg_ParseTuple(args, "s*", &buffer)) {
498 return NULL;
499 }
500 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
501 PyBuffer_Release(&buffer);
502 return bytes;
503 }
504
505 static PyObject *
506 getargs_s_hash(PyObject *self, PyObject *args)
507 {
508 char *str;
509 Py_ssize_t size;
510 if (!PyArg_ParseTuple(args, "s#", &str, &size)) {
511 return NULL;
512 }
513 return PyBytes_FromStringAndSize(str, size);
514 }
515
516 static PyObject *
517 getargs_z(PyObject *self, PyObject *args)
518 {
519 char *str;
520 if (!PyArg_ParseTuple(args, "z", &str)) {
521 return NULL;
522 }
523 if (str != NULL) {
524 return PyBytes_FromString(str);
525 }
526 Py_RETURN_NONE;
527 }
528
529 static PyObject *
530 getargs_z_star(PyObject *self, PyObject *args)
531 {
532 Py_buffer buffer;
533 PyObject *bytes;
534 if (!PyArg_ParseTuple(args, "z*", &buffer)) {
535 return NULL;
536 }
537 if (buffer.buf != NULL) {
538 bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
539 }
540 else {
541 bytes = Py_NewRef(Py_None);
542 }
543 PyBuffer_Release(&buffer);
544 return bytes;
545 }
546
547 static PyObject *
548 getargs_z_hash(PyObject *self, PyObject *args)
549 {
550 char *str;
551 Py_ssize_t size;
552 if (!PyArg_ParseTuple(args, "z#", &str, &size)) {
553 return NULL;
554 }
555 if (str != NULL) {
556 return PyBytes_FromStringAndSize(str, size);
557 }
558 Py_RETURN_NONE;
559 }
560
561 static PyObject *
562 getargs_y(PyObject *self, PyObject *args)
563 {
564 char *str;
565 if (!PyArg_ParseTuple(args, "y", &str)) {
566 return NULL;
567 }
568 return PyBytes_FromString(str);
569 }
570
571 static PyObject *
572 getargs_y_star(PyObject *self, PyObject *args)
573 {
574 Py_buffer buffer;
575 if (!PyArg_ParseTuple(args, "y*", &buffer)) {
576 return NULL;
577 }
578 PyObject *bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len);
579 PyBuffer_Release(&buffer);
580 return bytes;
581 }
582
583 static PyObject *
584 getargs_y_hash(PyObject *self, PyObject *args)
585 {
586 char *str;
587 Py_ssize_t size;
588 if (!PyArg_ParseTuple(args, "y#", &str, &size)) {
589 return NULL;
590 }
591 return PyBytes_FromStringAndSize(str, size);
592 }
593
594 static PyObject *
595 getargs_u(PyObject *self, PyObject *args)
596 {
597 Py_UNICODE *str;
598 if (!PyArg_ParseTuple(args, "u", &str)) {
599 return NULL;
600 }
601 return PyUnicode_FromWideChar(str, -1);
602 }
603
604 static PyObject *
605 getargs_u_hash(PyObject *self, PyObject *args)
606 {
607 Py_UNICODE *str;
608 Py_ssize_t size;
609 if (!PyArg_ParseTuple(args, "u#", &str, &size)) {
610 return NULL;
611 }
612 return PyUnicode_FromWideChar(str, size);
613 }
614
615 static PyObject *
616 getargs_Z(PyObject *self, PyObject *args)
617 {
618 Py_UNICODE *str;
619 if (!PyArg_ParseTuple(args, "Z", &str)) {
620 return NULL;
621 }
622 if (str != NULL) {
623 return PyUnicode_FromWideChar(str, -1);
624 }
625 Py_RETURN_NONE;
626 }
627
628 static PyObject *
629 getargs_Z_hash(PyObject *self, PyObject *args)
630 {
631 Py_UNICODE *str;
632 Py_ssize_t size;
633 if (!PyArg_ParseTuple(args, "Z#", &str, &size)) {
634 return NULL;
635 }
636 if (str != NULL) {
637 return PyUnicode_FromWideChar(str, size);
638 }
639 Py_RETURN_NONE;
640 }
641
642 static PyObject *
643 getargs_es(PyObject *self, PyObject *args)
644 {
645 PyObject *arg;
646 const char *encoding = NULL;
647 char *str;
648
649 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) {
650 return NULL;
651 }
652 if (!PyArg_Parse(arg, "es", encoding, &str)) {
653 return NULL;
654 }
655 PyObject *result = PyBytes_FromString(str);
656 PyMem_Free(str);
657 return result;
658 }
659
660 static PyObject *
661 getargs_et(PyObject *self, PyObject *args)
662 {
663 PyObject *arg;
664 const char *encoding = NULL;
665 char *str;
666
667 if (!PyArg_ParseTuple(args, "O|s", &arg, &encoding)) {
668 return NULL;
669 }
670 if (!PyArg_Parse(arg, "et", encoding, &str)) {
671 return NULL;
672 }
673 PyObject *result = PyBytes_FromString(str);
674 PyMem_Free(str);
675 return result;
676 }
677
678 static PyObject *
679 getargs_es_hash(PyObject *self, PyObject *args)
680 {
681 PyObject *arg;
682 const char *encoding = NULL;
683 PyByteArrayObject *buffer = NULL;
684 char *str = NULL;
685 Py_ssize_t size;
686
687 if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) {
688 return NULL;
689 }
690 if (buffer != NULL) {
691 str = PyByteArray_AS_STRING(buffer);
692 size = PyByteArray_GET_SIZE(buffer);
693 }
694 if (!PyArg_Parse(arg, "es#", encoding, &str, &size)) {
695 return NULL;
696 }
697 PyObject *result = PyBytes_FromStringAndSize(str, size);
698 if (buffer == NULL) {
699 PyMem_Free(str);
700 }
701 return result;
702 }
703
704 static PyObject *
705 getargs_et_hash(PyObject *self, PyObject *args)
706 {
707 PyObject *arg;
708 const char *encoding = NULL;
709 PyByteArrayObject *buffer = NULL;
710 char *str = NULL;
711 Py_ssize_t size;
712
713 if (!PyArg_ParseTuple(args, "O|sY", &arg, &encoding, &buffer)) {
714 return NULL;
715 }
716 if (buffer != NULL) {
717 str = PyByteArray_AS_STRING(buffer);
718 size = PyByteArray_GET_SIZE(buffer);
719 }
720 if (!PyArg_Parse(arg, "et#", encoding, &str, &size)) {
721 return NULL;
722 }
723 PyObject *result = PyBytes_FromStringAndSize(str, size);
724 if (buffer == NULL) {
725 PyMem_Free(str);
726 }
727 return result;
728 }
729
730 /* Test the L code for PyArg_ParseTuple. This should deliver a long long
731 for both long and int arguments. The test may leak a little memory if
732 it fails.
733 */
734 static PyObject *
735 test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored))
736 {
737 PyObject *tuple, *num;
738 long long value;
739
740 tuple = PyTuple_New(1);
741 if (tuple == NULL) {
742 return NULL;
743 }
744
745 num = PyLong_FromLong(42);
746 if (num == NULL) {
747 return NULL;
748 }
749
750 PyTuple_SET_ITEM(tuple, 0, num);
751
752 value = -1;
753 if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
754 return NULL;
755 }
756 if (value != 42) {
757 PyErr_SetString(PyExc_AssertionError,
758 "test_L_code: L code returned wrong value for long 42");
759 return NULL;
760 }
761
762 Py_DECREF(num);
763 num = PyLong_FromLong(42);
764 if (num == NULL) {
765 return NULL;
766 }
767
768 PyTuple_SET_ITEM(tuple, 0, num);
769
770 value = -1;
771 if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
772 return NULL;
773 }
774 if (value != 42) {
775 PyErr_SetString(PyExc_AssertionError,
776 "test_L_code: L code returned wrong value for int 42");
777 return NULL;
778 }
779
780 Py_DECREF(tuple);
781 Py_RETURN_NONE;
782 }
783
784 /* Test the s and z codes for PyArg_ParseTuple.
785 */
786 static PyObject *
787 test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored))
788 {
789 /* Unicode strings should be accepted */
790 PyObject *tuple = PyTuple_New(1);
791 if (tuple == NULL) {
792 return NULL;
793 }
794
795 PyObject *obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
796 "latin-1", NULL);
797 if (obj == NULL) {
798 return NULL;
799 }
800
801 PyTuple_SET_ITEM(tuple, 0, obj);
802
803 /* These two blocks used to raise a TypeError:
804 * "argument must be string without null bytes, not str"
805 */
806 char *value;
807 if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) {
808 return NULL;
809 }
810
811 if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) {
812 return NULL;
813 }
814
815 Py_DECREF(tuple);
816 Py_RETURN_NONE;
817 }
818
819 #undef PyArg_ParseTupleAndKeywords
820 PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
821 const char *, char **, ...);
822
823 static PyObject *
824 getargs_s_hash_int(PyObject *self, PyObject *args, PyObject *kwargs)
825 {
826 static char *keywords[] = {"", "", "x", NULL};
827 Py_buffer buf = {NULL};
828 const char *s;
829 int len;
830 int i = 0;
831 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "w*|s#i", keywords,
832 &buf, &s, &len, &i))
833 {
834 return NULL;
835 }
836 PyBuffer_Release(&buf);
837 Py_RETURN_NONE;
838 }
839
840 static PyObject *
841 getargs_s_hash_int2(PyObject *self, PyObject *args, PyObject *kwargs)
842 {
843 static char *keywords[] = {"", "", "x", NULL};
844 Py_buffer buf = {NULL};
845 const char *s;
846 int len;
847 int i = 0;
848 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "w*|(s#)i", keywords,
849 &buf, &s, &len, &i))
850 {
851 return NULL;
852 }
853 PyBuffer_Release(&buf);
854 Py_RETURN_NONE;
855 }
856
857 static PyObject *
858 gh_99240_clear_args(PyObject *self, PyObject *args)
859 {
860 char *a = NULL;
861 char *b = NULL;
862
863 if (!PyArg_ParseTuple(args, "eses", "idna", &a, "idna", &b)) {
864 if (a || b) {
865 PyErr_Clear();
866 PyErr_SetString(PyExc_AssertionError, "Arguments are not cleared.");
867 }
868 return NULL;
869 }
870 PyMem_Free(a);
871 PyMem_Free(b);
872 Py_RETURN_NONE;
873 }
874
875 static PyMethodDef test_methods[] = {
876 {"get_args", get_args, METH_VARARGS},
877 {"get_kwargs", _PyCFunction_CAST(get_kwargs), METH_VARARGS|METH_KEYWORDS},
878 {"getargs_B", getargs_B, METH_VARARGS},
879 {"getargs_C", getargs_C, METH_VARARGS},
880 {"getargs_D", getargs_D, METH_VARARGS},
881 {"getargs_H", getargs_H, METH_VARARGS},
882 {"getargs_I", getargs_I, METH_VARARGS},
883 {"getargs_K", getargs_K, METH_VARARGS},
884 {"getargs_L", getargs_L, METH_VARARGS},
885 {"getargs_S", getargs_S, METH_VARARGS},
886 {"getargs_U", getargs_U, METH_VARARGS},
887 {"getargs_Y", getargs_Y, METH_VARARGS},
888 {"getargs_Z", getargs_Z, METH_VARARGS},
889 {"getargs_Z_hash", getargs_Z_hash, METH_VARARGS},
890 {"getargs_b", getargs_b, METH_VARARGS},
891 {"getargs_c", getargs_c, METH_VARARGS},
892 {"getargs_d", getargs_d, METH_VARARGS},
893 {"getargs_es", getargs_es, METH_VARARGS},
894 {"getargs_es_hash", getargs_es_hash, METH_VARARGS},
895 {"getargs_et", getargs_et, METH_VARARGS},
896 {"getargs_et_hash", getargs_et_hash, METH_VARARGS},
897 {"getargs_f", getargs_f, METH_VARARGS},
898 {"getargs_h", getargs_h, METH_VARARGS},
899 {"getargs_i", getargs_i, METH_VARARGS},
900 {"getargs_k", getargs_k, METH_VARARGS},
901 {"getargs_keyword_only", _PyCFunction_CAST(getargs_keyword_only), METH_VARARGS|METH_KEYWORDS},
902 {"getargs_keywords", _PyCFunction_CAST(getargs_keywords), METH_VARARGS|METH_KEYWORDS},
903 {"getargs_l", getargs_l, METH_VARARGS},
904 {"getargs_n", getargs_n, METH_VARARGS},
905 {"getargs_p", getargs_p, METH_VARARGS},
906 {"getargs_positional_only_and_keywords", _PyCFunction_CAST(getargs_positional_only_and_keywords), METH_VARARGS|METH_KEYWORDS},
907 {"getargs_s", getargs_s, METH_VARARGS},
908 {"getargs_s_hash", getargs_s_hash, METH_VARARGS},
909 {"getargs_s_hash_int", _PyCFunction_CAST(getargs_s_hash_int), METH_VARARGS|METH_KEYWORDS},
910 {"getargs_s_hash_int2", _PyCFunction_CAST(getargs_s_hash_int2), METH_VARARGS|METH_KEYWORDS},
911 {"getargs_s_star", getargs_s_star, METH_VARARGS},
912 {"getargs_tuple", getargs_tuple, METH_VARARGS},
913 {"getargs_u", getargs_u, METH_VARARGS},
914 {"getargs_u_hash", getargs_u_hash, METH_VARARGS},
915 {"getargs_w_star", getargs_w_star, METH_VARARGS},
916 {"getargs_y", getargs_y, METH_VARARGS},
917 {"getargs_y_hash", getargs_y_hash, METH_VARARGS},
918 {"getargs_y_star", getargs_y_star, METH_VARARGS},
919 {"getargs_z", getargs_z, METH_VARARGS},
920 {"getargs_z_hash", getargs_z_hash, METH_VARARGS},
921 {"getargs_z_star", getargs_z_star, METH_VARARGS},
922 {"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
923 {"test_L_code", test_L_code, METH_NOARGS},
924 {"test_empty_argparse", test_empty_argparse, METH_NOARGS},
925 {"test_k_code", test_k_code, METH_NOARGS},
926 {"test_s_code", test_s_code, METH_NOARGS},
927 {"gh_99240_clear_args", gh_99240_clear_args, METH_VARARGS},
928 {NULL},
929 };
930
931 int
932 _PyTestCapi_Init_GetArgs(PyObject *mod)
933 {
934 if (PyModule_AddFunctions(mod, test_methods) < 0) {
935 return -1;
936 }
937
938 return 0;
939 }