diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 51e3542ff..fec040716 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -2349,8 +2349,6 @@ class AttributeErrorTests(unittest.TestCase): class ImportErrorTests(unittest.TestCase): - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_attributes(self): # Setting 'name' and 'path' should not be a problem. exc = ImportError('test') @@ -2385,8 +2383,6 @@ class ImportErrorTests(unittest.TestCase): with self.assertRaisesRegex(TypeError, msg): ImportError('test', invalid='keyword', another=True) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_reset_attributes(self): exc = ImportError('test', name='name', path='path') self.assertEqual(exc.args, ('test',)) diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index fb6243cbb..3c822c430 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -1308,17 +1308,21 @@ pub(super) mod types { args: ::rustpython_vm::function::FuncArgs, vm: &::rustpython_vm::VirtualMachine, ) -> ::rustpython_vm::PyResult<()> { - zelf.set_attr( - "name", - vm.unwrap_or_none(args.kwargs.get("name").cloned()), - vm, - )?; - zelf.set_attr( - "path", - vm.unwrap_or_none(args.kwargs.get("path").cloned()), - vm, - )?; - Ok(()) + let mut kwargs = args.kwargs.clone(); + let name = kwargs.swap_remove("name"); + let path = kwargs.swap_remove("path"); + + // Check for any remaining invalid keyword arguments + if let Some(invalid_key) = kwargs.keys().next() { + return Err(vm.new_type_error(format!( + "'{}' is an invalid keyword argument for ImportError", + invalid_key + ))); + } + + zelf.set_attr("name", vm.unwrap_or_none(name), vm)?; + zelf.set_attr("path", vm.unwrap_or_none(path), vm)?; + PyBaseException::slot_init(zelf, args, vm) } #[pymethod(magic)] fn reduce(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyTupleRef {