Skip to content

Commit 5d745f2

Browse files
committed
support wrapped booleans in CastToBoolean node
1 parent 87b61ee commit 5d745f2

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/CastToBooleanNode.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
2929

30+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3031
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
3132
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
3233
import com.oracle.graal.python.builtins.objects.ints.PInt;
@@ -35,6 +36,8 @@
3536
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
3637
import com.oracle.graal.python.nodes.expression.CastToBooleanNodeFactory.NotNodeGen;
3738
import com.oracle.graal.python.nodes.expression.CastToBooleanNodeFactory.YesNodeGen;
39+
import com.oracle.graal.python.nodes.object.GetClassNode;
40+
import com.oracle.truffle.api.CompilerDirectives;
3841
import com.oracle.truffle.api.dsl.Cached;
3942
import com.oracle.truffle.api.dsl.ImportStatic;
4043
import com.oracle.truffle.api.dsl.Specialization;
@@ -43,6 +46,7 @@
4346
import com.oracle.truffle.api.interop.TruffleObject;
4447

4548
public abstract class CastToBooleanNode extends UnaryOpNode {
49+
@Child private GetClassNode getClassNode;
4650

4751
public static CastToBooleanNode createIfTrueNode() {
4852
return YesNodeGen.create(null);
@@ -67,6 +71,14 @@ public static CastToBooleanNode createIfFalseNode(PNode operand) {
6771

6872
public abstract boolean executeWith(Object value);
6973

74+
GetClassNode getGetClassNode() {
75+
if (getClassNode == null) {
76+
CompilerDirectives.transferToInterpreterAndInvalidate();
77+
getClassNode = insert(GetClassNode.create());
78+
}
79+
return getClassNode;
80+
}
81+
7082
@ImportStatic(Message.class)
7183
public abstract static class YesNode extends CastToBooleanNode {
7284

@@ -116,8 +128,10 @@ boolean doObject(Object object,
116128
Object value = callBoolNode.executeObject(object);
117129
if (value instanceof Boolean) {
118130
return (boolean) value;
131+
} else if (value instanceof PInt && getGetClassNode().execute(value) == getCore().lookupType(PythonBuiltinClassType.Boolean)) {
132+
return ((PInt) value).isOne();
119133
} else {
120-
throw raise(TypeError, "__bool__ should return bool, returned %s", object);
134+
throw raise(TypeError, "__bool__ should return bool, returned %p", value);
121135
}
122136
}
123137
}
@@ -182,8 +196,10 @@ boolean doObject(Object object,
182196
Object value = callBoolNode.executeObject(object);
183197
if (value instanceof Boolean) {
184198
return !((boolean) value);
199+
} else if (value instanceof PInt && getGetClassNode().execute(value) == getCore().lookupType(PythonBuiltinClassType.Boolean)) {
200+
return ((PInt) value).isZero();
185201
} else {
186-
throw raise(TypeError, "__bool__ should return bool, returned %s", object);
202+
throw raise(TypeError, "__bool__ should return bool, returned %p", value);
187203
}
188204
}
189205
}

0 commit comments

Comments
 (0)