|
68 | 68 | import com.oracle.graal.python.builtins.objects.PNotImplemented;
|
69 | 69 | import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
|
70 | 70 | import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
|
| 71 | +import com.oracle.graal.python.builtins.objects.bytes.PByteArray; |
71 | 72 | import com.oracle.graal.python.builtins.objects.bytes.PBytes;
|
72 | 73 | import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
|
73 | 74 | import com.oracle.graal.python.builtins.objects.cell.PCell;
|
|
134 | 135 | import com.oracle.graal.python.runtime.exception.PException;
|
135 | 136 | import com.oracle.graal.python.runtime.exception.PythonErrorType;
|
136 | 137 | import com.oracle.graal.python.runtime.sequence.PSequence;
|
| 138 | +import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage; |
137 | 139 | import com.oracle.truffle.api.CompilerAsserts;
|
138 | 140 | import com.oracle.truffle.api.CompilerDirectives;
|
139 | 141 | import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
|
@@ -166,6 +168,8 @@ protected abstract static class CreateByteOrByteArrayNode extends PythonBuiltinN
|
166 | 168 | @Child private IsIndexNode isIndexNode;
|
167 | 169 | @Child private CastToIndexNode castToIndexNode;
|
168 | 170 |
|
| 171 | + private final IsBuiltinClassProfile isClassProfile = IsBuiltinClassProfile.create(); |
| 172 | + |
169 | 173 | @SuppressWarnings("unused")
|
170 | 174 | protected Object create(PythonClass cls, byte[] barr) {
|
171 | 175 | throw new AssertionError("should not reach");
|
@@ -193,6 +197,24 @@ public Object fromString(PythonClass cls, String source, PNone encoding, PNone e
|
193 | 197 | throw raise(PythonErrorType.TypeError, "string argument without an encoding");
|
194 | 198 | }
|
195 | 199 |
|
| 200 | + protected boolean isSimpleBytes(PBytes iterable) { |
| 201 | + return isClassProfile.profileObject(iterable, PythonBuiltinClassType.PBytes) && iterable.getSequenceStorage() instanceof ByteSequenceStorage; |
| 202 | + } |
| 203 | + |
| 204 | + @Specialization(guards = {"isSimpleBytes(iterable)", "isNoValue(encoding)", "isNoValue(errors)"}) |
| 205 | + public Object bytearray(PythonClass cls, PBytes iterable, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) { |
| 206 | + return create(cls, (byte[]) ((ByteSequenceStorage) iterable.getSequenceStorage()).getCopyOfInternalArrayObject()); |
| 207 | + } |
| 208 | + |
| 209 | + protected boolean isSimpleBytes(PByteArray iterable) { |
| 210 | + return isClassProfile.profileObject(iterable, PythonBuiltinClassType.PByteArray) && iterable.getSequenceStorage() instanceof ByteSequenceStorage; |
| 211 | + } |
| 212 | + |
| 213 | + @Specialization(guards = {"isSimpleBytes(iterable)", "isNoValue(encoding)", "isNoValue(errors)"}) |
| 214 | + public Object bytearray(PythonClass cls, PByteArray iterable, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors) { |
| 215 | + return create(cls, (byte[]) ((ByteSequenceStorage) iterable.getSequenceStorage()).getCopyOfInternalArrayObject()); |
| 216 | + } |
| 217 | + |
196 | 218 | @Specialization(guards = {"!isInt(iterable)", "!isNoValue(iterable)", "isNoValue(encoding)", "isNoValue(errors)"})
|
197 | 219 | public Object bytearray(PythonClass cls, Object iterable, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
|
198 | 220 | @Cached("create()") GetIteratorNode getIteratorNode,
|
|
0 commit comments