Skip to content

Commit fcb60f7

Browse files
committed
Add missing specialization to 'int' constructor.
1 parent 03d3676 commit fcb60f7

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -988,21 +988,23 @@ long parseLong(Object cls, PIBytesLike arg, int keywordArg) throws NumberFormatE
988988
return parseLong(cls, new String(getByteArray(arg)), keywordArg);
989989
}
990990

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) {
999994
try {
1000-
return parseBytes(cls, arg, base);
995+
return parsePInt(cls, new String(getByteArray(arg)), base);
1001996
} catch (NumberFormatException e) {
997+
errorProfile.enter();
1002998
throw raise(ValueError, "invalid literal for int() with base %s: %s", base, arg);
1003999
}
10041000
}
10051001

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+
10061008
@Specialization(guards = "isPrimitiveInt(cls)", rewriteOn = NumberFormatException.class)
10071009
int parseInt(Object cls, PString arg, int keywordArg) throws NumberFormatException {
10081010
return parseInt(cls, arg.getValue(), keywordArg);
@@ -1019,6 +1021,11 @@ Object parsePInt(PythonClass cls, PString arg, int keywordArg) {
10191021
return parsePInt(cls, arg.getValue(), keywordArg);
10201022
}
10211023

1024+
@Specialization(guards = "isNoValue(base)")
1025+
Object parsePInt(PythonClass cls, PString arg, PNone base) {
1026+
return createInt(cls, arg.getValue(), base);
1027+
}
1028+
10221029
@Specialization(guards = "isPrimitiveInt(cls)", rewriteOn = NumberFormatException.class)
10231030
@TruffleBoundary
10241031
int parseInt(@SuppressWarnings("unused") Object cls, String arg, int keywordArg) throws NumberFormatException {
@@ -1077,7 +1084,7 @@ Object fail(PythonClass cls, Object arg, Object keywordArg) {
10771084
throw raise(TypeError, "int() can't convert non-string with explicit base");
10781085
}
10791086

1080-
@Specialization(guards = {"isNoValue(keywordArg)", "!isNoValue(obj)"})
1087+
@Specialization(guards = {"isNoValue(keywordArg)", "!isNoValue(obj)", "!isHandledType(obj)"})
10811088
public Object createInt(PythonClass cls, Object obj, PNone keywordArg,
10821089
@Cached("create(__INT__)") LookupAndCallUnaryNode callIntNode,
10831090
@Cached("create(__TRUNC__)") LookupAndCallUnaryNode callTruncNode,
@@ -1112,6 +1119,10 @@ public Object createInt(PythonClass cls, Object obj, PNone keywordArg,
11121119
}
11131120
}
11141121

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+
11151126
private byte[] getByteArray(PIBytesLike pByteArray) {
11161127
if (toByteArrayNode == null) {
11171128
CompilerDirectives.transferToInterpreterAndInvalidate();

0 commit comments

Comments
 (0)