Skip to content

Commit b1cd7b6

Browse files
committed
remove unnecessary branch in keyword parsing helper
1 parent 61ada45 commit b1cd7b6

3 files changed

Lines changed: 48 additions & 71 deletions

File tree

Modules/clinic/_testclinic.c.h

Lines changed: 29 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/clinic/enumobject.c.h

Lines changed: 8 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/clinic/libclinic/parse_args.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ def declare_parser(
109109
PARSER_PROTOTYPE_KEYWORD_HELPER: Final[str] = libclinic.normalize_snippet("""
110110
static PyObject *
111111
{c_basename}_parse_args({self_type}{self_name}, PyObject *const *args,
112-
Py_ssize_t nargs, PyObject *kwargs, PyObject *kwnames)
112+
Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
113113
""")
114114
PARSER_PROTOTYPE_KEYWORD___INIT___HELPER: Final[str] = libclinic.normalize_snippet("""
115115
static int
116116
{c_basename}_parse_args({self_type}{self_name}, PyObject *const *args,
117-
Py_ssize_t nargs, PyObject *kwargs, PyObject *kwnames)
117+
Py_ssize_t nargs, Py_ssize_t nkw, PyObject *kwargs, PyObject *kwnames)
118118
""")
119119
PARSER_PROTOTYPE_VARARGS: Final[str] = libclinic.normalize_snippet("""
120120
static PyObject *
@@ -718,13 +718,6 @@ def parse_general(self, clang: CLanguage) -> None:
718718
self.declarations += "\nPyObject * const *fastargs;"
719719
if has_optional_kw:
720720
self.declarations += (
721-
"\nPy_ssize_t nkw = 0;"
722-
"\nif (kwnames != NULL) {{"
723-
"\n nkw = PyTuple_GET_SIZE(kwnames);"
724-
"\n}}"
725-
"\nelse if (kwargs != NULL) {{"
726-
"\n nkw = PyDict_GET_SIZE(kwargs);"
727-
"\n}}"
728721
"\nPy_ssize_t noptargs = %s + nkw - %d;"
729722
% (nargs, self.min_pos + self.min_kw_only))
730723
unpack_args = 'args, nargs, kwargs, kwnames'
@@ -912,7 +905,9 @@ def handle_new_or_init(self) -> None:
912905
'{{',
913906
' return {c_basename}_parse_args({self_name}, '
914907
'_PyTuple_CAST(args)->ob_item,',
915-
' PyTuple_GET_SIZE(args), kwargs, NULL);',
908+
' PyTuple_GET_SIZE(args),',
909+
' kwargs ? PyDict_GET_SIZE(kwargs) : 0,',
910+
' kwargs, NULL);',
916911
'}}',
917912
])
918913
return
@@ -1178,8 +1173,9 @@ def emit(text: str, indent: int = 4) -> None:
11781173
if (self == NULL) {{
11791174
return NULL;
11801175
}}
1181-
int _result = {c_basename}_parse_args(self,
1182-
args, nargs, NULL, kwnames);
1176+
int _result = {c_basename}_parse_args(self, args, nargs,
1177+
kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
1178+
NULL, kwnames);
11831179
if (_result != 0) {{
11841180
Py_DECREF(self);
11851181
return NULL;
@@ -1189,8 +1185,9 @@ def emit(text: str, indent: int = 4) -> None:
11891185
""")
11901186
else:
11911187
emit("""
1192-
return {c_basename}_parse_args(_PyType_CAST(type), args,
1193-
nargs, NULL, kwnames);
1188+
return {c_basename}_parse_args(_PyType_CAST(type), args, nargs,
1189+
kwnames ? PyTuple_GET_SIZE(kwnames) : 0,
1190+
NULL, kwnames);
11941191
""")
11951192

11961193
if emitted_fast_path:

0 commit comments

Comments
 (0)