|
60 | 60 | import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
|
61 | 61 | import com.oracle.graal.python.builtins.objects.bytes.PBytes;
|
62 | 62 | import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
|
| 63 | +import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes; |
63 | 64 | import com.oracle.graal.python.builtins.objects.tuple.PTuple;
|
64 | 65 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
65 | 66 | import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
|
66 | 67 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
|
67 | 68 | import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
|
68 | 69 | import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
|
69 |
| -import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage; |
70 | 70 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
| 71 | +import com.oracle.truffle.api.dsl.Cached; |
71 | 72 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
72 | 73 | import com.oracle.truffle.api.dsl.NodeFactory;
|
73 | 74 | import com.oracle.truffle.api.dsl.Specialization;
|
@@ -402,50 +403,44 @@ Object recvFrom(PSocket socket, int bufsize, PNone ancbufsize, PNone flags) {
|
402 | 403 | @GenerateNodeFactory
|
403 | 404 | abstract static class SendNode extends PythonTernaryBuiltinNode {
|
404 | 405 | @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 |
412 | 409 | if (socket.getSocket() == null) {
|
413 | 410 | throw raise(PythonBuiltinClassType.OSError);
|
414 | 411 | }
|
415 | 412 |
|
416 |
| - if (!socket.getSocket().isOpen()) { |
| 413 | + if (!socket.isOpen()) { |
417 | 414 | throw raise(PythonBuiltinClassType.OSError);
|
418 | 415 | }
|
419 | 416 |
|
420 | 417 | 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); |
425 | 421 | return PNone.NONE;
|
426 | 422 | } catch (IOException e) {
|
427 | 423 | throw raise(PythonBuiltinClassType.OSError);
|
428 | 424 | }
|
429 | 425 | }
|
430 | 426 | }
|
431 | 427 |
|
| 428 | + @TruffleBoundary |
| 429 | + private static void doWrite(PSocket socket, ByteBuffer buffer) throws IOException { |
| 430 | + socket.getSocket().write(buffer); |
| 431 | + } |
| 432 | + |
432 | 433 | // sendall(bytes[, flags])
|
433 | 434 | @Builtin(name = "sendall", minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
|
434 | 435 | @GenerateNodeFactory
|
435 | 436 | abstract static class SendAllNode extends PythonTernaryBuiltinNode {
|
436 | 437 | @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 |
444 | 441 | 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); |
449 | 444 | return PNone.NONE;
|
450 | 445 | } catch (IOException e) {
|
451 | 446 | throw raise(PythonBuiltinClassType.OSError);
|
|
0 commit comments