Fix int rounding

This commit is contained in:
Jeong, YunWon committed 2026-01-17 19:21:11 +09:00
1 parent db01a1d653
commit eafa0c0149
2 files changed
+7 -5

No files matched your search

+4 -3
View File
@@ -12,7 +12,8 @@ use crate::{
},
convert::{IntoPyException, ToPyObject, ToPyResult},
function::{
ArgByteOrder, ArgIntoBool, FuncArgs, OptionalArg, PyArithmeticValue, PyComparisonValue,
ArgByteOrder, ArgIntoBool, FuncArgs, OptionalArg, OptionalOption, PyArithmeticValue,
PyComparisonValue,
},
protocol::{PyNumberMethods, handle_bytes_to_int_err},
types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp, Representable},
@@ -381,10 +382,10 @@ impl PyInt {
#[pymethod]
fn __round__(
zelf: PyRef<Self>,
ndigits: OptionalArg<PyIntRef>,
ndigits: OptionalOption<PyIntRef>,
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
if let OptionalArg::Present(ndigits) = ndigits {
if let Some(ndigits) = ndigits.flatten() {
let ndigits = ndigits.as_bigint();
// round(12345, -2) == 12300
// If precision >= 0, then any integer is already rounded correctly
+3 -2
View File
@@ -318,8 +318,9 @@ assert isinstance((0).__round__(0), int)
assert isinstance((1).__round__(0), int)
assert (0).__round__(0) == 0
assert (1).__round__(0) == 1
assert_raises(TypeError, lambda: (0).__round__(None))
assert_raises(TypeError, lambda: (1).__round__(None))
# Python 3.14+: __round__(None) is now allowed, same as __round__()
assert (0).__round__(None) == 0
assert (1).__round__(None) == 1
assert_raises(TypeError, lambda: (0).__round__(0.0))
assert_raises(TypeError, lambda: (1).__round__(0.0))