From 15e6187583533dbf2893c2dc7390b4e753ae1f51 Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Wed, 27 Feb 2019 11:04:16 +0400 Subject: [PATCH] add overflow errors for int shifts. --- tests/snippets/ints.py | 3 +++ vm/src/obj/objint.rs | 6 ++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/snippets/ints.py b/tests/snippets/ints.py index 9aa83eba4..3bf91491d 100644 --- a/tests/snippets/ints.py +++ b/tests/snippets/ints.py @@ -1,3 +1,5 @@ +from testutils import assert_raises + # int to int comparisons assert 1 == 1 @@ -36,6 +38,7 @@ assert (2).__rtruediv__(1) == 0.5 assert (1).real == 1 assert (1).imag == 0 +assert_raises(OverflowError, lambda: 1 << 10 ** 100000) assert (1).__eq__(1.0) == NotImplemented assert (1).__ne__(1.0) == NotImplemented diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 9f5e48893..018e995fe 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -246,8 +246,7 @@ fn int_lshift(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { match get_value(i2) { ref v if *v < BigInt::zero() => Err(vm.new_value_error("negative shift count".to_string())), ref v if *v > BigInt::from(usize::max_value()) => { - // TODO: raise OverflowError - panic!("Failed converting {} to rust usize", get_value(i2)); + Err(vm.new_overflow_error("the number is too large to convert to float".to_string())) } _ => panic!("Failed converting {} to rust usize", get_value(i2)), } @@ -276,8 +275,7 @@ fn int_rshift(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { match get_value(i2) { ref v if *v < BigInt::zero() => Err(vm.new_value_error("negative shift count".to_string())), ref v if *v > BigInt::from(usize::max_value()) => { - // TODO: raise OverflowError - panic!("Failed converting {} to rust usize", get_value(i2)); + Err(vm.new_overflow_error("the number is too large to convert to float".to_string())) } _ => panic!("Failed converting {} to rust usize", get_value(i2)), }