Skip to content

Commit 15e6187

Browse files
add overflow errors for int shifts.
1 parent d6242ac commit 15e6187

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

tests/snippets/ints.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from testutils import assert_raises
2+
13
# int to int comparisons
24

35
assert 1 == 1
@@ -36,6 +38,7 @@
3638
assert (1).real == 1
3739
assert (1).imag == 0
3840

41+
assert_raises(OverflowError, lambda: 1 << 10 ** 100000)
3942

4043
assert (1).__eq__(1.0) == NotImplemented
4144
assert (1).__ne__(1.0) == NotImplemented

vm/src/obj/objint.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ fn int_lshift(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
246246
match get_value(i2) {
247247
ref v if *v < BigInt::zero() => Err(vm.new_value_error("negative shift count".to_string())),
248248
ref v if *v > BigInt::from(usize::max_value()) => {
249-
// TODO: raise OverflowError
250-
panic!("Failed converting {} to rust usize", get_value(i2));
249+
Err(vm.new_overflow_error("the number is too large to convert to float".to_string()))
251250
}
252251
_ => panic!("Failed converting {} to rust usize", get_value(i2)),
253252
}
@@ -276,8 +275,7 @@ fn int_rshift(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
276275
match get_value(i2) {
277276
ref v if *v < BigInt::zero() => Err(vm.new_value_error("negative shift count".to_string())),
278277
ref v if *v > BigInt::from(usize::max_value()) => {
279-
// TODO: raise OverflowError
280-
panic!("Failed converting {} to rust usize", get_value(i2));
278+
Err(vm.new_overflow_error("the number is too large to convert to float".to_string()))
281279
}
282280
_ => panic!("Failed converting {} to rust usize", get_value(i2)),
283281
}

0 commit comments

Comments
 (0)