Skip to content

Commit 51df060

Browse files
committed
Fix: convert arguments to 'int' if possible.
1 parent 495d886 commit 51df060

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@
6969
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
7070
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
7171
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
72-
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.LenNode;
7372
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
73+
import com.oracle.graal.python.builtins.objects.common.SequenceNodes.LenNode;
7474
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
7575
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetItemNode;
7676
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ToByteArrayNode;
@@ -85,6 +85,7 @@
8585
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
8686
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
8787
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
88+
import com.oracle.graal.python.nodes.util.CastToIndexNode;
8889
import com.oracle.graal.python.runtime.PythonCore;
8990
import com.oracle.graal.python.runtime.exception.PException;
9091
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -329,14 +330,10 @@ Object fstat(int fd,
329330
}
330331

331332
@Specialization
332-
Object fstatPInt(PInt fd,
333+
Object fstatPInt(Object fd,
334+
@Cached("createOverflow()") CastToIndexNode castToIntNode,
333335
@Cached("create()") FstatNode recursive) {
334-
return recursive.executeWith(fd.intValue());
335-
}
336-
337-
@Fallback
338-
Object doGeneric(Object o) {
339-
throw raise(TypeError, "an integer is required (got type %p)", o);
336+
return recursive.executeWith(castToIntNode.execute(fd));
340337
}
341338

342339
protected static StatNode createStatNode() {
@@ -775,9 +772,10 @@ Object writeStd(int fd, PByteArray data) {
775772
}
776773

777774
@Specialization
778-
Object writePInt(PInt fd, Object data,
775+
Object writePInt(Object fd, Object data,
776+
@Cached("createOverflow()") CastToIndexNode castToIntNode,
779777
@Cached("create()") WriteNode recursive) {
780-
return recursive.executeWith(fd.intValue(), data);
778+
return recursive.executeWith(castToIntNode.execute(fd), data);
781779
}
782780

783781
private byte[] getByteArray(PIBytesLike pByteArray) {
@@ -788,7 +786,7 @@ private byte[] getByteArray(PIBytesLike pByteArray) {
788786
return toByteArrayNode.execute(pByteArray.getSequenceStorage());
789787
}
790788

791-
protected WriteNode create() {
789+
public static WriteNode create() {
792790
return PosixModuleBuiltinsFactory.WriteNodeFactory.create(null);
793791
}
794792
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CExtNodes.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.BoolNativeWrapper;
6363
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.ByteNativeWrapper;
6464
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.DoubleNativeWrapper;
65+
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.DynamicObjectNativeWrapper;
6566
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.IntNativeWrapper;
6667
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.LongNativeWrapper;
6768
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PrimitiveNativeWrapper;
@@ -265,20 +266,22 @@ public abstract static class ToSulongNode extends CExtBaseNode {
265266

266267
public abstract Object execute(Object obj);
267268

268-
/*
269-
* This is very sad. Only for Sulong, we cannot hand out java.lang.Strings, because then it
270-
* won't know what to do with them when they go native. So all places where Strings may be
271-
* passed from Python into C code need to wrap Strings into PStrings.
272-
*/
273269
@Specialization
274270
Object doString(String str,
275271
@Cached("createBinaryProfile()") ConditionProfile noWrapperProfile) {
276272
return PythonObjectNativeWrapper.wrap(factory().createString(str), noWrapperProfile);
277273
}
278274

279275
@Specialization
280-
Object doBoolean(boolean b) {
281-
return BoolNativeWrapper.create(b);
276+
Object doBoolean(boolean b,
277+
@Cached("createBinaryProfile()") ConditionProfile profile) {
278+
PInt boxed = factory().createInt(b);
279+
DynamicObjectNativeWrapper nativeWrapper = boxed.getNativeWrapper();
280+
if (profile.profile(nativeWrapper == null)) {
281+
nativeWrapper = BoolNativeWrapper.create(b);
282+
boxed.setNativeWrapper(nativeWrapper);
283+
}
284+
return nativeWrapper;
282285
}
283286

284287
@Specialization
@@ -368,7 +371,7 @@ public abstract static class AsPythonObjectNode extends CExtBaseNode {
368371

369372
@Child GetClassNode getClassNode;
370373

371-
@Specialization(guards = "!isMaterialized(object)")
374+
@Specialization
372375
boolean doBoolNativeWrapper(BoolNativeWrapper object) {
373376
return object.getValue();
374377
}
@@ -455,7 +458,7 @@ Object run(Object obj) {
455458
}
456459

457460
protected static boolean isPrimitiveNativeWrapper(PythonNativeWrapper object) {
458-
return object instanceof PrimitiveNativeWrapper && !isMaterialized((PrimitiveNativeWrapper) object);
461+
return object instanceof PrimitiveNativeWrapper && !isMaterialized((PrimitiveNativeWrapper) object) || object instanceof BoolNativeWrapper;
459462
}
460463

461464
protected boolean isForeignObject(TruffleObject obj) {

0 commit comments

Comments
 (0)