Skip to content

Commit a73daf5

Browse files
gh-106368: Increase Argument Clinic test coverage (#107582)
Add tests for DSL parser state machine and docstring formatting
1 parent 46366ca commit a73daf5

File tree

2 files changed

+394
-2
lines changed

2 files changed

+394
-2
lines changed

Lib/test/clinic.test.c

+200
Original file line numberDiff line numberDiff line change
@@ -5264,3 +5264,203 @@ Test__pyarg_parsestackandkeywords_impl(TestObj *self, PyTypeObject *cls,
52645264
const char *key,
52655265
Py_ssize_t key_length)
52665266
/*[clinic end generated code: output=4fda8a7f2547137c input=fc72ef4b4cfafabc]*/
5267+
5268+
5269+
/*[clinic input]
5270+
Test.__init__ -> long
5271+
Test overriding the __init__ return converter
5272+
[clinic start generated code]*/
5273+
5274+
PyDoc_STRVAR(Test___init____doc__,
5275+
"Test()\n"
5276+
"--\n"
5277+
"\n"
5278+
"Test overriding the __init__ return converter");
5279+
5280+
static long
5281+
Test___init___impl(TestObj *self);
5282+
5283+
static int
5284+
Test___init__(PyObject *self, PyObject *args, PyObject *kwargs)
5285+
{
5286+
int return_value = -1;
5287+
PyTypeObject *base_tp = TestType;
5288+
long _return_value;
5289+
5290+
if ((Py_IS_TYPE(self, base_tp) ||
5291+
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
5292+
!_PyArg_NoPositional("Test", args)) {
5293+
goto exit;
5294+
}
5295+
if ((Py_IS_TYPE(self, base_tp) ||
5296+
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
5297+
!_PyArg_NoKeywords("Test", kwargs)) {
5298+
goto exit;
5299+
}
5300+
_return_value = Test___init___impl((TestObj *)self);
5301+
if ((_return_value == -1) && PyErr_Occurred()) {
5302+
goto exit;
5303+
}
5304+
return_value = PyLong_FromLong(_return_value);
5305+
5306+
exit:
5307+
return return_value;
5308+
}
5309+
5310+
static long
5311+
Test___init___impl(TestObj *self)
5312+
/*[clinic end generated code: output=daf6ee12c4e443fb input=311af0dc7f17e8e9]*/
5313+
5314+
5315+
/*[clinic input]
5316+
fn_with_default_binop_expr
5317+
arg: object(c_default='CONST_A + CONST_B') = a+b
5318+
[clinic start generated code]*/
5319+
5320+
PyDoc_STRVAR(fn_with_default_binop_expr__doc__,
5321+
"fn_with_default_binop_expr($module, /, arg=a+b)\n"
5322+
"--\n"
5323+
"\n");
5324+
5325+
#define FN_WITH_DEFAULT_BINOP_EXPR_METHODDEF \
5326+
{"fn_with_default_binop_expr", _PyCFunction_CAST(fn_with_default_binop_expr), METH_FASTCALL|METH_KEYWORDS, fn_with_default_binop_expr__doc__},
5327+
5328+
static PyObject *
5329+
fn_with_default_binop_expr_impl(PyObject *module, PyObject *arg);
5330+
5331+
static PyObject *
5332+
fn_with_default_binop_expr(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5333+
{
5334+
PyObject *return_value = NULL;
5335+
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
5336+
5337+
#define NUM_KEYWORDS 1
5338+
static struct {
5339+
PyGC_Head _this_is_not_used;
5340+
PyObject_VAR_HEAD
5341+
PyObject *ob_item[NUM_KEYWORDS];
5342+
} _kwtuple = {
5343+
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
5344+
.ob_item = { &_Py_ID(arg), },
5345+
};
5346+
#undef NUM_KEYWORDS
5347+
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
5348+
5349+
#else // !Py_BUILD_CORE
5350+
# define KWTUPLE NULL
5351+
#endif // !Py_BUILD_CORE
5352+
5353+
static const char * const _keywords[] = {"arg", NULL};
5354+
static _PyArg_Parser _parser = {
5355+
.keywords = _keywords,
5356+
.fname = "fn_with_default_binop_expr",
5357+
.kwtuple = KWTUPLE,
5358+
};
5359+
#undef KWTUPLE
5360+
PyObject *argsbuf[1];
5361+
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
5362+
PyObject *arg = CONST_A + CONST_B;
5363+
5364+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
5365+
if (!args) {
5366+
goto exit;
5367+
}
5368+
if (!noptargs) {
5369+
goto skip_optional_pos;
5370+
}
5371+
arg = args[0];
5372+
skip_optional_pos:
5373+
return_value = fn_with_default_binop_expr_impl(module, arg);
5374+
5375+
exit:
5376+
return return_value;
5377+
}
5378+
5379+
static PyObject *
5380+
fn_with_default_binop_expr_impl(PyObject *module, PyObject *arg)
5381+
/*[clinic end generated code: output=018672772e4092ff input=1b55c8ae68d89453]*/
5382+
5383+
/*[python input]
5384+
class Custom_converter(CConverter):
5385+
type = "str"
5386+
default = "Hello!"
5387+
converter = "c_converter_func"
5388+
[python start generated code]*/
5389+
/*[python end generated code: output=da39a3ee5e6b4b0d input=d612708f0efb8e3c]*/
5390+
5391+
/*[clinic input]
5392+
docstr_fallback_to_converter_default
5393+
a: Custom
5394+
Check docstring default value fallback.
5395+
5396+
Verify that the docstring formatter fetches the default
5397+
value from the converter if no 'py_default' is found.
5398+
The signature should have the default a='Hello!',
5399+
as given by the Custom converter.
5400+
[clinic start generated code]*/
5401+
5402+
PyDoc_STRVAR(docstr_fallback_to_converter_default__doc__,
5403+
"docstr_fallback_to_converter_default($module, /, a=\'Hello!\')\n"
5404+
"--\n"
5405+
"\n"
5406+
"Check docstring default value fallback.\n"
5407+
"\n"
5408+
"Verify that the docstring formatter fetches the default\n"
5409+
"value from the converter if no \'py_default\' is found.\n"
5410+
"The signature should have the default a=\'Hello!\',\n"
5411+
"as given by the Custom converter.");
5412+
5413+
#define DOCSTR_FALLBACK_TO_CONVERTER_DEFAULT_METHODDEF \
5414+
{"docstr_fallback_to_converter_default", _PyCFunction_CAST(docstr_fallback_to_converter_default), METH_FASTCALL|METH_KEYWORDS, docstr_fallback_to_converter_default__doc__},
5415+
5416+
static PyObject *
5417+
docstr_fallback_to_converter_default_impl(PyObject *module, str a);
5418+
5419+
static PyObject *
5420+
docstr_fallback_to_converter_default(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
5421+
{
5422+
PyObject *return_value = NULL;
5423+
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
5424+
5425+
#define NUM_KEYWORDS 1
5426+
static struct {
5427+
PyGC_Head _this_is_not_used;
5428+
PyObject_VAR_HEAD
5429+
PyObject *ob_item[NUM_KEYWORDS];
5430+
} _kwtuple = {
5431+
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
5432+
.ob_item = { &_Py_ID(a), },
5433+
};
5434+
#undef NUM_KEYWORDS
5435+
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
5436+
5437+
#else // !Py_BUILD_CORE
5438+
# define KWTUPLE NULL
5439+
#endif // !Py_BUILD_CORE
5440+
5441+
static const char * const _keywords[] = {"a", NULL};
5442+
static _PyArg_Parser _parser = {
5443+
.keywords = _keywords,
5444+
.fname = "docstr_fallback_to_converter_default",
5445+
.kwtuple = KWTUPLE,
5446+
};
5447+
#undef KWTUPLE
5448+
PyObject *argsbuf[1];
5449+
str a;
5450+
5451+
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
5452+
if (!args) {
5453+
goto exit;
5454+
}
5455+
if (!c_converter_func(args[0], &a)) {
5456+
goto exit;
5457+
}
5458+
return_value = docstr_fallback_to_converter_default_impl(module, a);
5459+
5460+
exit:
5461+
return return_value;
5462+
}
5463+
5464+
static PyObject *
5465+
docstr_fallback_to_converter_default_impl(PyObject *module, str a)
5466+
/*[clinic end generated code: output=ae24a9c6f60ee8a6 input=0cbe6a4d24bc2274]*/

0 commit comments

Comments
 (0)