Skip to content

Commit 45985d3

Browse files
committed
do not assume storage type of PBytes when sending
1 parent 3485bd2 commit 45985d3

File tree

1 file changed

+19
-24
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket

1 file changed

+19
-24
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/SocketBuiltins.java

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@
6060
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
6161
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
6262
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
63+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
6364
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6465
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6566
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
6667
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
6768
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
6869
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
69-
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
7070
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
71+
import com.oracle.truffle.api.dsl.Cached;
7172
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7273
import com.oracle.truffle.api.dsl.NodeFactory;
7374
import com.oracle.truffle.api.dsl.Specialization;
@@ -402,50 +403,44 @@ Object recvFrom(PSocket socket, int bufsize, PNone ancbufsize, PNone flags) {
402403
@GenerateNodeFactory
403404
abstract static class SendNode extends PythonTernaryBuiltinNode {
404405
@Specialization
405-
Object send(PSocket socket, PBytes bytes, int flags) {
406-
return send(socket, bytes, PNone.NONE);
407-
}
408-
409-
@Specialization
410-
@TruffleBoundary
411-
Object send(PSocket socket, PBytes bytes, PNone flags) {
406+
Object send(PSocket socket, PBytes bytes, Object flags,
407+
@Cached SequenceStorageNodes.ToByteArrayNode toBytes) {
408+
// TODO: do not ignore flags
412409
if (socket.getSocket() == null) {
413410
throw raise(PythonBuiltinClassType.OSError);
414411
}
415412

416-
if (!socket.getSocket().isOpen()) {
413+
if (!socket.isOpen()) {
417414
throw raise(PythonBuiltinClassType.OSError);
418415
}
419416

420417
try {
421-
ByteSequenceStorage byteSequenceStorage = (ByteSequenceStorage) bytes.getSequenceStorage();
422-
ByteBuffer buffer = ByteBuffer.wrap(byteSequenceStorage.getInternalByteArray());
423-
socket.getSocket().write(buffer);
424-
418+
byte[] storageArray = toBytes.execute(bytes.getSequenceStorage());
419+
ByteBuffer buffer = ByteBuffer.wrap(storageArray);
420+
doWrite(socket, buffer);
425421
return PNone.NONE;
426422
} catch (IOException e) {
427423
throw raise(PythonBuiltinClassType.OSError);
428424
}
429425
}
430426
}
431427

428+
@TruffleBoundary
429+
private static void doWrite(PSocket socket, ByteBuffer buffer) throws IOException {
430+
socket.getSocket().write(buffer);
431+
}
432+
432433
// sendall(bytes[, flags])
433434
@Builtin(name = "sendall", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
434435
@GenerateNodeFactory
435436
abstract static class SendAllNode extends PythonTernaryBuiltinNode {
436437
@Specialization
437-
Object sendAll(PSocket socket, PIBytesLike bytes, int flags) {
438-
return sendAll(socket, bytes, PNone.NONE);
439-
}
440-
441-
@Specialization(guards = {"isNoValue(flags)"})
442-
@TruffleBoundary
443-
Object sendAll(PSocket socket, PIBytesLike bytes, @SuppressWarnings("unused") PNone flags) {
438+
Object sendAll(PSocket socket, PIBytesLike bytes, Object flags,
439+
@Cached SequenceStorageNodes.ToByteArrayNode toBytes) {
440+
// TODO: do not ignore flags
444441
try {
445-
ByteSequenceStorage byteSequenceStorage = (ByteSequenceStorage) bytes.getSequenceStorage();
446-
ByteBuffer buffer = ByteBuffer.wrap(byteSequenceStorage.getInternalByteArray());
447-
socket.getSocket().write(buffer);
448-
442+
ByteBuffer buffer = ByteBuffer.wrap(toBytes.execute(bytes.getSequenceStorage()));
443+
doWrite(socket, buffer);
449444
return PNone.NONE;
450445
} catch (IOException e) {
451446
throw raise(PythonBuiltinClassType.OSError);

0 commit comments

Comments
 (0)