Skip to content

Commit d07d066

Browse files
committed
[GR-10295] Is not created int from a class that have __int__ returning TRUE
1 parent 40b6ae7 commit d07d066

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,15 @@ def subclassTest(number):
298298
assert MyInt(MyTrunc()) == 1972
299299
assert MyInt(MyIntTrunc()) == 66
300300

301+
def test_create_int_from_bool():
302+
303+
class SpecInt1:
304+
def __int__(self):
305+
return True
306+
307+
class SpecInt0:
308+
def __int__(self):
309+
return False
310+
311+
assert int(SpecInt1()) == 1
312+
assert int(SpecInt0()) == 0

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ public Object createInt(PythonClass cls, PythonObject obj, PNone keywordArg,
719719
return createInt(cls, (int) result, keywordArg);
720720
} else if (result instanceof Long) {
721721
return createInt(cls, (long) result, keywordArg, isIntProfile);
722+
} else if (result instanceof Boolean) {
723+
return createInt(cls, (boolean) result ? 1 : 0, keywordArg, isIntProfile);
722724
} else if (result instanceof PInt) {
723725
// TODO warn if 'result' not of exact Python type 'int'
724726
return isPrimitiveInt(cls) ? result : factory().createInt(cls, ((PInt) result).getValue());

0 commit comments

Comments
 (0)