@@ -932,7 +932,7 @@ public abstract static class IntNode extends PythonBuiltinNode {
932
932
public abstract Object executeWith (VirtualFrame frame , Object cls , Object arg , Object keywordArg );
933
933
934
934
@ TruffleBoundary (transferToInterpreterOnException = false )
935
- private Object stringToInt (String num , int base ) {
935
+ private Object stringToIntInternal (String num , int base ) {
936
936
String s = num .replace ("_" , "" );
937
937
if ((base >= 2 && base <= 32 ) || base == 0 ) {
938
938
BigInteger bi = asciiToBigInteger (s , base , false );
@@ -946,36 +946,63 @@ private Object stringToInt(String num, int base) {
946
946
}
947
947
}
948
948
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 );
959
958
}
960
- return null ;
961
959
}
962
960
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 );
969
964
}
970
965
971
- private static Object doubleToInt (double num ) {
966
+ private static Object doubleToIntInternal (double num ) {
972
967
if (num > Integer .MAX_VALUE || num < Integer .MIN_VALUE ) {
973
968
return BigInteger .valueOf ((long ) num );
974
969
} else {
975
970
return (int ) num ;
976
971
}
977
972
}
978
973
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
+
979
1006
// Copied directly from Jython
980
1007
@ TruffleBoundary (transferToInterpreterOnException = false )
981
1008
private static BigInteger asciiToBigInteger (String str , int possibleBase , boolean isLong ) {
@@ -1123,12 +1150,7 @@ public Object createInt(LazyPythonClass cls, @SuppressWarnings("unused") PNone n
1123
1150
@ Specialization (guards = "isNoValue(keywordArg)" )
1124
1151
public Object createInt (LazyPythonClass cls , String arg , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
1125
1152
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 );
1132
1154
} catch (NumberFormatException e ) {
1133
1155
throw raise (ValueError , "invalid literal for int() with base 10: %s" , arg );
1134
1156
}
@@ -1195,40 +1217,23 @@ long parseLong(@SuppressWarnings("unused") LazyPythonClass cls, String arg, int
1195
1217
}
1196
1218
1197
1219
@ 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 );
1208
1222
}
1209
1223
1210
1224
@ Specialization (replaces = "parsePInt" )
1211
- Object parsePIntError (LazyPythonClass cls , String arg , int base ) {
1225
+ Object parsePIntError (LazyPythonClass cls , String number , int base ) {
1212
1226
try {
1213
- return parsePInt (cls , arg , base );
1227
+ return parsePInt (cls , number , base );
1214
1228
} 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 );
1216
1230
}
1217
1231
}
1218
1232
1219
1233
@ Specialization
1220
- public Object createInt (LazyPythonClass cls , String arg , Object keywordArg ) {
1234
+ public Object createInt (LazyPythonClass cls , String number , Object keywordArg ) {
1221
1235
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 );
1232
1237
} else {
1233
1238
CompilerDirectives .transferToInterpreter ();
1234
1239
throw new RuntimeException ("Not implemented integer with base: " + keywordArg );
0 commit comments