Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions crates/vm/src/builtins/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,9 @@ pub fn try_to_bigint(value: f64, vm: &VirtualMachine) -> PyResult<BigInt> {
Some(int) => Ok(int),
None => {
if value.is_infinite() {
Err(vm
.new_overflow_error("OverflowError: cannot convert float infinity to integer"))
Err(vm.new_overflow_error("cannot convert float infinity to integer"))
} else if value.is_nan() {
Err(vm.new_value_error("ValueError: cannot convert float NaN to integer"))
Err(vm.new_value_error("cannot convert float NaN to integer"))
} else {
// unreachable unless BigInt has a bug
unreachable!(
Expand Down
32 changes: 32 additions & 0 deletions extra_tests/snippets/builtin_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,35 @@ def identical(x, y):

assert 3.0 % -2.0 == -1.0
assert -3.0 % 2.0 == 1.0


# Float-to-int conversion error wording matches CPython exactly. The
# error class name is added by Python's exception display layer; the
# message itself must not duplicate it.
def _check_msg(call, exc_type, expected_msg):
try:
call()
except exc_type as e:
assert str(e) == expected_msg, repr(e)
else:
raise AssertionError(f"expected {exc_type.__name__}")


_check_msg(lambda: int(INF), OverflowError, "cannot convert float infinity to integer")
_check_msg(lambda: int(NINF), OverflowError, "cannot convert float infinity to integer")
_check_msg(lambda: int(NAN), ValueError, "cannot convert float NaN to integer")
_check_msg(
lambda: round(INF), OverflowError, "cannot convert float infinity to integer"
)
_check_msg(lambda: round(NAN), ValueError, "cannot convert float NaN to integer")
_check_msg(
lambda: math.floor(INF), OverflowError, "cannot convert float infinity to integer"
)
_check_msg(lambda: math.ceil(NAN), ValueError, "cannot convert float NaN to integer")
_check_msg(
lambda: math.trunc(INF), OverflowError, "cannot convert float infinity to integer"
)
_check_msg(
lambda: INF.__int__(), OverflowError, "cannot convert float infinity to integer"
)
_check_msg(lambda: NAN.__floor__(), ValueError, "cannot convert float NaN to integer")
Loading