Skip to content

Commit 0b23191

Browse files
committed
[GR-23206] Make 4 more boolean tests pass
PullRequest: graalpython/960
2 parents 19cd2c7 + 30f3994 commit 0b23191

File tree

12 files changed

+157
-19
lines changed

12 files changed

+157
-19
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_bool.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
*BoolTest.test_blocked
2+
*BoolTest.test_boolean
23
*BoolTest.test_callable
34
*BoolTest.test_contains
45
*BoolTest.test_convert
6+
*BoolTest.test_convert_to_bool
57
*BoolTest.test_fileclosed
68
*BoolTest.test_float
79
*BoolTest.test_format
10+
*BoolTest.test_from_bytes
811
*BoolTest.test_hasattr
912
*BoolTest.test_int
1013
*BoolTest.test_isinstance
1114
*BoolTest.test_issubclass
1215
*BoolTest.test_keyword_args
1316
*BoolTest.test_marshal
17+
*BoolTest.test_math
1418
*BoolTest.test_operator
1519
*BoolTest.test_pickle
1620
*BoolTest.test_picklevalues

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ private static final String[] initializeCoreFiles() {
260260
"_lsprof",
261261
"marshal",
262262
"_struct",
263+
"bool",
263264
"_lzma"));
264265
// add service loader defined python file extensions
265266
if (!ImageInfo.inImageRuntimeCode()) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
public enum PythonBuiltinClassType implements LazyPythonClass {
5757

5858
ForeignObject(BuiltinNames.FOREIGN),
59-
Boolean("bool", BuiltinNames.BUILTINS),
59+
Boolean("bool", BuiltinNames.BUILTINS, false),
6060
GetSetDescriptor("get_set_desc"),
6161
PArray("array", "array"),
6262
PArrayIterator("arrayiterator"),
@@ -206,11 +206,12 @@ public enum PythonBuiltinClassType implements LazyPythonClass {
206206
private final Shape instanceShape;
207207
private final String publicInModule;
208208
private final String qualifiedName;
209+
private final boolean basetype;
209210

210211
// initialized in static constructor
211212
@CompilationFinal private PythonBuiltinClassType base;
212213

213-
PythonBuiltinClassType(String name, String publicInModule) {
214+
PythonBuiltinClassType(String name, String publicInModule, boolean basetype) {
214215
this.name = name;
215216
this.publicInModule = publicInModule;
216217
if (publicInModule != null && publicInModule != BuiltinNames.BUILTINS) {
@@ -219,10 +220,19 @@ public enum PythonBuiltinClassType implements LazyPythonClass {
219220
qualifiedName = name;
220221
}
221222
this.instanceShape = com.oracle.graal.python.builtins.objects.object.PythonObject.freshShape(this);
223+
this.basetype = basetype;
224+
}
225+
226+
PythonBuiltinClassType(String name, String publicInModule) {
227+
this(name, publicInModule, true);
222228
}
223229

224230
PythonBuiltinClassType(String name) {
225-
this(name, null);
231+
this(name, null, true);
232+
}
233+
234+
public boolean isAcceptableBase() {
235+
return basetype;
226236
}
227237

228238
@Override

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
146146
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroNode;
147147
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetNameNode;
148+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsAcceptableBaseNode;
148149
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
149150
import com.oracle.graal.python.nodes.BuiltinNames;
150151
import com.oracle.graal.python.nodes.PGuards;
@@ -2100,6 +2101,7 @@ public abstract static class TypeNode extends PythonBuiltinNode {
21002101
@Child private GetMroNode getMroNode;
21012102
@Child private IsSubtypeNode isSubtypeNode;
21022103
@Child private GetObjectArrayNode getObjectArrayNode;
2104+
@Child private IsAcceptableBaseNode isAcceptableBaseNode;
21032105

21042106
protected abstract Object execute(VirtualFrame frame, Object cls, Object name, Object bases, Object dict, PKeyword[] kwds);
21052107

@@ -2122,7 +2124,6 @@ Object type(VirtualFrame frame, LazyPythonClass cls, String name, PTuple bases,
21222124
@Cached CallNode callSetNameNode,
21232125
@Cached CallNode callInitSubclassNode,
21242126
@Cached CallNode callNewFuncNode) {
2125-
21262127
// Determine the proper metatype to deal with this
21272128
LazyPythonClass metaclass = calculate_metaclass(frame, cls, bases, getMetaclassNode);
21282129
if (metaclass != cls) {
@@ -2479,6 +2480,9 @@ private PythonAbstractClass[] getMro(PythonAbstractClass pythonClass) {
24792480
private LazyPythonClass calculate_metaclass(VirtualFrame frame, LazyPythonClass cls, PTuple bases, GetLazyClassNode getMetaclassNode) {
24802481
LazyPythonClass winner = cls;
24812482
for (Object base : ensureGetObjectArrayNode().execute(bases)) {
2483+
if (!ensureIsAcceptableBaseNode().execute(base)) {
2484+
throw raise(TypeError, "type '%p' is not an acceptable base type", base);
2485+
}
24822486
LazyPythonClass typ = getMetaclassNode.execute(base);
24832487
if (isSubType(frame, winner, typ)) {
24842488
continue;
@@ -2569,6 +2573,14 @@ private GetObjectArrayNode ensureGetObjectArrayNode() {
25692573
}
25702574
return getObjectArrayNode;
25712575
}
2576+
2577+
private IsAcceptableBaseNode ensureIsAcceptableBaseNode() {
2578+
if (isAcceptableBaseNode == null) {
2579+
CompilerDirectives.transferToInterpreterAndInvalidate();
2580+
isAcceptableBaseNode = insert(IsAcceptableBaseNode.create());
2581+
}
2582+
return isAcceptableBaseNode;
2583+
}
25722584
}
25732585

25742586
@Builtin(name = MODULE, minNumOfPositionalArgs = 1, takesVarArgs = true, takesVarKeywordArgs = true, constructsClass = PythonBuiltinClassType.PythonModule, isPublic = false)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ public void postInitialize(PythonCore core) {
219219
@GenerateNodeFactory
220220
public abstract static class AbsNode extends PythonUnaryBuiltinNode {
221221
@Specialization
222-
public boolean absInt(boolean arg) {
223-
return arg;
222+
public int absInt(boolean arg) {
223+
return arg ? 1 : 0;
224224
}
225225

226226
@Specialization

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
7070
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
7171
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
72+
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
7273
import com.oracle.graal.python.nodes.PGuards;
7374
import com.oracle.graal.python.nodes.SpecialMethodNames;
7475
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
@@ -2132,7 +2133,13 @@ private boolean isBigEndian(String order) {
21322133
}
21332134

21342135
private Object createIntObject(LazyPythonClass cl, BigInteger number) {
2135-
if (PGuards.isPythonBuiltinClass(cl)) {
2136+
PythonBuiltinClassType type = null;
2137+
if (cl instanceof PythonBuiltinClass) {
2138+
type = ((PythonBuiltinClass) cl).getType();
2139+
} else if (cl instanceof PythonBuiltinClassType) {
2140+
type = (PythonBuiltinClassType) cl;
2141+
}
2142+
if (type == PythonBuiltinClassType.PInt) {
21362143
return factory().createInt(number);
21372144
}
21382145
if (constructNode == null) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/PInt.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,6 @@ public Object asIndexWithState(@SuppressWarnings("unused") ThreadState threadSta
205205
return this;
206206
}
207207

208-
@ExportMessage
209-
boolean isTrueWithState(@SuppressWarnings("unused") ThreadState threadState) {
210-
return !isZero();
211-
}
212-
213208
@Override
214209
public int hashCode() {
215210
return value.hashCode();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonBuiltinClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public final class PythonBuiltinClass extends PythonManagedClass {
4646
private final PythonBuiltinClassType type;
4747

4848
public PythonBuiltinClass(PythonBuiltinClassType builtinClass, PythonAbstractClass base) {
49-
super(PythonBuiltinClassType.PythonClass, PythonBuiltinClassType.PythonClass.newInstance(), builtinClass.getQualifiedName(), base);
49+
super(PythonBuiltinClassType.PythonClass, PythonBuiltinClassType.PythonClass.newInstance(), builtinClass.getInstanceShape(), builtinClass.getQualifiedName(), base);
5050
this.type = builtinClass;
5151
}
5252

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
public final class PythonClass extends PythonManagedClass {
4848

4949
public PythonClass(LazyPythonClass typeClass, DynamicObject storage, String name, PythonAbstractClass[] baseClasses) {
50-
super(typeClass, storage, name, baseClasses);
50+
super(typeClass, storage, null, name, baseClasses);
5151
}
5252

5353
@ExportMessage

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonManagedClass.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public abstract class PythonManagedClass extends PythonObject implements PythonA
6565
@CompilationFinal private Object sulongType;
6666

6767
@TruffleBoundary
68-
public PythonManagedClass(LazyPythonClass typeClass, DynamicObject storage, String name, PythonAbstractClass... baseClasses) {
68+
protected PythonManagedClass(LazyPythonClass typeClass, DynamicObject storage, Shape instanceShape, String name, PythonAbstractClass... baseClasses) {
6969
super(typeClass, storage);
7070
this.className = name;
7171

@@ -86,11 +86,15 @@ public PythonManagedClass(LazyPythonClass typeClass, DynamicObject storage, Stri
8686
setAttribute(__NAME__, getBaseName(name));
8787
setAttribute(__QUALNAME__, className);
8888
setAttribute(__DOC__, PNone.NONE);
89-
// provide our instances with a fresh shape tree
90-
if (PythonLanguage.getCurrent().singleContextAssumption.isValid()) {
91-
this.instanceShape = PythonObject.freshShape(this);
89+
if (instanceShape != null) {
90+
this.instanceShape = instanceShape;
9291
} else {
93-
this.instanceShape = PythonObject.freshShape();
92+
// provide our instances with a fresh shape tree
93+
if (PythonLanguage.getCurrent().singleContextAssumption.isValid()) {
94+
this.instanceShape = PythonObject.freshShape(this);
95+
} else {
96+
this.instanceShape = PythonObject.freshShape();
97+
}
9498
}
9599
}
96100

0 commit comments

Comments
 (0)