|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.builtins.modules.io; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.PBufferedWriter; |
| 44 | +import static com.oracle.graal.python.nodes.ErrorMessages.BUF_SIZE_POS; |
| 45 | +import static com.oracle.graal.python.nodes.ErrorMessages.IO_STREAM_DETACHED; |
| 46 | +import static com.oracle.graal.python.nodes.ErrorMessages.IO_UNINIT; |
| 47 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError; |
| 48 | + |
| 49 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 50 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 51 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 52 | +import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary; |
| 53 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 54 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode; |
| 55 | +import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode; |
| 56 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 57 | +import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider; |
| 58 | +import com.oracle.truffle.api.CompilerDirectives; |
| 59 | +import com.oracle.truffle.api.dsl.Specialization; |
| 60 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 61 | + |
| 62 | +abstract class AbstractBufferedIOBuiltins extends PythonBuiltins { |
| 63 | + |
| 64 | + protected static final int DEFAULT_BUFFER_SIZE = IOModuleBuiltins.DEFAULT_BUFFER_SIZE; |
| 65 | + |
| 66 | + protected static final String DETACH = "detach"; |
| 67 | + protected static final String FLUSH = "flush"; |
| 68 | + protected static final String CLOSE = "close"; |
| 69 | + protected static final String SEEKABLE = "seekable"; |
| 70 | + protected static final String READABLE = "readable"; |
| 71 | + protected static final String WRITABLE = "writable"; |
| 72 | + protected static final String FILENO = "fileno"; |
| 73 | + protected static final String ISATTY = "isatty"; |
| 74 | + protected static final String _DEALLOC_WARN = "_dealloc_warn"; |
| 75 | + |
| 76 | + protected static final String READ = "read"; |
| 77 | + protected static final String PEEK = "peek"; |
| 78 | + protected static final String READ1 = "read1"; |
| 79 | + protected static final String READINTO = "readinto"; |
| 80 | + protected static final String READINTO1 = "readinto1"; |
| 81 | + protected static final String READLINE = "readline"; |
| 82 | + protected static final String READLINES = "readlines"; |
| 83 | + protected static final String WRITELINES = "writelines"; |
| 84 | + protected static final String WRITE = "write"; |
| 85 | + protected static final String SEEK = "seek"; |
| 86 | + protected static final String TELL = "tell"; |
| 87 | + protected static final String TRUNCATE = "truncate"; |
| 88 | + |
| 89 | + protected static final String RAW = "raw"; |
| 90 | + protected static final String _FINALIZING = "_finalizing"; |
| 91 | + |
| 92 | + protected static final String CLOSED = "closed"; |
| 93 | + protected static final String NAME = "name"; |
| 94 | + protected static final String MODE = "mode"; |
| 95 | + |
| 96 | + public abstract static class BaseInitNode extends PythonTernaryClinicBuiltinNode { |
| 97 | + |
| 98 | + @Child BufferedIONodes.RawTellNode rawTellNode = BufferedIONodesFactory.RawTellNodeGen.create(true); |
| 99 | + |
| 100 | + @Override |
| 101 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 102 | + throw CompilerDirectives.shouldNotReachHere("abstract method"); |
| 103 | + } |
| 104 | + |
| 105 | + protected boolean isFileIO(PBuffered self, Object raw, |
| 106 | + PythonObjectLibrary libSelf, |
| 107 | + PythonObjectLibrary libRaw) { |
| 108 | + return raw instanceof PFileIO && |
| 109 | + libSelf.getLazyPythonClass(self) == PBufferedWriter && |
| 110 | + libRaw.getLazyPythonClass(raw) == PythonBuiltinClassType.PFileIO; |
| 111 | + } |
| 112 | + |
| 113 | + protected void bufferedInit(VirtualFrame frame, PBuffered self, int bufferSize) { |
| 114 | + self.initBuffer(bufferSize); |
| 115 | + self.setLock(factory().createRLock()); |
| 116 | + self.setOwner(0); |
| 117 | + int n; |
| 118 | + for (n = bufferSize - 1; (n & 1) != 0; n >>= 1) { |
| 119 | + } |
| 120 | + int mask = n == 0 ? bufferSize - 1 : 0; |
| 121 | + self.setBufferMask(mask); |
| 122 | + rawTellNode.execute(frame, self); |
| 123 | + } |
| 124 | + |
| 125 | + @SuppressWarnings("unused") |
| 126 | + @Specialization(guards = "bufferSize <= 0") |
| 127 | + public PNone bufferSizeError(VirtualFrame frame, PBuffered self, Object raw, int bufferSize) { |
| 128 | + throw raise(ValueError, BUF_SIZE_POS); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + abstract static class PythonBinaryWithInitErrorClinicBuiltinNode extends PythonBinaryClinicBuiltinNode { |
| 133 | + @Override |
| 134 | + protected ArgumentClinicProvider getArgumentClinic() { |
| 135 | + throw CompilerDirectives.shouldNotReachHere("abstract"); |
| 136 | + } |
| 137 | + |
| 138 | + @SuppressWarnings("unused") |
| 139 | + @Specialization(guards = "!self.isOK()") |
| 140 | + Object initError(VirtualFrame frame, PBuffered self, Object o) { |
| 141 | + if (self.isDetached()) { |
| 142 | + throw raise(ValueError, IO_STREAM_DETACHED); |
| 143 | + } else { |
| 144 | + throw raise(ValueError, IO_UNINIT); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + abstract static class PythonBinaryWithInitErrorBuiltinNode extends PythonBinaryBuiltinNode { |
| 150 | + |
| 151 | + @SuppressWarnings("unused") |
| 152 | + @Specialization(guards = "!self.isOK()") |
| 153 | + Object initError(VirtualFrame frame, PBuffered self, Object buffer) { |
| 154 | + if (self.isDetached()) { |
| 155 | + throw raise(ValueError, IO_STREAM_DETACHED); |
| 156 | + } else { |
| 157 | + throw raise(ValueError, IO_UNINIT); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + abstract static class PythonUnaryWithInitErrorBuiltinNode extends PythonUnaryBuiltinNode { |
| 163 | + @SuppressWarnings("unused") |
| 164 | + @Specialization(guards = "!self.isOK()") |
| 165 | + Object initError(PBuffered self) { |
| 166 | + if (self.isDetached()) { |
| 167 | + throw raise(ValueError, IO_STREAM_DETACHED); |
| 168 | + } else { |
| 169 | + throw raise(ValueError, IO_UNINIT); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments