Skip to content

Commit 88d5928

Browse files
committed
Simplify _check_positional callsites
Move repeated check inside _check_positional
1 parent c2b8174 commit 88d5928

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

Tools/clinic/libclinic/parse_args.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,23 @@ def _parse_kwarg(self) -> str:
587587
assert isinstance(c, libclinic.converters.VarKeywordCConverter)
588588
return c.parse_var_keyword()
589589

590-
def _check_positional(self, nargs: str, *, indent: int = 4) -> str:
590+
def _check_positional(self, nargs: str, *,
591+
indent: int = 4) -> list[str]:
592+
"""Emit an argument count check when needed.
593+
594+
Varpos functions have no upper bound but still need a check when a
595+
minimum number of positional arguments are required.
596+
"""
597+
max_args = NO_VARARG if self.varpos else self.max_pos
598+
if not self.min_pos and max_args == NO_VARARG:
599+
return []
591600
self.codegen.add_include('pycore_modsupport.h',
592601
'_PyArg_CheckPositional()')
593-
max_args = NO_VARARG if self.varpos else self.max_pos
594-
return libclinic.normalize_snippet(f"""
602+
return [libclinic.normalize_snippet(f"""
595603
if (!_PyArg_CheckPositional("{{name}}", {nargs}, {self.min_pos}, {max_args})) {{{{
596604
goto exit;
597605
}}}}
598-
""", indent=indent)
606+
""", indent=indent)]
599607

600608
def _parse_positional_args(
601609
self,
@@ -694,8 +702,8 @@ def parse_pos_only(self) -> None:
694702
}}}}
695703
""",
696704
indent=4))
697-
elif self.min_pos or max_args != NO_VARARG:
698-
parser_code.append(self._check_positional(nargs))
705+
else:
706+
parser_code.extend(self._check_positional(nargs))
699707

700708
pos_code = self._parse_positional_args(
701709
argname_fmt=argname_fmt, nargs=nargs,
@@ -734,7 +742,6 @@ def parse_var_keyword(self) -> None:
734742
nargs = 'PyTuple_GET_SIZE(args)'
735743

736744
parser_code = []
737-
max_args = NO_VARARG if self.varpos else self.max_pos
738745
if self.varpos is None and self.min_pos == self.max_pos == 0:
739746
self.codegen.add_include('pycore_modsupport.h',
740747
'_PyArg_NoPositional()')
@@ -743,8 +750,8 @@ def parse_var_keyword(self) -> None:
743750
goto exit;
744751
}}
745752
""", indent=4))
746-
elif self.min_pos or max_args != NO_VARARG:
747-
parser_code.append(self._check_positional(nargs))
753+
else:
754+
parser_code.extend(self._check_positional(nargs))
748755

749756
has_optional = False
750757
for i, p in enumerate(self.parameters):
@@ -1163,10 +1170,7 @@ def _vectorcall_positional(self, *,
11631170
assert pos_code is not None
11641171
if arity_checked:
11651172
return pos_code
1166-
# varpos allows arbitrary length; still needs a check if there is a min.
1167-
if self.min_pos or not self.varpos:
1168-
return [self._check_positional('nargs', indent=4), *pos_code]
1169-
return pos_code
1173+
return [*self._check_positional('nargs'), *pos_code]
11701174

11711175
def _assemble_vectorcall(self, preamble: str, fields: tuple[str, ...],
11721176
finale: str) -> None:

0 commit comments

Comments
 (0)