Skip to content

Commit 8d25d7a

Browse files
committed
Use CastToJava*Node in os.dup and os.lseek.
1 parent 025c8bb commit 8d25d7a

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
130130
import com.oracle.graal.python.nodes.util.CastToIndexNode;
131131
import com.oracle.graal.python.nodes.util.CastToIntegerFromIntNode;
132+
import com.oracle.graal.python.nodes.util.CastToJavaIntNode;
132133
import com.oracle.graal.python.nodes.util.CastToJavaLongNode;
133134
import com.oracle.graal.python.nodes.util.CastToPathNode;
134135
import com.oracle.graal.python.nodes.util.ChannelNodes.ReadFromChannelNode;
@@ -865,25 +866,16 @@ Object doit(VirtualFrame frame, LazyPythonClass cls, String name, Object pathArg
865866

866867
@Builtin(name = "dup", minNumOfPositionalArgs = 1)
867868
@GenerateNodeFactory
868-
@TypeSystemReference(PythonArithmeticTypes.class)
869869
abstract static class DupNode extends PythonFileNode {
870870
@Specialization
871-
int dup(int fd) {
871+
int dupInt(int fd) {
872872
return getResources().dup(fd);
873873
}
874874

875-
@Specialization(rewriteOn = ArithmeticException.class)
876-
int dupPInt(PInt fd) {
877-
return getResources().dup(fd.intValueExact());
878-
}
879-
880-
@Specialization(replaces = "dupPInt")
881-
int dupOvf(PInt fd) {
882-
try {
883-
return dupPInt(fd);
884-
} catch (ArithmeticException e) {
885-
throw raise(OSError, "invalid fd %r", fd);
886-
}
875+
@Specialization(replaces = "dupInt")
876+
int dupGeneric(Object fd,
877+
@Cached CastToJavaIntNode castToJavaIntNode) {
878+
return getResources().dup(castToJavaIntNode.execute(fd));
887879
}
888880
}
889881

@@ -1026,22 +1018,31 @@ public abstract static class LseekNode extends PythonFileNode {
10261018

10271019
@Specialization
10281020
Object lseek(VirtualFrame frame, long fd, long pos, int how,
1029-
@Cached PRaiseOSErrorNode raise,
1030-
@Cached("createClassProfile()") ValueProfile channelClassProfile) {
1021+
@Shared("channelClassProfile") @Cached("createClassProfile()") ValueProfile channelClassProfile) {
10311022
Channel channel = getResources().getFileChannel((int) fd, channelClassProfile);
1032-
if (noFile.profile(channel == null || !(channel instanceof SeekableByteChannel))) {
1033-
throw raise.raiseOSError(frame, OSErrorEnum.ESPIPE);
1023+
if (noFile.profile(!(channel instanceof SeekableByteChannel))) {
1024+
throw raiseOSError(frame, OSErrorEnum.ESPIPE);
10341025
}
10351026
SeekableByteChannel fc = (SeekableByteChannel) channel;
10361027
try {
10371028
return setPosition(pos, how, fc);
10381029
} catch (IOException e) {
10391030
gotException.enter();
10401031
// if this happen, we should raise OSError with appropriate errno
1041-
throw raise.raiseOSError(frame, -1);
1032+
throw raiseOSError(frame, -1);
10421033
}
10431034
}
10441035

1036+
@Specialization
1037+
Object lseekGeneric(VirtualFrame frame, Object fd, Object pos, Object how,
1038+
@Shared("channelClassProfile") @Cached("createClassProfile()") ValueProfile channelClassProfile,
1039+
@Cached CastToJavaLongNode castFdNode,
1040+
@Cached CastToJavaLongNode castPosNode,
1041+
@Cached CastToJavaIntNode castHowNode) {
1042+
1043+
return lseek(frame, castFdNode.execute(fd), castPosNode.execute(pos), castHowNode.execute(how), channelClassProfile);
1044+
}
1045+
10451046
@TruffleBoundary(allowInlining = true)
10461047
private static Object setPosition(long pos, int how, SeekableByteChannel fc) throws IOException {
10471048
switch (how) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/PythonBuiltinBaseNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ public final PException raiseOSError(VirtualFrame frame, int num) {
171171
return getRaiseOSNode().raiseOSError(frame, num);
172172
}
173173

174+
public final PException raiseOSError(VirtualFrame frame, OSErrorEnum num) {
175+
return getRaiseOSNode().raiseOSError(frame, num);
176+
}
177+
174178
public final PException raiseOSError(VirtualFrame frame, OSErrorEnum oserror, Exception e) {
175179
return getRaiseOSNode().raiseOSError(frame, oserror, e);
176180
}

0 commit comments

Comments
 (0)