Skip to content

Commit 181e4e7

Browse files
Drop redundant exception type prefix from float-to-int error messages (RustPython#7763)
CPython raises `OverflowError("cannot convert float infinity to integer")` and `ValueError("cannot convert float NaN to integer")` from `Objects/floatobject.c::float___trunc___impl` and friends. The exception type name is added by Python's traceback display layer; the message itself should not duplicate it. `try_to_bigint` was producing `OverflowError("OverflowError: cannot convert ...")` etc., which made `repr(e)` and any code path that inspects `str(e)` diverge from CPython. Affects all 5 callers of `try_to_bigint`: `__int__`, `__floor__`, `__ceil__`, `__round__` (no-arg), `__trunc__` — i.e. `int(x)`, `math.floor/ceil/trunc(x)`, `round(x)` for non-finite floats. Verified byte-identical with CPython 3.14.4 across 14 affected sites.
1 parent eb99a8e commit 181e4e7

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

crates/vm/src/builtins/float.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,9 @@ pub fn try_to_bigint(value: f64, vm: &VirtualMachine) -> PyResult<BigInt> {
131131
Some(int) => Ok(int),
132132
None => {
133133
if value.is_infinite() {
134-
Err(vm
135-
.new_overflow_error("OverflowError: cannot convert float infinity to integer"))
134+
Err(vm.new_overflow_error("cannot convert float infinity to integer"))
136135
} else if value.is_nan() {
137-
Err(vm.new_value_error("ValueError: cannot convert float NaN to integer"))
136+
Err(vm.new_value_error("cannot convert float NaN to integer"))
138137
} else {
139138
// unreachable unless BigInt has a bug
140139
unreachable!(

extra_tests/snippets/builtin_float.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,35 @@ def identical(x, y):
517517

518518
assert 3.0 % -2.0 == -1.0
519519
assert -3.0 % 2.0 == 1.0
520+
521+
522+
# Float-to-int conversion error wording matches CPython exactly. The
523+
# error class name is added by Python's exception display layer; the
524+
# message itself must not duplicate it.
525+
def _check_msg(call, exc_type, expected_msg):
526+
try:
527+
call()
528+
except exc_type as e:
529+
assert str(e) == expected_msg, repr(e)
530+
else:
531+
raise AssertionError(f"expected {exc_type.__name__}")
532+
533+
534+
_check_msg(lambda: int(INF), OverflowError, "cannot convert float infinity to integer")
535+
_check_msg(lambda: int(NINF), OverflowError, "cannot convert float infinity to integer")
536+
_check_msg(lambda: int(NAN), ValueError, "cannot convert float NaN to integer")
537+
_check_msg(
538+
lambda: round(INF), OverflowError, "cannot convert float infinity to integer"
539+
)
540+
_check_msg(lambda: round(NAN), ValueError, "cannot convert float NaN to integer")
541+
_check_msg(
542+
lambda: math.floor(INF), OverflowError, "cannot convert float infinity to integer"
543+
)
544+
_check_msg(lambda: math.ceil(NAN), ValueError, "cannot convert float NaN to integer")
545+
_check_msg(
546+
lambda: math.trunc(INF), OverflowError, "cannot convert float infinity to integer"
547+
)
548+
_check_msg(
549+
lambda: INF.__int__(), OverflowError, "cannot convert float infinity to integer"
550+
)
551+
_check_msg(lambda: NAN.__floor__(), ValueError, "cannot convert float NaN to integer")

0 commit comments

Comments
 (0)