Skip to content

Commit 3242ff3

Browse files
Merge pull request RustPython#690 from RustPython/joey/kwargs-surprise-positional
Handle unexpected positional argument in KwArgs
2 parents a32ebd5 + 60ed1bd commit 3242ff3

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

vm/src/function.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ impl PyFuncArgs {
129129
given_args,
130130
)));
131131
}
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+
}
132139
Err(ArgumentError::Exception(ex)) => {
133140
return Err(ex);
134141
}
@@ -153,8 +160,15 @@ pub enum PyArg {
153160
Keyword(String, PyObjectRef),
154161
}
155162

163+
/// An error encountered while binding arguments to the parameters of a Python
164+
/// function call.
156165
pub enum ArgumentError {
166+
/// The call provided fewer positional arguments than the function requires.
157167
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.
158172
Exception(PyObjectRef),
159173
}
160174

@@ -205,10 +219,19 @@ where
205219
I: Iterator<Item = PyArg>,
206220
{
207221
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+
}
210234
}
211-
Ok(KwArgs(kwargs))
212235
}
213236
}
214237

0 commit comments

Comments
 (0)