@@ -933,11 +933,11 @@ def _parse_vararg(self, *, fastcall: bool) -> str:
933933 fastcall = fastcall ,
934934 limited_capi = self .limited_capi )
935935
936- def _parse_kwarg (self ) -> str :
936+ def _parse_kwarg (self , * , stackargs : bool ) -> str :
937937 assert self .var_keyword is not None
938938 c = self .var_keyword .converter
939939 assert isinstance (c , libclinic .converters .VarKeywordCConverter )
940- return c .parse_var_keyword ()
940+ return c .parse_var_keyword (stackargs = stackargs )
941941
942942 def _check_positional (self , nargs : str , * ,
943943 indent : int = 4 ) -> list [str ]:
@@ -967,9 +967,8 @@ def _parse_positional_args(
967967 ) -> list [str ] | None :
968968 """Emit per-parameter positional argument parsing.
969969
970- Shared by parse_pos_only() and the vectorcall paths. Returns the
971- code snippets, or None if a converter doesn't support parse_arg
972- (the caller must fall back to a tuple/stack parser).
970+ Returns the code snippets, or None if a converter doesn't support
971+ parse_arg (the caller must fall back to a tuple/stack parser).
973972 """
974973 parser_code : list [str ] = []
975974 for i , p in enumerate (self .parameters ):
@@ -998,9 +997,9 @@ def _parse_positional_args(
998997 if self .varpos :
999998 parser_code .append (libclinic .normalize_snippet (
1000999 self ._parse_vararg (fastcall = fastcall ), indent = 4 ))
1001- elif self .var_keyword :
1002- parser_code .append (libclinic .normalize_snippet (self . _parse_kwarg (),
1003- indent = 4 ))
1000+ if self .var_keyword :
1001+ parser_code .append (libclinic .normalize_snippet (
1002+ self . _parse_kwarg ( stackargs = fastcall ), indent = 4 ))
10041003 return parser_code
10051004
10061005 def select_positional_convention (self ) -> tuple [str , str ]:
@@ -1112,48 +1111,38 @@ def parse_pos_only(self) -> None:
11121111 """ , indent = 4 ))
11131112 self .parser_body (* parser_code )
11141113
1115- def parse_var_keyword (self ) -> None :
1114+ def parse_var_keyword (self , * , vectorcall : bool = False ) -> None :
11161115 self .flags = "METH_VARARGS|METH_KEYWORDS"
1117- self .parser_prototype = PARSER_PROTOTYPE_KEYWORD
1118- nargs = 'PyTuple_GET_SIZE(args)'
1116+ # The vectorcall gets an array of arguments and kwnames, not a tuple
1117+ # and a dict: use the converters' fastcall convention, which handles
1118+ # that.
1119+ if vectorcall :
1120+ nargs , argname_fmt = 'nargs' , 'args[%d]'
1121+ check , arg = '_PyArg_NoPositionalStack' , 'nargs'
1122+ parser_code = self ._vectorcall_type_check ()
1123+ else :
1124+ self .parser_prototype = PARSER_PROTOTYPE_KEYWORD
1125+ nargs , argname_fmt = 'PyTuple_GET_SIZE(args)' , 'PyTuple_GET_ITEM(args, %d)'
1126+ check , arg = '_PyArg_NoPositional' , 'args'
1127+ parser_code = []
11191128
1120- parser_code = []
11211129 if self .varpos is None and self .min_pos == self .max_pos == 0 :
1122- self .codegen .add_include ('pycore_modsupport.h' ,
1123- '_PyArg_NoPositional()' )
1124- parser_code .append (libclinic .normalize_snippet ("""
1125- if (!_PyArg_NoPositional("{name}", args)) {{
1130+ self .codegen .add_include ('pycore_modsupport.h' , f'{ check } ()' )
1131+ parser_code .append (libclinic .normalize_snippet (f"""
1132+ if (!{ check } ("{{name}}", { arg } )) {{{{
11261133 goto exit;
1127- }}
1134+ }}}}
11281135 """ , indent = 4 ))
11291136 else :
11301137 parser_code .extend (self ._check_positional (nargs ))
11311138
1132- has_optional = False
1133- for i , p in enumerate (self .parameters ):
1134- parse_arg = p .converter .parse_arg (
1135- f'PyTuple_GET_ITEM(args, { i } )' ,
1136- p .get_displayname (i + 1 ),
1137- limited_capi = self .limited_capi ,
1138- )
1139- assert parse_arg is not None
1140- if has_optional or p .is_optional ():
1141- has_optional = True
1142- parser_code .append (libclinic .normalize_snippet ("""
1143- if (%s < %d) {{
1144- goto skip_optional;
1145- }}
1146- """ , indent = 4 ) % (nargs , i + 1 ))
1147- parser_code .append (libclinic .normalize_snippet (parse_arg , indent = 4 ))
1148- if has_optional :
1149- parser_code .append ("skip_optional:" )
1150-
1151- if self .varpos :
1152- parser_code .append (libclinic .normalize_snippet (
1153- self ._parse_vararg (fastcall = self .fastcall ), indent = 4 ))
1154- if self .var_keyword :
1155- parser_code .append (libclinic .normalize_snippet (self ._parse_kwarg (), indent = 4 ))
1156- self .parser_body (* parser_code )
1139+ pos_code = self ._parse_positional_args (
1140+ argname_fmt = argname_fmt , nargs = nargs ,
1141+ limited_capi = self .limited_capi , fastcall = vectorcall )
1142+ assert pos_code is not None
1143+ parser_code .extend (pos_code )
1144+ body = self .vectorcall_body if vectorcall else self .parser_body
1145+ body (* parser_code )
11571146
11581147 def use_converters (self ) -> None :
11591148 """Prepare for parsing all arguments by a single call.
@@ -1456,7 +1445,7 @@ def handle_new_or_init(self) -> None:
14561445 if self .func .kind is METHOD_INIT :
14571446 self .return_value_declaration = "int {parser_retval} = -1;"
14581447
1459- if self .func . vectorcall and 'METH_KEYWORDS' in self . flags :
1448+ if self .parser_prototype == PARSER_PROTOTYPE_KEYWORD_HELPER :
14601449 self ._new_or_init_delegate_to_helper ()
14611450 else :
14621451 self ._new_or_init_parser_body ()
@@ -1624,8 +1613,6 @@ def _vectorcall_positional(self, *,
16241613
16251614 arity_checked: Already have a number of arguments check.
16261615 """
1627- # The vectorcall gets an array of arguments, not a tuple: use the
1628- # converters' fastcall convention, which handles that.
16291616 pos_code = self ._parse_positional_args (
16301617 argname_fmt = 'args[%d]' , nargs = 'nargs' , limited_capi = False ,
16311618 fastcall = True )
@@ -1781,7 +1768,7 @@ def parse_vectorcall(self) -> None:
17811768 if not self .parameters and not self .varpos and not self .var_keyword :
17821769 self .parse_vectorcall_no_args ()
17831770 elif self .var_keyword is not None :
1784- self .parse_vectorcall_kw_required ( )
1771+ self .parse_var_keyword ( vectorcall = True )
17851772 elif self .pos_only == len (self .parameters ):
17861773 self .parse_vectorcall_pos_only ()
17871774 elif any (p .is_keyword_only () for p in self .parameters ) or self .varpos :
0 commit comments