Implemented __reduce__ method for the ImportError exception (#4973)

This commit is contained in:
Luca Sforza
2023-05-15 13:51:20 +02:00
committed by GitHub
parent f5fc30ae63
commit 3ae6ce5216

View File

@@ -1182,6 +1182,20 @@ pub(super) mod types {
)?;
Ok(())
}
#[pymethod(magic)]
fn reduce(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyTupleRef {
let obj = exc.as_object().to_owned();
let mut result: Vec<PyObjectRef> = vec![
obj.class().to_owned().into(),
vm.new_tuple((exc.get_arg(0).unwrap(),)).into(),
];
if let Some(dict) = obj.dict().filter(|x| !x.is_empty()) {
result.push(dict.into());
}
result.into_pytuple(vm)
}
}
#[pyexception(name, base = "PyImportError", ctx = "module_not_found_error", impl)]