Skip to content

Commit a98288b

Browse files
committed
Add overflow handling logic
1 parent c8d0360 commit a98288b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

vm/src/builtins/int.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,16 @@ fn inner_truediv(i1: &BigInt, i2: &BigInt, vm: &VirtualMachine) -> PyResult {
213213
return Err(vm.new_zero_division_error("division by zero".to_owned()));
214214
}
215215

216-
Ok(vm
217-
.ctx
218-
.new_float(Ratio::from(i1.clone()).div(i2).to_f64().unwrap())
219-
.into())
216+
let float = Ratio::from(i1.clone()).div(i2).to_f64().unwrap();
217+
218+
if float.is_infinite() {
219+
Err(vm.new_exception_msg(
220+
vm.ctx.exceptions.overflow_error.to_owned(),
221+
"integer division result too large for a float".to_owned(),
222+
))
223+
} else {
224+
Ok(vm.ctx.new_float(float).into())
225+
}
220226
}
221227

222228
impl Constructor for PyInt {

0 commit comments

Comments
 (0)