Skip to content

Commit fb98b6e

Browse files
committed
workaround for DSL problem
1 parent ce951bd commit fb98b6e

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import com.oracle.truffle.api.dsl.NodeFactory;
102102
import com.oracle.truffle.api.dsl.Specialization;
103103
import com.oracle.truffle.api.dsl.TypeSystemReference;
104+
import com.oracle.truffle.api.frame.VirtualFrame;
104105
import com.oracle.truffle.api.profiles.ValueProfile;
105106

106107
@CoreFunctions(defineModule = "posix")
@@ -802,38 +803,42 @@ public static WriteNode create() {
802803
@GenerateNodeFactory
803804
@TypeSystemReference(PythonArithmeticTypes.class)
804805
public abstract static class ReadNode extends PythonFileNode {
805-
@Specialization(guards = "readOpaque()")
806-
@TruffleBoundary
807-
Object readOpaque(int fd, @SuppressWarnings("unused") Object requestedSize) {
806+
@Specialization(guards = "readOpaque(frame)")
807+
Object readOpaque(@SuppressWarnings("unused") VirtualFrame frame, int fd, @SuppressWarnings("unused") Object requestedSize) {
808808
SeekableByteChannel channel = getFileChannel(fd);
809809
try {
810-
long size = channel.size() - channel.position();
811-
ByteBuffer dst = ByteBuffer.allocate((int) size);
812-
channel.read(dst);
813-
return new OpaqueBytes(dst.array());
810+
return new OpaqueBytes(doRead(channel, Integer.MAX_VALUE));
814811
} catch (IOException e) {
815812
throw raise(OSError, e.getMessage());
816813
}
817814
}
818815

819-
@Specialization(guards = "!readOpaque()")
820-
@TruffleBoundary
821-
Object read(int fd, long requestedSize) {
816+
@Specialization(guards = "!readOpaque(frame)")
817+
Object read(@SuppressWarnings("unused") VirtualFrame frame, int fd, long requestedSize) {
822818
SeekableByteChannel channel = getFileChannel(fd);
823819
try {
824-
long size = Math.min(requestedSize, channel.size() - channel.position());
825-
// cast below will always succeed, since requestedSize was an int,
826-
// and must thus will always be smaller than a long that cannot be
827-
// downcast
828-
ByteBuffer dst = ByteBuffer.allocate((int) size);
829-
getFileChannel(fd).read(dst);
830-
return factory().createBytes(dst.array());
820+
byte[] array = doRead(channel, (int) requestedSize);
821+
return factory().createBytes(array);
831822
} catch (IOException e) {
832823
throw raise(OSError, e.getMessage());
833824
}
834825
}
835826

836-
protected boolean readOpaque() {
827+
@TruffleBoundary
828+
private static byte[] doRead(SeekableByteChannel channel, int requestedSize) throws IOException {
829+
long size = Math.min(requestedSize, channel.size() - channel.position());
830+
// cast below will always succeed, since requestedSize was an int,
831+
// and must thus will always be smaller than a long that cannot be
832+
// downcast
833+
ByteBuffer dst = ByteBuffer.allocate((int) size);
834+
channel.read(dst);
835+
return dst.array();
836+
}
837+
838+
/**
839+
* @param frame - only used so the DSL sees this as a dynamic check
840+
*/
841+
protected boolean readOpaque(VirtualFrame frame) {
837842
return OpaqueBytes.isEnabled(getContext());
838843
}
839844
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesBuiltins.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,14 @@ public Object repr(PBytes self,
296296
@Builtin(name = "join", fixedNumOfPositionalArgs = 2)
297297
@GenerateNodeFactory
298298
public abstract static class JoinNode extends PythonBinaryBuiltinNode {
299-
protected boolean readOpaque() {
299+
/**
300+
* @param bytes - the parameter is used to force the DSL to make this a dynamic check
301+
*/
302+
protected boolean readOpaque(PBytes bytes) {
300303
return OpaqueBytes.isEnabled(getContext());
301304
}
302305

303-
@Specialization(guards = {"readOpaque()"})
306+
@Specialization(guards = {"readOpaque(bytes)"})
304307
public Object join(PBytes bytes, PList iterable,
305308
@Cached("create()") SequenceStorageNodes.GetItemNode getItemNode,
306309
@Cached("create()") SequenceStorageNodes.LenNode lenNode,
@@ -319,7 +322,7 @@ public Object join(PBytes bytes, PList iterable,
319322
return join(bytes, iterable, toByteArrayNode, bytesJoinNode);
320323
}
321324

322-
@Specialization(guards = {"!readOpaque()"})
325+
@Specialization(guards = {"!readOpaque(bytes)"})
323326
public PBytes join(PBytes bytes, Object iterable,
324327
@Cached("create()") SequenceStorageNodes.ToByteArrayNode toByteArrayNode,
325328
@Cached("create()") BytesNodes.BytesJoinNode bytesJoinNode) {

0 commit comments

Comments
 (0)