Skip to content

Commit b40ecd4

Browse files
committed
cleanup usage of OSError
1 parent f3840c2 commit b40ecd4

File tree

1 file changed

+32
-64
lines changed

1 file changed

+32
-64
lines changed

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

Lines changed: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
6161
import com.oracle.graal.python.builtins.PythonBuiltins;
6262
import com.oracle.graal.python.builtins.objects.PNone;
63+
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
6364
import com.oracle.graal.python.builtins.objects.ints.PInt;
6465
import com.oracle.graal.python.builtins.objects.list.PList;
6566
import com.oracle.graal.python.builtins.objects.socket.PSocket;
@@ -73,9 +74,11 @@
7374
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
7475
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7576
import com.oracle.truffle.api.dsl.Cached;
77+
import com.oracle.truffle.api.dsl.Fallback;
7678
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7779
import com.oracle.truffle.api.dsl.NodeFactory;
7880
import com.oracle.truffle.api.dsl.Specialization;
81+
import com.oracle.truffle.api.frame.VirtualFrame;
7982

8083
import org.graalvm.nativeimage.ImageInfo;
8184

@@ -169,7 +172,8 @@ private static String searchServicesForPort(int port, String protocol) {
169172
return null;
170173
}
171174

172-
private static String[] cleanLine(String line) {
175+
private static String[] cleanLine(String input) {
176+
String line = input;
173177
if (line.startsWith("#")) {
174178
return null;
175179
}
@@ -203,67 +207,34 @@ Object socket(LazyPythonClass cls, @SuppressWarnings("unused") PNone family, @Su
203207
return createSocketInternal(cls, PSocket.AF_INET, PSocket.SOCK_STREAM, 0);
204208
}
205209

206-
@Specialization(guards = {"isNoValue(family)", "isNoValue(type)", "isNoValue(proto)"})
207-
Object socket(LazyPythonClass cls, @SuppressWarnings("unused") PNone family, @SuppressWarnings("unused") PNone type, @SuppressWarnings("unused") PNone proto, int fileno) {
208-
return createSocketInternal(cls, -1, -1, -1, fileno);
210+
@Specialization(guards = {"isNoValue(family)", "isNoValue(type)", "isNoValue(proto)", "!isNoValue(fileno)"})
211+
Object socket(VirtualFrame frame, LazyPythonClass cls, @SuppressWarnings("unused") PNone family, @SuppressWarnings("unused") PNone type, @SuppressWarnings("unused") PNone proto, Object fileno,
212+
@Cached CastToIndexNode cast) {
213+
return createSocketInternal(frame, cls, -1, -1, -1, cast.execute(fileno));
209214
}
210215

211-
@Specialization(guards = {"isNoValue(type)", "isNoValue(proto)", "isNoValue(fileno)"})
212-
Object socket(LazyPythonClass cls, PInt family, @SuppressWarnings("unused") PNone type, @SuppressWarnings("unused") PNone proto, @SuppressWarnings("unused") PNone fileno) {
213-
return createSocketInternal(cls, family.intValue(), PSocket.SOCK_STREAM, 0);
214-
}
215-
216-
@Specialization(guards = {"isNoValue(type)", "isNoValue(proto)", "isNoValue(fileno)"})
216+
@Specialization(guards = {"!isNoValue(family)", "isNoValue(type)", "isNoValue(proto)", "isNoValue(fileno)"})
217217
Object socket(LazyPythonClass cls, Object family, @SuppressWarnings("unused") PNone type, @SuppressWarnings("unused") PNone proto, @SuppressWarnings("unused") PNone fileno,
218218
@Cached CastToIndexNode cast) {
219219
return createSocketInternal(cls, cast.execute(family), PSocket.SOCK_STREAM, 0);
220220
}
221221

222-
@Specialization(guards = {"isNoValue(proto)", "isNoValue(fileno)"})
222+
@Specialization(guards = {"!isNoValue(family)", "!isNoValue(type)", "isNoValue(proto)", "isNoValue(fileno)"})
223223
Object socket(LazyPythonClass cls, Object family, Object type, @SuppressWarnings("unused") PNone proto, @SuppressWarnings("unused") PNone fileno,
224224
@Cached CastToIndexNode cast) {
225225
return createSocketInternal(cls, cast.execute(family), cast.execute(type), 0);
226226
}
227227

228-
@Specialization(guards = {"isNoValue(proto)", "isNoValue(fileno)"})
229-
Object socket(LazyPythonClass cls, int family, long type, @SuppressWarnings("unused") PNone proto, @SuppressWarnings("unused") PNone fileno) {
230-
return createSocketInternal(cls, family, ((int) type), 0);
231-
}
232-
233-
@Specialization(guards = {"isNoValue(proto)", "isNoValue(fileno)"})
234-
Object socket(LazyPythonClass cls, PInt family, long type, @SuppressWarnings("unused") PNone proto, @SuppressWarnings("unused") PNone fileno) {
235-
return createSocketInternal(cls, family.asInt(), ((int) type), 0);
236-
}
237-
238-
@Specialization(guards = {"isNoValue(fileno)"})
228+
@Specialization(guards = {"!isNoValue(family)", "!isNoValue(type)", "!isNoValue(proto)", "isNoValue(fileno)"})
239229
Object socket(LazyPythonClass cls, Object family, Object type, Object proto, @SuppressWarnings("unused") PNone fileno,
240230
@Cached CastToIndexNode cast) {
241231
return createSocketInternal(cls, cast.execute(family), cast.execute(type), cast.execute(proto));
242232
}
243233

244-
@Specialization(guards = {"isNoValue(fileno)"})
245-
Object socket(LazyPythonClass cls, PInt family, PInt type, Integer proto, @SuppressWarnings("unused") PNone fileno) {
246-
return createSocketInternal(cls, family.asInt(), type.asInt(), proto);
247-
}
248-
249-
@Specialization()
250-
Object socket(LazyPythonClass cls, PInt family, PInt type, PInt proto, int fileno) {
251-
return createSocketInternal(cls, family.intValue(), type.intValue(), proto.intValue(), fileno);
252-
}
253-
254-
@Specialization()
255-
Object socket(LazyPythonClass cls, PInt family, PInt type, int proto, int fileno) {
256-
return createSocketInternal(cls, family.intValue(), type.intValue(), proto, fileno);
257-
}
258-
259-
@Specialization()
260-
Object socket(LazyPythonClass cls, int family, int type, int proto, int fileno) {
261-
return createSocketInternal(cls, family, type, proto, fileno);
262-
}
263-
264-
@Specialization()
265-
Object socket(LazyPythonClass cls, long family, long type, int proto, int fileno) {
266-
return createSocketInternal(cls, ((int) family), ((int) type), proto, fileno);
234+
@Specialization(guards = {"!isNoValue(family)", "!isNoValue(type)", "!isNoValue(proto)", "!isNoValue(fileno)"})
235+
Object socket(VirtualFrame frame, LazyPythonClass cls, Object family, Object type, Object proto, Object fileno,
236+
@Cached CastToIndexNode cast) {
237+
return createSocketInternal(frame, cls, cast.execute(family), cast.execute(type), cast.execute(proto), cast.execute(fileno));
267238
}
268239

269240
private Object createSocketInternal(LazyPythonClass cls, int family, int type, int proto) {
@@ -273,16 +244,15 @@ private Object createSocketInternal(LazyPythonClass cls, int family, int type, i
273244
newSocket.setFileno(fd);
274245
return newSocket;
275246
} else {
276-
throw raise(PythonErrorType.OSError, "creating sockets not allowed");
247+
throw raise(PythonErrorType.RuntimeError, "creating sockets not allowed");
277248
}
278249
}
279250

280-
@TruffleBoundary
281-
private Object createSocketInternal(LazyPythonClass cls, int family, int type, int proto, int fileno) {
251+
private Object createSocketInternal(VirtualFrame frame, LazyPythonClass cls, int family, int type, int proto, int fileno) {
282252
if (getContext().getEnv().isNativeAccessAllowed()) {
283253
PSocket oldSocket = getContext().getResources().getSocket(fileno);
284254
if (oldSocket == null) {
285-
throw raise(PythonErrorType.OSError, "bad filedescriptor");
255+
throw raiseOSError(frame, OSErrorEnum.EBADF.getNumber());
286256
}
287257
PSocket newSocket = factory().createSocket(cls, family == -1 ? oldSocket.getFamily() : family, type == -1 ? oldSocket.getType() : type, proto == -1 ? oldSocket.getProto() : proto, fileno);
288258
if (oldSocket.getSocket() != null) {
@@ -293,7 +263,7 @@ private Object createSocketInternal(LazyPythonClass cls, int family, int type, i
293263
getContext().getResources().reopenSocket(newSocket, fileno);
294264
return newSocket;
295265
} else {
296-
throw raise(PythonErrorType.OSError, "creating sockets not allowed");
266+
throw raise(PythonErrorType.RuntimeError, "creating sockets not allowed");
297267
}
298268
}
299269
}
@@ -629,48 +599,46 @@ InetAddress[] resolveHost(String host) {
629599
@GenerateNodeFactory
630600
public abstract static class moduleCloseNode extends PythonBuiltinNode {
631601
@Specialization
632-
@TruffleBoundary
633-
Object close(@SuppressWarnings("unused") PNone fd) {
634-
throw raise(PythonBuiltinClassType.TypeError);
635-
}
636-
637-
@Specialization
638-
@TruffleBoundary
639-
Object close(int fd) {
602+
Object close(VirtualFrame frame, int fd) {
640603
if (fd < 0) {
641604
throw raise(PythonBuiltinClassType.OSError, "Bad file descriptor");
642605
}
643606

644607
PSocket socket = getContext().getResources().getSocket(fd);
645608

646609
if (socket == null) {
647-
throw raise(PythonBuiltinClassType.OSError, "Bad file descriptor");
610+
throw raiseOSError(frame, OSErrorEnum.EBADF.getNumber());
648611
}
649612

650613
if (socket.getSocket() != null) {
651614
if (!socket.getSocket().isOpen()) {
652-
throw raise(PythonBuiltinClassType.OSError, "Bad file descriptor");
615+
throw raiseOSError(frame, OSErrorEnum.EBADF.getNumber());
653616
}
654617

655618
try {
656619
socket.getSocket().close();
657620
} catch (IOException e) {
658-
throw raise(PythonBuiltinClassType.OSError, "Bad file descriptor");
621+
throw raiseOSError(frame, OSErrorEnum.EBADF.getNumber());
659622
}
660623
}
661624
else if (socket.getServerSocket() != null) {
662625
if (!socket.getServerSocket().isOpen()) {
663-
throw raise(PythonBuiltinClassType.OSError, "Bad file descriptor");
626+
throw raiseOSError(frame, OSErrorEnum.EBADF.getNumber());
664627
}
665628

666629
try {
667630
socket.getServerSocket().close();
668631
} catch (IOException e) {
669-
throw raise(PythonBuiltinClassType.OSError, "Bad file descriptor");
632+
throw raiseOSError(frame, OSErrorEnum.EBADF.getNumber());
670633
}
671634
}
672-
getContext().getResources().closeSocket(socket.getFileno());
635+
getContext().getResources().close(socket.getFileno());
673636
return PNone.NONE;
674637
}
638+
639+
@Fallback
640+
Object close(@SuppressWarnings("unused") Object fd) {
641+
throw raise(PythonBuiltinClassType.TypeError);
642+
}
675643
}
676644
}

0 commit comments

Comments
 (0)