@@ -932,7 +932,7 @@ public abstract static class IntNode extends PythonBuiltinNode {
932932 public abstract Object executeWith (VirtualFrame frame , Object cls , Object arg , Object keywordArg );
933933
934934 @ TruffleBoundary (transferToInterpreterOnException = false )
935- private Object stringToInt (String num , int base ) {
935+ private Object stringToIntInternal (String num , int base ) {
936936 String s = num .replace ("_" , "" );
937937 if ((base >= 2 && base <= 32 ) || base == 0 ) {
938938 BigInteger bi = asciiToBigInteger (s , base , false );
@@ -946,36 +946,63 @@ private Object stringToInt(String num, int base) {
946946 }
947947 }
948948
949- @ TruffleBoundary (transferToInterpreterOnException = false )
950- private Object toInt (Object arg ) {
951- if (arg instanceof Integer || arg instanceof BigInteger ) {
952- return arg ;
953- } else if (arg instanceof Boolean ) {
954- return (boolean ) arg ? 1 : 0 ;
955- } else if (arg instanceof Double ) {
956- return doubleToInt ((Double ) arg );
957- } else if (arg instanceof String ) {
958- return stringToInt ((String ) arg , 10 );
949+ private Object convertToIntInternal (LazyPythonClass cls , Object value , Object number , int base ) {
950+ if (value == null ) {
951+ throw raise (ValueError , "invalid literal for int() with base %s: %s" , base , number );
952+ } else if (value instanceof BigInteger ) {
953+ return factory ().createInt (cls , (BigInteger ) value );
954+ } else if (isPrimitiveInt (cls )) {
955+ return value ;
956+ } else {
957+ return factory ().createInt (cls , (int ) value );
959958 }
960- return null ;
961959 }
962960
963- private Object toInt (Object arg1 , Object arg2 ) {
964- if (arg1 instanceof String && arg2 instanceof Integer ) {
965- return stringToInt ((String ) arg1 , (Integer ) arg2 );
966- } else {
967- throw raise (ValueError , "invalid base or val for int()" );
968- }
961+ private Object stringToInt (LazyPythonClass cls , String number , int base ) {
962+ Object value = stringToIntInternal (number , base );
963+ return convertToIntInternal (cls , value , number , base );
969964 }
970965
971- private static Object doubleToInt (double num ) {
966+ private static Object doubleToIntInternal (double num ) {
972967 if (num > Integer .MAX_VALUE || num < Integer .MIN_VALUE ) {
973968 return BigInteger .valueOf ((long ) num );
974969 } else {
975970 return (int ) num ;
976971 }
977972 }
978973
974+ @ TruffleBoundary (transferToInterpreterOnException = false )
975+ private Object toIntInternal (Object number ) {
976+ if (number instanceof Integer || number instanceof BigInteger ) {
977+ return number ;
978+ } else if (number instanceof Boolean ) {
979+ return (boolean ) number ? 1 : 0 ;
980+ } else if (number instanceof Double ) {
981+ return doubleToIntInternal ((Double ) number );
982+ } else if (number instanceof String ) {
983+ return stringToIntInternal ((String ) number , 10 );
984+ }
985+ return null ;
986+ }
987+
988+ private Object toIntInternal (Object number , Object base ) {
989+ if (number instanceof String && base instanceof Integer ) {
990+ return stringToIntInternal ((String ) number , (Integer ) base );
991+ } else {
992+ throw raise (ValueError , "invalid base or val for int()" );
993+ }
994+ }
995+
996+ private Object toInt (LazyPythonClass cls , Object number ) {
997+ Object value = toIntInternal (number );
998+ return convertToIntInternal (cls , value , number , 10 );
999+ }
1000+
1001+ private Object toInt (LazyPythonClass cls , Object number , int base ) {
1002+ Object value = toIntInternal (number , base );
1003+ return convertToIntInternal (cls , value , number , base );
1004+ }
1005+
9791006 // Copied directly from Jython
9801007 @ TruffleBoundary (transferToInterpreterOnException = false )
9811008 private static BigInteger asciiToBigInteger (String str , int possibleBase , boolean isLong ) {
@@ -1123,12 +1150,7 @@ public Object createInt(LazyPythonClass cls, @SuppressWarnings("unused") PNone n
11231150 @ Specialization (guards = "isNoValue(keywordArg)" )
11241151 public Object createInt (LazyPythonClass cls , String arg , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
11251152 try {
1126- Object value = stringToInt (arg , 10 );
1127- if (isPrimitiveInt (cls )) {
1128- return value ;
1129- } else {
1130- return value instanceof BigInteger ? factory ().createInt (cls , (BigInteger ) value ) : factory ().createInt (cls , (int ) value );
1131- }
1153+ return stringToInt (cls , arg , 10 );
11321154 } catch (NumberFormatException e ) {
11331155 throw raise (ValueError , "invalid literal for int() with base 10: %s" , arg );
11341156 }
@@ -1195,40 +1217,23 @@ long parseLong(@SuppressWarnings("unused") LazyPythonClass cls, String arg, int
11951217 }
11961218
11971219 @ Specialization (rewriteOn = NumberFormatException .class )
1198- Object parsePInt (LazyPythonClass cls , String arg , int base ) {
1199- Object int2 = toInt (arg , base );
1200- if (int2 instanceof BigInteger ) {
1201- return factory ().createInt (cls , (BigInteger ) int2 );
1202- } else if (isPrimitiveInt (cls )) {
1203- return int2 ;
1204- } else {
1205- assert int2 instanceof Integer ;
1206- return factory ().createInt (cls , (int ) int2 );
1207- }
1220+ Object parsePInt (LazyPythonClass cls , String number , int base ) {
1221+ return toInt (cls , number , base );
12081222 }
12091223
12101224 @ Specialization (replaces = "parsePInt" )
1211- Object parsePIntError (LazyPythonClass cls , String arg , int base ) {
1225+ Object parsePIntError (LazyPythonClass cls , String number , int base ) {
12121226 try {
1213- return parsePInt (cls , arg , base );
1227+ return parsePInt (cls , number , base );
12141228 } catch (NumberFormatException e ) {
1215- throw raise (ValueError , "invalid literal for int() with base %s: %s" , base , arg );
1229+ throw raise (ValueError , "invalid literal for int() with base %s: %s" , base , number );
12161230 }
12171231 }
12181232
12191233 @ Specialization
1220- public Object createInt (LazyPythonClass cls , String arg , Object keywordArg ) {
1234+ public Object createInt (LazyPythonClass cls , String number , Object keywordArg ) {
12211235 if (keywordArg instanceof PNone ) {
1222- Object value = toInt (arg );
1223- if (value == null ) {
1224- throw raise (ValueError , "invalid literal for int() with base 10: %s" , arg );
1225- } else if (value instanceof BigInteger ) {
1226- return factory ().createInt (cls , (BigInteger ) value );
1227- } else if (isPrimitiveInt (cls )) {
1228- return value ;
1229- } else {
1230- return factory ().createInt (cls , (int ) value );
1231- }
1236+ return toInt (cls , number );
12321237 } else {
12331238 CompilerDirectives .transferToInterpreter ();
12341239 throw new RuntimeException ("Not implemented integer with base: " + keywordArg );
0 commit comments