102
102
import com .oracle .graal .python .builtins .objects .complex .PComplex ;
103
103
import com .oracle .graal .python .builtins .objects .dict .PDict ;
104
104
import com .oracle .graal .python .builtins .objects .enumerate .PEnumerate ;
105
+ import com .oracle .graal .python .builtins .objects .floats .FloatBuiltins ;
106
+ import com .oracle .graal .python .builtins .objects .floats .FloatBuiltinsFactory ;
105
107
import com .oracle .graal .python .builtins .objects .floats .PFloat ;
106
108
import com .oracle .graal .python .builtins .objects .frame .PFrame ;
107
109
import com .oracle .graal .python .builtins .objects .function .PBuiltinFunction ;
@@ -928,7 +930,7 @@ public abstract static class IntNode extends PythonBuiltinNode {
928
930
public abstract Object executeWith (VirtualFrame frame , Object cls , Object arg , Object keywordArg );
929
931
930
932
@ TruffleBoundary (transferToInterpreterOnException = false )
931
- private Object stringToIntInternal (String num , int base ) {
933
+ private Object stringToIntInternal (String num , int base ) throws NumberFormatException {
932
934
String s = num .replace ("_" , "" );
933
935
if ((base >= 2 && base <= 32 ) || base == 0 ) {
934
936
BigInteger bi = asciiToBigInteger (s , base , false );
@@ -942,7 +944,7 @@ private Object stringToIntInternal(String num, int base) {
942
944
}
943
945
}
944
946
945
- private Object convertToIntInternal (LazyPythonClass cls , Object value , Object number , int base ) {
947
+ private Object convertToIntInternal (LazyPythonClass cls , Object value , Object number , int base ) throws NumberFormatException {
946
948
if (value == null ) {
947
949
throw raise (ValueError , "invalid literal for int() with base %s: %s" , base , number );
948
950
} else if (value instanceof BigInteger ) {
@@ -959,44 +961,22 @@ private Object stringToInt(LazyPythonClass cls, String number, int base) {
959
961
return convertToIntInternal (cls , value , number , base );
960
962
}
961
963
962
- private static Object doubleToIntInternal (double num ) {
963
- if (num > Integer .MAX_VALUE || num < Integer .MIN_VALUE ) {
964
- return BigInteger .valueOf ((long ) num );
965
- } else {
966
- return (int ) num ;
967
- }
968
- }
969
-
970
- @ TruffleBoundary (transferToInterpreterOnException = false )
971
- private Object toIntInternal (Object number ) {
972
- if (number instanceof Integer || number instanceof BigInteger ) {
973
- return number ;
974
- } else if (number instanceof Boolean ) {
975
- return (boolean ) number ? 1 : 0 ;
976
- } else if (number instanceof Double ) {
977
- return doubleToIntInternal ((Double ) number );
978
- } else if (number instanceof String ) {
979
- return stringToIntInternal ((String ) number , 10 );
980
- }
981
- return null ;
982
- }
983
-
984
- private Object toIntInternal (Object number , Object base ) {
964
+ private Object toIntInternal (Object number , Object base ) throws NumberFormatException {
985
965
if (number instanceof String && base instanceof Integer ) {
986
966
return stringToIntInternal ((String ) number , (Integer ) base );
987
967
} else {
988
968
throw raise (ValueError , "invalid base or val for int()" );
989
969
}
990
970
}
991
971
992
- private Object toInt (LazyPythonClass cls , Object number , int base ) {
972
+ private Object toInt (LazyPythonClass cls , Object number , int base ) throws NumberFormatException {
993
973
Object value = toIntInternal (number , base );
994
974
return convertToIntInternal (cls , value , number , base );
995
975
}
996
976
997
977
// Copied directly from Jython
998
978
@ TruffleBoundary (transferToInterpreterOnException = false )
999
- private static BigInteger asciiToBigInteger (String str , int possibleBase , boolean isLong ) {
979
+ private static BigInteger asciiToBigInteger (String str , int possibleBase , boolean isLong ) throws NumberFormatException {
1000
980
int base = possibleBase ;
1001
981
int b = 0 ;
1002
982
int e = str .length ();
@@ -1090,15 +1070,15 @@ Object parseInt(LazyPythonClass cls, boolean arg, @SuppressWarnings("unused") PN
1090
1070
}
1091
1071
1092
1072
@ Specialization (guards = "isNoValue(keywordArg)" )
1093
- public Object createInt (LazyPythonClass cls , int arg , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
1073
+ Object createInt (LazyPythonClass cls , int arg , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
1094
1074
if (isPrimitiveInt (cls )) {
1095
1075
return arg ;
1096
1076
}
1097
1077
return factory ().createInt (cls , arg );
1098
1078
}
1099
1079
1100
1080
@ Specialization (guards = "isNoValue(keywordArg)" )
1101
- public Object createInt (LazyPythonClass cls , long arg , @ SuppressWarnings ("unused" ) PNone keywordArg ,
1081
+ Object createInt (LazyPythonClass cls , long arg , @ SuppressWarnings ("unused" ) PNone keywordArg ,
1102
1082
@ Cached ("createBinaryProfile()" ) ConditionProfile isIntProfile ) {
1103
1083
if (isPrimitiveInt (cls )) {
1104
1084
int intValue = (int ) arg ;
@@ -1112,7 +1092,7 @@ public Object createInt(LazyPythonClass cls, long arg, @SuppressWarnings("unused
1112
1092
}
1113
1093
1114
1094
@ Specialization (guards = "isNoValue(keywordArg)" )
1115
- public Object createInt (LazyPythonClass cls , PythonNativeVoidPtr arg , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
1095
+ Object createInt (LazyPythonClass cls , PythonNativeVoidPtr arg , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
1116
1096
if (isPrimitiveInt (cls )) {
1117
1097
return arg ;
1118
1098
} else {
@@ -1122,24 +1102,23 @@ public Object createInt(LazyPythonClass cls, PythonNativeVoidPtr arg, @SuppressW
1122
1102
}
1123
1103
1124
1104
@ Specialization (guards = "isNoValue(keywordArg)" )
1125
- public Object createInt (LazyPythonClass cls , double arg , @ SuppressWarnings ("unused" ) PNone keywordArg ,
1126
- @ Cached ("createBinaryProfile()" ) ConditionProfile isIntProfile ) {
1127
- if (isPrimitiveInt (cls ) && isIntProfile .profile (arg >= Integer .MIN_VALUE && arg <= Integer .MAX_VALUE )) {
1128
- return (int ) arg ;
1129
- }
1130
- return factory ().createInt (cls , (long ) arg );
1105
+ Object createInt (VirtualFrame frame , LazyPythonClass cls , double arg , @ SuppressWarnings ("unused" ) PNone keywordArg ,
1106
+ @ Cached ("createFloatInt()" ) FloatBuiltins .IntNode intNode ,
1107
+ @ Cached ("createGeneric()" ) CreateIntFromObjectNode createIntFromObjectNode ) {
1108
+ Object result = intNode .executeWithDouble (arg );
1109
+ return createIntFromObjectNode .execute (frame , cls , result );
1131
1110
}
1132
1111
1133
1112
@ Specialization
1134
- public Object createInt (LazyPythonClass cls , @ SuppressWarnings ("unused" ) PNone none , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
1113
+ Object createInt (LazyPythonClass cls , @ SuppressWarnings ("unused" ) PNone none , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
1135
1114
if (isPrimitiveInt (cls )) {
1136
1115
return 0 ;
1137
1116
}
1138
1117
return factory ().createInt (cls , 0 );
1139
1118
}
1140
1119
1141
1120
@ Specialization (guards = "isNoValue(keywordArg)" )
1142
- public Object createInt (LazyPythonClass cls , String arg , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
1121
+ Object createInt (LazyPythonClass cls , String arg , @ SuppressWarnings ("unused" ) PNone keywordArg ) {
1143
1122
try {
1144
1123
return stringToInt (cls , arg , 10 );
1145
1124
} catch (NumberFormatException e ) {
@@ -1222,13 +1201,13 @@ Object parsePIntError(LazyPythonClass cls, String number, int base) {
1222
1201
}
1223
1202
1224
1203
@ Specialization (guards = "!isNoValue(base)" , rewriteOn = NumberFormatException .class )
1225
- public Object parsePIntWithBaseObject (LazyPythonClass cls , String number , Object base ,
1204
+ Object parsePIntWithBaseObject (LazyPythonClass cls , String number , Object base ,
1226
1205
@ Cached CastToIndexNode castToIndexNode ) {
1227
1206
return toInt (cls , number , castToIndexNode .execute (base ));
1228
1207
}
1229
1208
1230
1209
@ Specialization (guards = "!isNoValue(base)" , replaces = "parsePIntWithBaseObject" )
1231
- public Object createIntError (LazyPythonClass cls , String number , Object base ,
1210
+ Object createIntError (LazyPythonClass cls , String number , Object base ,
1232
1211
@ Cached CastToIndexNode castToIndexNode ) {
1233
1212
try {
1234
1213
return toInt (cls , number , castToIndexNode .execute (base ));
@@ -1244,7 +1223,7 @@ Object fail(LazyPythonClass cls, Object arg, Object keywordArg) {
1244
1223
}
1245
1224
1246
1225
@ Specialization (guards = {"isNoValue(keywordArg)" , "!isNoValue(obj)" , "!isHandledType(obj)" })
1247
- public Object createInt (VirtualFrame frame , LazyPythonClass cls , Object obj , @ SuppressWarnings ("unused" ) PNone keywordArg ,
1226
+ Object createInt (VirtualFrame frame , LazyPythonClass cls , Object obj , @ SuppressWarnings ("unused" ) PNone keywordArg ,
1248
1227
@ Cached ("createGeneric()" ) CreateIntFromObjectNode createIntFromObjectNode ) {
1249
1228
return createIntFromObjectNode .execute (frame , cls , obj );
1250
1229
}
@@ -1257,6 +1236,10 @@ protected static CreateIntFromObjectNode createGeneric() {
1257
1236
return CreateIntFromObjectNode .create (true , () -> LookupAndCallUnaryNode .create (SpecialMethodNames .__INT__ ));
1258
1237
}
1259
1238
1239
+ protected static FloatBuiltins .IntNode createFloatInt () {
1240
+ return FloatBuiltinsFactory .IntNodeFactory .create ();
1241
+ }
1242
+
1260
1243
private String toString (VirtualFrame frame , PIBytesLike pByteArray ) {
1261
1244
if (toByteArrayNode == null ) {
1262
1245
CompilerDirectives .transferToInterpreterAndInvalidate ();
0 commit comments