Skip to content

Commit b24444e

Browse files
committed
fix int fn
1 parent c32b714 commit b24444e

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

vm/src/obj/objint.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use num_traits::{One, Pow, Signed, ToPrimitive, Zero};
66

77
use crate::format::FormatSpec;
88
use crate::function::{KwArgs, OptionalArg, PyFuncArgs};
9+
use crate::obj::objtype::PyClassRef;
910
use crate::pyhash;
1011
use crate::pyobject::{
1112
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
@@ -17,7 +18,6 @@ use super::objbyteinner::PyByteInner;
1718
use super::objbytes::PyBytes;
1819
use super::objstr::{PyString, PyStringRef};
1920
use super::objtype;
20-
use crate::obj::objtype::PyClassRef;
2121

2222
/// int(x=0) -> integer
2323
/// int(x, base=10) -> integer
@@ -667,10 +667,16 @@ fn int_new(cls: PyClassRef, options: IntOptions, vm: &VirtualMachine) -> PyResul
667667
}
668668

669669
// Casting function:
670-
pub fn to_int(vm: &VirtualMachine, obj: &PyObjectRef, base: u32) -> PyResult<BigInt> {
670+
pub fn to_int(vm: &VirtualMachine, obj: &PyObjectRef, mut base: u32) -> PyResult<BigInt> {
671+
if base == 0 {
672+
base = 10
673+
} else if base < 2 || base > 36 {
674+
return Err(vm.new_value_error(format!("int() base must be >= 2 and <= 36, or 0")));
675+
}
676+
671677
match_class!(obj.clone(),
672678
s @ PyString => {
673-
i32::from_str_radix(s.as_str(), base)
679+
i32::from_str_radix(s.as_str().trim(), base)
674680
.map(BigInt::from)
675681
.map_err(|_|vm.new_value_error(format!(
676682
"invalid literal for int() with base {}: '{}'",

0 commit comments

Comments
 (0)