Store OSError errno in the args tuple

This commit is contained in:
Noah
2021-01-27 21:05:45 -06:00
committed by coolreader18
parent aa238de4a9
commit 2e831f5daa
2 changed files with 18 additions and 12 deletions

View File

@@ -672,6 +672,14 @@ impl ExceptionZoo {
"__str__" => ctx.new_method("__str__", key_error_str),
});
extend_class!(ctx, &excs.os_error, {
"errno" => ctx.new_readonly_getset("errno", |exc: PyBaseExceptionRef| {
let args = exc.args();
let args = args.borrow_value();
args.get(0).filter(|_| args.len() > 1).cloned()
}),
});
extend_class!(ctx, &excs.unicode_decode_error, {
"encoding" => ctx.new_readonly_getset("encoding", make_arg_getter(0)),
"object" => ctx.new_readonly_getset("object", make_arg_getter(1)),

View File

@@ -191,17 +191,16 @@ impl IntoPyException for &'_ io::Error {
_ => vm.ctx.exceptions.os_error.clone(),
},
};
let os_error = vm.new_exception_msg(exc_type, self.to_string());
let errno = self.raw_os_error().into_pyobject(vm);
vm.set_attr(os_error.as_object(), "errno", errno).unwrap();
os_error
let msg = vm.ctx.new_str(self.to_string());
vm.new_exception(exc_type, vec![errno, msg])
}
}
#[cfg(unix)]
impl IntoPyException for nix::Error {
fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
let nix_error = match self {
match self {
nix::Error::InvalidPath => {
let exc_type = vm.ctx.exceptions.file_not_found_error.clone();
vm.new_exception_msg(exc_type, self.to_string())
@@ -213,16 +212,15 @@ impl IntoPyException for nix::Error {
nix::Error::UnsupportedOperation => vm.new_runtime_error(self.to_string()),
nix::Error::Sys(errno) => {
let exc_type = posix::convert_nix_errno(vm, errno);
vm.new_exception_msg(exc_type, self.to_string())
vm.new_exception(
exc_type,
vec![
vm.ctx.new_int(errno as i32),
vm.ctx.new_str(self.to_string()),
],
)
}
};
if let nix::Error::Sys(errno) = self {
vm.set_attr(nix_error.as_object(), "errno", vm.ctx.new_int(errno as i32))
.unwrap();
}
nix_error
}
}