Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fac5b77

Browse files
committedAug 23, 2022
Add functional test for Argument Clinic
1 parent 2bd331e commit fac5b77

9 files changed

+369
-0
lines changed
 

‎Include/internal/pycore_global_strings.h

+5
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ struct _Py_global_strings {
425425
STRUCT_FOR_ID(keyfile)
426426
STRUCT_FOR_ID(keys)
427427
STRUCT_FOR_ID(kind)
428+
STRUCT_FOR_ID(kw)
429+
STRUCT_FOR_ID(kw1)
430+
STRUCT_FOR_ID(kw2)
428431
STRUCT_FOR_ID(lambda)
429432
STRUCT_FOR_ID(last)
430433
STRUCT_FOR_ID(last_node)
@@ -516,6 +519,8 @@ struct _Py_global_strings {
516519
STRUCT_FOR_ID(pid)
517520
STRUCT_FOR_ID(policy)
518521
STRUCT_FOR_ID(pos)
522+
STRUCT_FOR_ID(pos1)
523+
STRUCT_FOR_ID(pos2)
519524
STRUCT_FOR_ID(print_file_and_line)
520525
STRUCT_FOR_ID(priority)
521526
STRUCT_FOR_ID(progress)

‎Include/internal/pycore_runtime_init_generated.h

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Lib/test/test_clinic_functionality.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
from test.support import import_helper
3+
4+
ac_tester = import_helper.import_module('_testclinicfunctionality')
5+
6+
7+
class TestClinicFunctionality(unittest.TestCase):
8+
def test_gh_32092_oob(self):
9+
res = ac_tester.gh_32092_oob(1, 2, 3, 4, kw1=5, kw2=6)
10+
expect = (1, 2, (3, 4), 5, 6)
11+
self.assertEqual(res, expect)
12+
13+
def test_gh_32092_kw_pass(self):
14+
res = ac_tester.gh_32092_kw_pass(1, 2, 3)
15+
expect = (1, (2, 3), None)
16+
self.assertEqual(res, expect)

‎Modules/Setup.stdlib.in

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
171171
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c
172172
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c
173+
@MODULE__TESTCLINICFUNCTIONALITY_TRUE@_testclinicfunctionality _testclinicfunctionality.c
173174

174175
# Some testing modules MUST be built as shared libraries.
175176
*shared*

‎Modules/_testclinicfunctionality.c

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#ifndef Py_BUILD_CORE_BUILTIN
2+
# define Py_BUILD_CORE_MODULE 1
3+
#endif
4+
5+
/* Always enable assertions */
6+
#undef NDEBUG
7+
8+
#define PY_SSIZE_T_CLEAN
9+
10+
#include "Python.h"
11+
12+
#include "clinic/_testclinicfunctionality.c.h"
13+
14+
/*[clinic input]
15+
module _testclinicfunctionality
16+
[clinic start generated code]*/
17+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=19bd80db1aefb188]*/
18+
19+
/*[clinic input]
20+
gh_32092_oob
21+
22+
pos1: object
23+
pos2: object
24+
*varargs: object
25+
kw1: object = None
26+
kw2: object = None
27+
28+
Proof-of-concept of GH-32092 OOB bug.
29+
30+
Array index out-of-bound bug in function
31+
`_PyArg_UnpackKeywordsWithVararg` .
32+
33+
Calling this function by gh_32092_oob(1, 2, 3, 4, kw1=5, kw2=6)
34+
to trigger this bug (crash).
35+
Expected return: (1, 2, (3, 4), 5, 6)
36+
37+
[clinic start generated code]*/
38+
39+
static PyObject *
40+
gh_32092_oob_impl(PyObject *module, PyObject *pos1, PyObject *pos2,
41+
PyObject *varargs, PyObject *kw1, PyObject *kw2)
42+
/*[clinic end generated code: output=ee259c130054653f input=91d8e227acf93b02]*/
43+
{
44+
PyObject *tuple = PyTuple_New(5);
45+
PyTuple_SET_ITEM(tuple, 0, Py_NewRef(pos1));
46+
PyTuple_SET_ITEM(tuple, 1, Py_NewRef(pos2));
47+
PyTuple_SET_ITEM(tuple, 2, Py_NewRef(varargs));
48+
PyTuple_SET_ITEM(tuple, 3, Py_NewRef(kw1));
49+
PyTuple_SET_ITEM(tuple, 4, Py_NewRef(kw2));
50+
return tuple;
51+
}
52+
53+
/*[clinic input]
54+
gh_32092_kw_pass
55+
56+
pos: object
57+
*args: object
58+
kw: object = None
59+
60+
Proof-of-concept of GH-32092 keyword args passing bug.
61+
62+
The calculation of `noptargs` in AC-generated function
63+
`builtin_kw_pass_poc` is incorrect.
64+
65+
Calling this function by gh_32092_kw_pass(1, 2, 3)
66+
to trigger this bug (crash).
67+
Expected return: (1, (2, 3), None)
68+
69+
[clinic start generated code]*/
70+
71+
static PyObject *
72+
gh_32092_kw_pass_impl(PyObject *module, PyObject *pos, PyObject *args,
73+
PyObject *kw)
74+
/*[clinic end generated code: output=4a2bbe4f7c8604e9 input=5fc48f72f49193b6]*/
75+
{
76+
PyObject *tuple = PyTuple_New(3);
77+
PyTuple_SET_ITEM(tuple, 0, Py_NewRef(pos));
78+
PyTuple_SET_ITEM(tuple, 1, Py_NewRef(args));
79+
PyTuple_SET_ITEM(tuple, 2, Py_NewRef(kw));
80+
return tuple;
81+
}
82+
83+
static PyMethodDef tester_methods[] = {
84+
GH_32092_OOB_METHODDEF
85+
GH_32092_KW_PASS_METHODDEF
86+
{NULL, NULL}
87+
};
88+
89+
static struct PyModuleDef _testclinicfunctionality_module = {
90+
PyModuleDef_HEAD_INIT,
91+
"_testclinicfunctionality",
92+
NULL,
93+
0,
94+
tester_methods,
95+
NULL,
96+
NULL,
97+
NULL,
98+
NULL
99+
};
100+
101+
PyMODINIT_FUNC
102+
PyInit__testclinicfunctionality(void)
103+
{
104+
return PyModule_Create(&_testclinicfunctionality_module);
105+
}

‎Modules/clinic/_testclinicfunctionality.c.h

+165
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.