Skip to content

Commit 832ffad

Browse files
committed
Do not add OSError attributes when needed argument not supplied
1 parent 053c096 commit 832ffad

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

‎vm/src/exceptions.rs‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,23 +1234,26 @@ pub(super) mod types {
12341234
args: &FuncArgs,
12351235
vm: &VirtualMachine,
12361236
) -> PyResult<()> {
1237-
let exc_self = exc_self.as_ref();
1238-
vm.set_attr(
1239-
exc_self,
1240-
"filename",
1241-
vm.unwrap_or_none(args.args.as_slice().get(2).cloned()),
1242-
)?;
1237+
os_error_set_attr(&exc_self, args, vm, "filename", 2)?;
12431238
#[cfg(windows)]
1244-
vm.set_attr(
1245-
exc_self,
1246-
"winerror",
1247-
vm.unwrap_or_none(args.args.as_slice().get(3).cloned()),
1248-
)?;
1249-
vm.set_attr(
1250-
exc_self,
1251-
"filename2",
1252-
vm.unwrap_or_none(args.args.as_slice().get(4).cloned()),
1253-
)?;
1239+
os_error_set_attr(&exc_self, args, vm, "winerror", 3)?;
1240+
os_error_set_attr(&exc_self, args, vm, "filename2", 4)?;
1241+
Ok(())
1242+
}
1243+
1244+
fn os_error_set_attr(
1245+
exc_self: &PyBaseExceptionRef,
1246+
args: &FuncArgs,
1247+
vm: &VirtualMachine,
1248+
attr_name: &str,
1249+
arg_idx: usize,
1250+
) -> PyResult<()> {
1251+
let exc_self = exc_self.as_ref();
1252+
1253+
if let Some(arg) = args.args.as_slice().get(arg_idx).cloned() {
1254+
vm.set_attr(exc_self, attr_name, arg)?;
1255+
}
1256+
12541257
Ok(())
12551258
}
12561259

0 commit comments

Comments
 (0)