|
119 | 119 | import com.oracle.graal.python.builtins.modules.io.IONodes.IOMode;
|
120 | 120 | import com.oracle.graal.python.builtins.objects.PNone;
|
121 | 121 | import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAccessLibrary;
|
122 |
| -import com.oracle.graal.python.builtins.objects.bytes.BytesNodes; |
123 | 122 | import com.oracle.graal.python.builtins.objects.bytes.PBytes;
|
124 | 123 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
|
125 | 124 | import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
|
@@ -710,40 +709,44 @@ protected ArgumentClinicProvider getArgumentClinic() {
|
710 | 709 | }
|
711 | 710 | }
|
712 | 711 |
|
713 |
| - @Builtin(name = J_WRITE, minNumOfPositionalArgs = 2) |
| 712 | + @Builtin(name = J_WRITE, minNumOfPositionalArgs = 2, numOfPositionalOnlyArgs = 2, parameterNames = {"$self", "b"}) |
| 713 | + @ArgumentClinic(name = "b", conversion = ArgumentClinic.ClinicConversion.ReadableBuffer) |
714 | 714 | @GenerateNodeFactory
|
715 |
| - public abstract static class WriteNode extends PythonBinaryBuiltinNode { |
| 715 | + public abstract static class WriteNode extends PythonBinaryClinicBuiltinNode { |
716 | 716 |
|
717 |
| - @Specialization(guards = {"!self.isClosed()", "self.isWritable()"}) |
718 |
| - static Object write(VirtualFrame frame, PFileIO self, Object data, |
| 717 | + @Specialization(limit = "3") |
| 718 | + static Object write(VirtualFrame frame, PFileIO self, Object buffer, |
719 | 719 | @Bind("this") Node inliningTarget,
|
720 |
| - @Cached BytesNodes.ToBytesNode toBytes, |
| 720 | + @CachedLibrary("buffer") PythonBufferAccessLibrary bufferLib, |
721 | 721 | @CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib,
|
722 | 722 | @Cached InlinedBranchProfile errorProfile,
|
723 | 723 | @Cached GilNode gil,
|
724 |
| - @Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode) { |
725 |
| - byte[] bytes = toBytes.execute(frame, data); |
| 724 | + @Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode, |
| 725 | + @Cached PRaiseNode.Lazy raiseNode) { |
726 | 726 | try {
|
727 |
| - return PosixModuleBuiltins.WriteNode.write(self.getFD(), bytes, bytes.length, inliningTarget, posixLib, errorProfile, gil); |
728 |
| - } catch (PosixException e) { |
729 |
| - if (e.getErrorCode() == EAGAIN.getNumber()) { |
730 |
| - return PNone.NONE; |
| 727 | + if (self.isClosed()) { |
| 728 | + throw raiseNode.get(inliningTarget).raise(ValueError, IO_CLOSED); |
731 | 729 | }
|
732 |
| - errorProfile.enter(inliningTarget); |
733 |
| - throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e); |
| 730 | + if (!self.isWritable()) { |
| 731 | + throw raiseNode.get(inliningTarget).raise(IOUnsupportedOperation, FILE_NOT_OPEN_FOR_S, "writing"); |
| 732 | + } |
| 733 | + try { |
| 734 | + return PosixModuleBuiltins.WriteNode.write(self.getFD(), bufferLib.getInternalOrCopiedByteArray(buffer), bufferLib.getBufferLength(buffer), |
| 735 | + inliningTarget, posixLib, errorProfile, gil); |
| 736 | + } catch (PosixException e) { |
| 737 | + if (e.getErrorCode() == EAGAIN.getNumber()) { |
| 738 | + return PNone.NONE; |
| 739 | + } |
| 740 | + throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e); |
| 741 | + } |
| 742 | + } finally { |
| 743 | + bufferLib.release(buffer); |
734 | 744 | }
|
735 | 745 | }
|
736 | 746 |
|
737 |
| - @Specialization(guards = {"!self.isClosed()", "!self.isWritable()"}) |
738 |
| - static Object notWritable(@SuppressWarnings("unused") PFileIO self, @SuppressWarnings("unused") Object buf, |
739 |
| - @Shared @Cached PRaiseNode raiseNode) { |
740 |
| - throw raiseNode.raise(IOUnsupportedOperation, FILE_NOT_OPEN_FOR_S, "writing"); |
741 |
| - } |
742 |
| - |
743 |
| - @Specialization(guards = "self.isClosed()") |
744 |
| - static Object closedError(@SuppressWarnings("unused") PFileIO self, @SuppressWarnings("unused") Object buf, |
745 |
| - @Shared @Cached PRaiseNode raiseNode) { |
746 |
| - throw raiseNode.raise(ValueError, IO_CLOSED); |
| 747 | + @Override |
| 748 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 749 | + return FileIOBuiltinsClinicProviders.WriteNodeClinicProviderGen.INSTANCE; |
747 | 750 | }
|
748 | 751 | }
|
749 | 752 |
|
|
0 commit comments