@@ -6,6 +6,7 @@ use num_traits::{One, Pow, Signed, ToPrimitive, Zero};
66
77use crate :: format:: FormatSpec ;
88use crate :: function:: { KwArgs , OptionalArg , PyFuncArgs } ;
9+ use crate :: obj:: objtype:: PyClassRef ;
910use crate :: pyhash;
1011use crate :: pyobject:: {
1112 IntoPyObject , PyClassImpl , PyContext , PyObjectRef , PyRef , PyResult , PyValue , TryFromObject ,
@@ -17,7 +18,6 @@ use super::objbyteinner::PyByteInner;
1718use super :: objbytes:: PyBytes ;
1819use super :: objstr:: { PyString , PyStringRef } ;
1920use 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