@@ -988,21 +988,23 @@ long parseLong(Object cls, PIBytesLike arg, int keywordArg) throws NumberFormatE
988
988
return parseLong (cls , new String (getByteArray (arg )), keywordArg );
989
989
}
990
990
991
- @ Specialization (rewriteOn = NumberFormatException .class )
992
- @ TruffleBoundary
993
- Object parseBytes (PythonClass cls , PIBytesLike arg , int base ) {
994
- return parsePInt (cls , new String (getByteArray (arg )), base );
995
- }
996
-
997
- @ Specialization (replaces = "parseBytes" )
998
- Object parseBytesError (PythonClass cls , PIBytesLike arg , int base ) {
991
+ @ Specialization
992
+ Object parseBytesError (PythonClass cls , PIBytesLike arg , int base ,
993
+ @ Cached ("create()" ) BranchProfile errorProfile ) {
999
994
try {
1000
- return parseBytes (cls , arg , base );
995
+ return parsePInt (cls , new String ( getByteArray ( arg )) , base );
1001
996
} catch (NumberFormatException e ) {
997
+ errorProfile .enter ();
1002
998
throw raise (ValueError , "invalid literal for int() with base %s: %s" , base , arg );
1003
999
}
1004
1000
}
1005
1001
1002
+ @ Specialization (guards = "isNoValue(base)" )
1003
+ Object parseBytesError (PythonClass cls , PIBytesLike arg , @ SuppressWarnings ("unused" ) PNone base ,
1004
+ @ Cached ("create()" ) BranchProfile errorProfile ) {
1005
+ return parseBytesError (cls , arg , 10 , errorProfile );
1006
+ }
1007
+
1006
1008
@ Specialization (guards = "isPrimitiveInt(cls)" , rewriteOn = NumberFormatException .class )
1007
1009
int parseInt (Object cls , PString arg , int keywordArg ) throws NumberFormatException {
1008
1010
return parseInt (cls , arg .getValue (), keywordArg );
@@ -1019,6 +1021,11 @@ Object parsePInt(PythonClass cls, PString arg, int keywordArg) {
1019
1021
return parsePInt (cls , arg .getValue (), keywordArg );
1020
1022
}
1021
1023
1024
+ @ Specialization (guards = "isNoValue(base)" )
1025
+ Object parsePInt (PythonClass cls , PString arg , PNone base ) {
1026
+ return createInt (cls , arg .getValue (), base );
1027
+ }
1028
+
1022
1029
@ Specialization (guards = "isPrimitiveInt(cls)" , rewriteOn = NumberFormatException .class )
1023
1030
@ TruffleBoundary
1024
1031
int parseInt (@ SuppressWarnings ("unused" ) Object cls , String arg , int keywordArg ) throws NumberFormatException {
@@ -1077,7 +1084,7 @@ Object fail(PythonClass cls, Object arg, Object keywordArg) {
1077
1084
throw raise (TypeError , "int() can't convert non-string with explicit base" );
1078
1085
}
1079
1086
1080
- @ Specialization (guards = {"isNoValue(keywordArg)" , "!isNoValue(obj)" })
1087
+ @ Specialization (guards = {"isNoValue(keywordArg)" , "!isNoValue(obj)" , "!isHandledType(obj)" })
1081
1088
public Object createInt (PythonClass cls , Object obj , PNone keywordArg ,
1082
1089
@ Cached ("create(__INT__)" ) LookupAndCallUnaryNode callIntNode ,
1083
1090
@ Cached ("create(__TRUNC__)" ) LookupAndCallUnaryNode callTruncNode ,
@@ -1112,6 +1119,10 @@ public Object createInt(PythonClass cls, Object obj, PNone keywordArg,
1112
1119
}
1113
1120
}
1114
1121
1122
+ protected static boolean isHandledType (Object obj ) {
1123
+ return PGuards .isInteger (obj ) || obj instanceof Double || obj instanceof Boolean || PGuards .isString (obj ) || PGuards .isBytes (obj );
1124
+ }
1125
+
1115
1126
private byte [] getByteArray (PIBytesLike pByteArray ) {
1116
1127
if (toByteArrayNode == null ) {
1117
1128
CompilerDirectives .transferToInterpreterAndInvalidate ();
0 commit comments