Skip to content

Commit e4c8f2b

Browse files
committed
Fix ImportError
1 parent d78ed67 commit e4c8f2b

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

Lib/test/test_exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,8 +2349,6 @@ def __getattribute__(self, attr):
23492349

23502350

23512351
class ImportErrorTests(unittest.TestCase):
2352-
# TODO: RUSTPYTHON
2353-
@unittest.expectedFailure
23542352
def test_attributes(self):
23552353
# Setting 'name' and 'path' should not be a problem.
23562354
exc = ImportError('test')
@@ -2385,8 +2383,6 @@ def test_attributes(self):
23852383
with self.assertRaisesRegex(TypeError, msg):
23862384
ImportError('test', invalid='keyword', another=True)
23872385

2388-
# TODO: RUSTPYTHON
2389-
@unittest.expectedFailure
23902386
def test_reset_attributes(self):
23912387
exc = ImportError('test', name='name', path='path')
23922388
self.assertEqual(exc.args, ('test',))

vm/src/exceptions.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,17 +1308,21 @@ pub(super) mod types {
13081308
args: ::rustpython_vm::function::FuncArgs,
13091309
vm: &::rustpython_vm::VirtualMachine,
13101310
) -> ::rustpython_vm::PyResult<()> {
1311-
zelf.set_attr(
1312-
"name",
1313-
vm.unwrap_or_none(args.kwargs.get("name").cloned()),
1314-
vm,
1315-
)?;
1316-
zelf.set_attr(
1317-
"path",
1318-
vm.unwrap_or_none(args.kwargs.get("path").cloned()),
1319-
vm,
1320-
)?;
1321-
Ok(())
1311+
let mut kwargs = args.kwargs.clone();
1312+
let name = kwargs.swap_remove("name");
1313+
let path = kwargs.swap_remove("path");
1314+
1315+
// Check for any remaining invalid keyword arguments
1316+
if let Some(invalid_key) = kwargs.keys().next() {
1317+
return Err(vm.new_type_error(format!(
1318+
"'{}' is an invalid keyword argument for ImportError",
1319+
invalid_key
1320+
)));
1321+
}
1322+
1323+
zelf.set_attr("name", vm.unwrap_or_none(name), vm)?;
1324+
zelf.set_attr("path", vm.unwrap_or_none(path), vm)?;
1325+
PyBaseException::slot_init(zelf, args, vm)
13221326
}
13231327
#[pymethod(magic)]
13241328
fn reduce(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyTupleRef {

0 commit comments

Comments
 (0)