Skip to content

Commit 3ae6ce5

Browse files
authored
Implemented __reduce__ method for the ImportError exception (RustPython#4973)
1 parent f5fc30a commit 3ae6ce5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

vm/src/exceptions.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,20 @@ pub(super) mod types {
11821182
)?;
11831183
Ok(())
11841184
}
1185+
#[pymethod(magic)]
1186+
fn reduce(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyTupleRef {
1187+
let obj = exc.as_object().to_owned();
1188+
let mut result: Vec<PyObjectRef> = vec![
1189+
obj.class().to_owned().into(),
1190+
vm.new_tuple((exc.get_arg(0).unwrap(),)).into(),
1191+
];
1192+
1193+
if let Some(dict) = obj.dict().filter(|x| !x.is_empty()) {
1194+
result.push(dict.into());
1195+
}
1196+
1197+
result.into_pytuple(vm)
1198+
}
11851199
}
11861200

11871201
#[pyexception(name, base = "PyImportError", ctx = "module_not_found_error", impl)]

0 commit comments

Comments
 (0)