@@ -129,6 +129,13 @@ impl PyFuncArgs {
129
129
given_args,
130
130
) ) ) ;
131
131
}
132
+ Err ( ArgumentError :: TooManyArgs ) => {
133
+ return Err ( vm. new_type_error ( format ! (
134
+ "Expected at most {} arguments ({} given)" ,
135
+ T :: arity( ) . end( ) ,
136
+ given_args,
137
+ ) ) ) ;
138
+ }
132
139
Err ( ArgumentError :: Exception ( ex) ) => {
133
140
return Err ( ex) ;
134
141
}
@@ -153,8 +160,15 @@ pub enum PyArg {
153
160
Keyword ( String , PyObjectRef ) ,
154
161
}
155
162
163
+ /// An error encountered while binding arguments to the parameters of a Python
164
+ /// function call.
156
165
pub enum ArgumentError {
166
+ /// The call provided fewer positional arguments than the function requires.
157
167
TooFewArgs ,
168
+ /// The call provided more positional arguments than the function accepts.
169
+ TooManyArgs ,
170
+ /// An exception was raised while binding arguments to the function
171
+ /// parameters.
158
172
Exception ( PyObjectRef ) ,
159
173
}
160
174
@@ -205,10 +219,19 @@ where
205
219
I : Iterator < Item = PyArg > ,
206
220
{
207
221
let mut kwargs = HashMap :: new ( ) ;
208
- while let Some ( PyArg :: Keyword ( name, value) ) = args. next ( ) {
209
- kwargs. insert ( name, T :: try_from_object ( vm, value) ?) ;
222
+ loop {
223
+ match args. next ( ) {
224
+ Some ( PyArg :: Keyword ( name, value) ) => {
225
+ kwargs. insert ( name, T :: try_from_object ( vm, value) ?) ;
226
+ }
227
+ Some ( PyArg :: Positional ( _) ) => {
228
+ return Err ( ArgumentError :: TooManyArgs ) ;
229
+ }
230
+ None => {
231
+ return Ok ( KwArgs ( kwargs) ) ;
232
+ }
233
+ }
210
234
}
211
- Ok ( KwArgs ( kwargs) )
212
235
}
213
236
}
214
237
0 commit comments