Skip to content

Commit bcdb029

Browse files
committed
Fix: allow index-like objects as base in int constructor
1 parent a70d645 commit bcdb029

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_int.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,17 @@ def __int__(self):
336336
assert int(SpecInt0()) == 0
337337

338338
def test_create_int_from_string():
339-
assert int("5c7920a80f5261a2e5322163c79b71a25a41f414", 16) == 527928385865769069253929759180846776123316630548
339+
assert int("5c7920a80f5261a2e5322163c79b71a25a41f414", 16) == 527928385865769069253929759180846776123316630548
340+
class IndexLike:
341+
def __index__(self):
342+
return 16
343+
assert int("123ff", IndexLike()) == 0x123ff
344+
try:
345+
int("123ff", None)
346+
except TypeError:
347+
assert True
348+
else:
349+
assert False, "expected TypeError"
340350

341351

342352
class FromBytesTests(unittest.TestCase):

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -989,11 +989,6 @@ private Object toIntInternal(Object number, Object base) {
989989
}
990990
}
991991

992-
private Object toInt(LazyPythonClass cls, Object number) {
993-
Object value = toIntInternal(number);
994-
return convertToIntInternal(cls, value, number, 10);
995-
}
996-
997992
private Object toInt(LazyPythonClass cls, Object number, int base) {
998993
Object value = toIntInternal(number, base);
999994
return convertToIntInternal(cls, value, number, base);
@@ -1226,13 +1221,19 @@ Object parsePIntError(LazyPythonClass cls, String number, int base) {
12261221
}
12271222
}
12281223

1229-
@Specialization
1230-
public Object createInt(LazyPythonClass cls, String number, Object keywordArg) {
1231-
if (keywordArg instanceof PNone) {
1232-
return toInt(cls, number);
1233-
} else {
1234-
CompilerDirectives.transferToInterpreter();
1235-
throw new RuntimeException("Not implemented integer with base: " + keywordArg);
1224+
@Specialization(guards = "!isNoValue(base)", rewriteOn = NumberFormatException.class)
1225+
public Object parsePIntWithBaseObject(LazyPythonClass cls, String number, Object base,
1226+
@Cached CastToIndexNode castToIndexNode) {
1227+
return toInt(cls, number, castToIndexNode.execute(base));
1228+
}
1229+
1230+
@Specialization(guards = "!isNoValue(base)", replaces = "parsePIntWithBaseObject")
1231+
public Object createIntError(LazyPythonClass cls, String number, Object base,
1232+
@Cached CastToIndexNode castToIndexNode) {
1233+
try {
1234+
return toInt(cls, number, castToIndexNode.execute(base));
1235+
} catch (NumberFormatException e) {
1236+
throw raise(ValueError, "invalid literal for int() with base %s: %s", base, number);
12361237
}
12371238
}
12381239

0 commit comments

Comments
 (0)