Skip to content

Commit b9c9a36

Browse files
gh-106368: Increase test coverage for Argument Clinic (#107514)
As per this commit, we've got approx. ~91% test coverage for clinic.py.
1 parent 9ff7b4a commit b9c9a36

File tree

2 files changed

+350
-15
lines changed

2 files changed

+350
-15
lines changed

Lib/test/clinic.test.c

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5004,3 +5004,263 @@ PyDoc_STRVAR(new_dest__doc__,
50045004
"\n"
50055005
"Only this docstring should be outputted to test1.");
50065006
/*[clinic end generated code: output=9cac703f51d90e84 input=090db8df4945576d]*/
5007+
5008+
5009+
/*[clinic input]
5010+
mangled_c_keyword_identifier
5011+
i as int: int
5012+
The 'int' param should be mangled as 'int_value'
5013+
[clinic start generated code]*/
5014+
5015+
PyDoc_STRVAR(mangled_c_keyword_identifier__doc__,
5016+
"mangled_c_keyword_identifier($module, /, i)\n"
5017+
"--\n"
5018+
"\n"
5019+
"The \'int\' param should be mangled as \'int_value\'");
5020+
5021+
#define MANGLED_C_KEYWORD_IDENTIFIER_METHODDEF \
5022+
{"mangled_c_keyword_identifier", _PyCFunction_CAST(mangled_c_keyword_identifier), METH_FASTCALL|METH_KEYWORDS, mangled_c_keyword_identifier__doc__},
5023+
5024+
static PyObject *
5025+
mangled_c_keyword_identifier_impl(PyObject *module, int int_value);
5026+
5027+
static PyObject *
5028+
mangled_c_keyword_identifier(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5029+
{
5030+
PyObject *return_value = NULL;
5031+
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
5032+
5033+
#define NUM_KEYWORDS 1
5034+
static struct {
5035+
PyGC_Head _this_is_not_used;
5036+
PyObject_VAR_HEAD
5037+
PyObject *ob_item[NUM_KEYWORDS];
5038+
} _kwtuple = {
5039+
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
5040+
.ob_item = { &_Py_ID(i), },
5041+
};
5042+
#undef NUM_KEYWORDS
5043+
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
5044+
5045+
#else // !Py_BUILD_CORE
5046+
# define KWTUPLE NULL
5047+
#endif // !Py_BUILD_CORE
5048+
5049+
static const char * const _keywords[] = {"i", NULL};
5050+
static _PyArg_Parser _parser = {
5051+
.keywords = _keywords,
5052+
.fname = "mangled_c_keyword_identifier",
5053+
.kwtuple = KWTUPLE,
5054+
};
5055+
#undef KWTUPLE
5056+
PyObject *argsbuf[1];
5057+
int int_value;
5058+
5059+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
5060+
if (!args) {
5061+
goto exit;
5062+
}
5063+
int_value = _PyLong_AsInt(args[0]);
5064+
if (int_value == -1 && PyErr_Occurred()) {
5065+
goto exit;
5066+
}
5067+
return_value = mangled_c_keyword_identifier_impl(module, int_value);
5068+
5069+
exit:
5070+
return return_value;
5071+
}
5072+
5073+
static PyObject *
5074+
mangled_c_keyword_identifier_impl(PyObject *module, int int_value)
5075+
/*[clinic end generated code: output=c049d7d79be26cda input=060876448ab567a2]*/
5076+
5077+
5078+
/*[clinic input]
5079+
bool_return -> bool
5080+
[clinic start generated code]*/
5081+
5082+
PyDoc_STRVAR(bool_return__doc__,
5083+
"bool_return($module, /)\n"
5084+
"--\n"
5085+
"\n");
5086+
5087+
#define BOOL_RETURN_METHODDEF \
5088+
{"bool_return", (PyCFunction)bool_return, METH_NOARGS, bool_return__doc__},
5089+
5090+
static int
5091+
bool_return_impl(PyObject *module);
5092+
5093+
static PyObject *
5094+
bool_return(PyObject *module, PyObject *Py_UNUSED(ignored))
5095+
{
5096+
PyObject *return_value = NULL;
5097+
int _return_value;
5098+
5099+
_return_value = bool_return_impl(module);
5100+
if ((_return_value == -1) && PyErr_Occurred()) {
5101+
goto exit;
5102+
}
5103+
return_value = PyBool_FromLong((long)_return_value);
5104+
5105+
exit:
5106+
return return_value;
5107+
}
5108+
5109+
static int
5110+
bool_return_impl(PyObject *module)
5111+
/*[clinic end generated code: output=3a65f07830e48e98 input=93ba95d39ee98f39]*/
5112+
5113+
5114+
/*[clinic input]
5115+
double_return -> double
5116+
[clinic start generated code]*/
5117+
5118+
PyDoc_STRVAR(double_return__doc__,
5119+
"double_return($module, /)\n"
5120+
"--\n"
5121+
"\n");
5122+
5123+
#define DOUBLE_RETURN_METHODDEF \
5124+
{"double_return", (PyCFunction)double_return, METH_NOARGS, double_return__doc__},
5125+
5126+
static double
5127+
double_return_impl(PyObject *module);
5128+
5129+
static PyObject *
5130+
double_return(PyObject *module, PyObject *Py_UNUSED(ignored))
5131+
{
5132+
PyObject *return_value = NULL;
5133+
double _return_value;
5134+
5135+
_return_value = double_return_impl(module);
5136+
if ((_return_value == -1.0) && PyErr_Occurred()) {
5137+
goto exit;
5138+
}
5139+
return_value = PyFloat_FromDouble(_return_value);
5140+
5141+
exit:
5142+
return return_value;
5143+
}
5144+
5145+
static double
5146+
double_return_impl(PyObject *module)
5147+
/*[clinic end generated code: output=076dc72595d3f66d input=da11b6255e4cbfd7]*/
5148+
5149+
5150+
/*[clinic input]
5151+
Test.__init__
5152+
a: object
5153+
[
5154+
b: object
5155+
]
5156+
/
5157+
Should generate two PyArg_ParseTuple calls.
5158+
[clinic start generated code]*/
5159+
5160+
PyDoc_STRVAR(Test___init____doc__,
5161+
"Test(a, [b])\n"
5162+
"Should generate two PyArg_ParseTuple calls.");
5163+
5164+
static int
5165+
Test___init___impl(TestObj *self, PyObject *a, int group_right_1,
5166+
PyObject *b);
5167+
5168+
static int
5169+
Test___init__(PyObject *self, PyObject *args, PyObject *kwargs)
5170+
{
5171+
int return_value = -1;
5172+
PyTypeObject *base_tp = TestType;
5173+
PyObject *a;
5174+
int group_right_1 = 0;
5175+
PyObject *b = NULL;
5176+
5177+
if ((Py_IS_TYPE(self, base_tp) ||
5178+
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
5179+
!_PyArg_NoKeywords("Test", kwargs)) {
5180+
goto exit;
5181+
}
5182+
switch (PyTuple_GET_SIZE(args)) {
5183+
case 1:
5184+
if (!PyArg_ParseTuple(args, "O:__init__", &a)) {
5185+
goto exit;
5186+
}
5187+
break;
5188+
case 2:
5189+
if (!PyArg_ParseTuple(args, "OO:__init__", &a, &b)) {
5190+
goto exit;
5191+
}
5192+
group_right_1 = 1;
5193+
break;
5194+
default:
5195+
PyErr_SetString(PyExc_TypeError, "Test.__init__ requires 1 to 2 arguments");
5196+
goto exit;
5197+
}
5198+
return_value = Test___init___impl((TestObj *)self, a, group_right_1, b);
5199+
5200+
exit:
5201+
return return_value;
5202+
}
5203+
5204+
static int
5205+
Test___init___impl(TestObj *self, PyObject *a, int group_right_1,
5206+
PyObject *b)
5207+
/*[clinic end generated code: output=2bbb8ea60e8f57a6 input=10f5d0f1e8e466ef]*/
5208+
5209+
5210+
/*[clinic input]
5211+
Test._pyarg_parsestackandkeywords
5212+
cls: defining_class
5213+
key: str(accept={str, robuffer}, zeroes=True)
5214+
/
5215+
Check that _PyArg_ParseStackAndKeywords() is generated.
5216+
[clinic start generated code]*/
5217+
5218+
PyDoc_STRVAR(Test__pyarg_parsestackandkeywords__doc__,
5219+
"_pyarg_parsestackandkeywords($self, key, /)\n"
5220+
"--\n"
5221+
"\n"
5222+
"Check that _PyArg_ParseStackAndKeywords() is generated.");
5223+
5224+
#define TEST__PYARG_PARSESTACKANDKEYWORDS_METHODDEF \
5225+
{"_pyarg_parsestackandkeywords", _PyCFunction_CAST(Test__pyarg_parsestackandkeywords), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, Test__pyarg_parsestackandkeywords__doc__},
5226+
5227+
static PyObject *
5228+
Test__pyarg_parsestackandkeywords_impl(TestObj *self, PyTypeObject *cls,
5229+
const char *key,
5230+
Py_ssize_t key_length);
5231+
5232+
static PyObject *
5233+
Test__pyarg_parsestackandkeywords(TestObj *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5234+
{
5235+
PyObject *return_value = NULL;
5236+
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
5237+
# define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
5238+
#else
5239+
# define KWTUPLE NULL
5240+
#endif
5241+
5242+
static const char * const _keywords[] = {"", NULL};
5243+
static _PyArg_Parser _parser = {
5244+
.keywords = _keywords,
5245+
.format = "s#:_pyarg_parsestackandkeywords",
5246+
.kwtuple = KWTUPLE,
5247+
};
5248+
#undef KWTUPLE
5249+
const char *key;
5250+
Py_ssize_t key_length;
5251+
5252+
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
5253+
&key, &key_length)) {
5254+
goto exit;
5255+
}
5256+
return_value = Test__pyarg_parsestackandkeywords_impl(self, cls, key, key_length);
5257+
5258+
exit:
5259+
return return_value;
5260+
}
5261+
5262+
static PyObject *
5263+
Test__pyarg_parsestackandkeywords_impl(TestObj *self, PyTypeObject *cls,
5264+
const char *key,
5265+
Py_ssize_t key_length)
5266+
/*[clinic end generated code: output=4fda8a7f2547137c input=fc72ef4b4cfafabc]*/

0 commit comments

Comments
 (0)