Skip to content

Commit 0937b11

Browse files
authored
gh-110964: clinic: refactor output_templates() (#110982)
1 parent 7162c3a commit 0937b11

File tree

1 file changed

+53
-56
lines changed

1 file changed

+53
-56
lines changed

Tools/clinic/clinic.py

+53-56
Original file line numberDiff line numberDiff line change
@@ -1432,67 +1432,64 @@ def parser_body(
14321432
deprecated_keywords[i] = p
14331433

14341434
has_optional_kw = (max(pos_only, min_pos) + min_kw_only < len(converters) - int(vararg != NO_VARARG))
1435-
if vararg == NO_VARARG:
1436-
# FIXME: refactor the code to not declare args_declaration
1437-
# if limited_capi is true
1438-
if not limited_capi:
1439-
clinic.add_include('pycore_modsupport.h',
1440-
'_PyArg_UnpackKeywords()')
1441-
args_declaration = "_PyArg_UnpackKeywords", "%s, %s, %s" % (
1442-
min_pos,
1443-
max_pos,
1444-
min_kw_only
1445-
)
1446-
nargs = "nargs"
1447-
else:
1448-
if not limited_capi:
1449-
clinic.add_include('pycore_modsupport.h',
1450-
'_PyArg_UnpackKeywordsWithVararg()')
1451-
args_declaration = "_PyArg_UnpackKeywordsWithVararg", "%s, %s, %s, %s" % (
1452-
min_pos,
1453-
max_pos,
1454-
min_kw_only,
1455-
vararg
1456-
)
1457-
nargs = f"Py_MIN(nargs, {max_pos})" if max_pos else "0"
14581435

14591436
if limited_capi:
14601437
parser_code = None
14611438
fastcall = False
1462-
1463-
elif fastcall:
1464-
flags = "METH_FASTCALL|METH_KEYWORDS"
1465-
parser_prototype = self.PARSER_PROTOTYPE_FASTCALL_KEYWORDS
1466-
argname_fmt = 'args[%d]'
1467-
declarations = declare_parser(f, clinic=clinic,
1468-
limited_capi=clinic.limited_capi)
1469-
declarations += "\nPyObject *argsbuf[%s];" % len(converters)
1470-
if has_optional_kw:
1471-
declarations += "\nPy_ssize_t noptargs = %s + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - %d;" % (nargs, min_pos + min_kw_only)
1472-
parser_code = [normalize_snippet("""
1473-
args = %s(args, nargs, NULL, kwnames, &_parser, %s, argsbuf);
1474-
if (!args) {{
1475-
goto exit;
1476-
}}
1477-
""" % args_declaration, indent=4)]
14781439
else:
1479-
# positional-or-keyword arguments
1480-
flags = "METH_VARARGS|METH_KEYWORDS"
1481-
parser_prototype = self.PARSER_PROTOTYPE_KEYWORD
1482-
argname_fmt = 'fastargs[%d]'
1483-
declarations = declare_parser(f, clinic=clinic,
1484-
limited_capi=clinic.limited_capi)
1485-
declarations += "\nPyObject *argsbuf[%s];" % len(converters)
1486-
declarations += "\nPyObject * const *fastargs;"
1487-
declarations += "\nPy_ssize_t nargs = PyTuple_GET_SIZE(args);"
1488-
if has_optional_kw:
1489-
declarations += "\nPy_ssize_t noptargs = %s + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - %d;" % (nargs, min_pos + min_kw_only)
1490-
parser_code = [normalize_snippet("""
1491-
fastargs = %s(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, %s, argsbuf);
1492-
if (!fastargs) {{
1493-
goto exit;
1494-
}}
1495-
""" % args_declaration, indent=4)]
1440+
if vararg == NO_VARARG:
1441+
clinic.add_include('pycore_modsupport.h',
1442+
'_PyArg_UnpackKeywords()')
1443+
args_declaration = "_PyArg_UnpackKeywords", "%s, %s, %s" % (
1444+
min_pos,
1445+
max_pos,
1446+
min_kw_only
1447+
)
1448+
nargs = "nargs"
1449+
else:
1450+
clinic.add_include('pycore_modsupport.h',
1451+
'_PyArg_UnpackKeywordsWithVararg()')
1452+
args_declaration = "_PyArg_UnpackKeywordsWithVararg", "%s, %s, %s, %s" % (
1453+
min_pos,
1454+
max_pos,
1455+
min_kw_only,
1456+
vararg
1457+
)
1458+
nargs = f"Py_MIN(nargs, {max_pos})" if max_pos else "0"
1459+
1460+
if fastcall:
1461+
flags = "METH_FASTCALL|METH_KEYWORDS"
1462+
parser_prototype = self.PARSER_PROTOTYPE_FASTCALL_KEYWORDS
1463+
argname_fmt = 'args[%d]'
1464+
declarations = declare_parser(f, clinic=clinic,
1465+
limited_capi=clinic.limited_capi)
1466+
declarations += "\nPyObject *argsbuf[%s];" % len(converters)
1467+
if has_optional_kw:
1468+
declarations += "\nPy_ssize_t noptargs = %s + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - %d;" % (nargs, min_pos + min_kw_only)
1469+
parser_code = [normalize_snippet("""
1470+
args = %s(args, nargs, NULL, kwnames, &_parser, %s, argsbuf);
1471+
if (!args) {{
1472+
goto exit;
1473+
}}
1474+
""" % args_declaration, indent=4)]
1475+
else:
1476+
# positional-or-keyword arguments
1477+
flags = "METH_VARARGS|METH_KEYWORDS"
1478+
parser_prototype = self.PARSER_PROTOTYPE_KEYWORD
1479+
argname_fmt = 'fastargs[%d]'
1480+
declarations = declare_parser(f, clinic=clinic,
1481+
limited_capi=clinic.limited_capi)
1482+
declarations += "\nPyObject *argsbuf[%s];" % len(converters)
1483+
declarations += "\nPyObject * const *fastargs;"
1484+
declarations += "\nPy_ssize_t nargs = PyTuple_GET_SIZE(args);"
1485+
if has_optional_kw:
1486+
declarations += "\nPy_ssize_t noptargs = %s + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - %d;" % (nargs, min_pos + min_kw_only)
1487+
parser_code = [normalize_snippet("""
1488+
fastargs = %s(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, %s, argsbuf);
1489+
if (!fastargs) {{
1490+
goto exit;
1491+
}}
1492+
""" % args_declaration, indent=4)]
14961493

14971494
if requires_defining_class:
14981495
flags = 'METH_METHOD|' + flags

0 commit comments

Comments
 (0)