|
55 | 55 | import java.nio.charset.CharsetEncoder;
|
56 | 56 | import java.nio.charset.CodingErrorAction;
|
57 | 57 | import java.nio.charset.StandardCharsets;
|
| 58 | +import java.util.Arrays; |
58 | 59 | import java.util.List;
|
59 | 60 |
|
60 | 61 | import com.oracle.graal.python.PythonLanguage;
|
|
70 | 71 | import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
|
71 | 72 | import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins;
|
72 | 73 | import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
|
73 |
| -import com.oracle.graal.python.builtins.objects.bytes.PByteArray; |
74 | 74 | import com.oracle.graal.python.builtins.objects.bytes.PBytes;
|
| 75 | +import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike; |
75 | 76 | import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CByteArrayWrapper;
|
76 | 77 | import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CStringWrapper;
|
77 | 78 | import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
|
@@ -2136,29 +2137,28 @@ protected static boolean isPSequence(Object object) {
|
2136 | 2137 | }
|
2137 | 2138 | }
|
2138 | 2139 |
|
2139 |
| - @Builtin(name = "PyBytes_FromStringAndSize", minNumOfPositionalArgs = 2, declaresExplicitSelf = true) |
| 2140 | + @Builtin(name = "PyBytes_FromStringAndSize", minNumOfPositionalArgs = 3, declaresExplicitSelf = true) |
2140 | 2141 | @GenerateNodeFactory
|
2141 | 2142 | abstract static class PyBytes_FromStringAndSize extends NativeBuiltin {
|
2142 |
| - // n.b.: the specializations for PBytes/PByteArray are quite common on |
| 2143 | + // n.b.: the specializations for PIBytesLike are quite common on |
2143 | 2144 | // managed, when the PySequenceArrayWrapper that we used never went
|
2144 | 2145 | // native, and during the upcall to here it was simply unwrapped again
|
2145 | 2146 | // with the ToJava (rather than mapped from a native pointer back into a
|
2146 | 2147 | // PythonNativeObject)
|
2147 | 2148 |
|
2148 | 2149 | @Specialization
|
2149 |
| - Object doGeneric(@SuppressWarnings("unused") Object module, PByteArray object, |
| 2150 | + Object doGeneric(@SuppressWarnings("unused") Object module, PIBytesLike object, long size, |
2150 | 2151 | @Exclusive @Cached BytesNodes.ToBytesNode getByteArrayNode) {
|
2151 |
| - return factory().createBytes(getByteArrayNode.execute(object)); |
2152 |
| - } |
2153 |
| - |
2154 |
| - @Specialization |
2155 |
| - Object doGeneric(@SuppressWarnings("unused") Object module, PBytes object, |
2156 |
| - @Exclusive @Cached BytesNodes.ToBytesNode getByteArrayNode) { |
2157 |
| - return factory().createBytes(getByteArrayNode.execute(object)); |
| 2152 | + byte[] ary = getByteArrayNode.execute(object); |
| 2153 | + if (size < Integer.MAX_VALUE && size >= 0 && size < ary.length) { |
| 2154 | + return factory().createBytes(Arrays.copyOf(ary, (int)size)); |
| 2155 | + } else { |
| 2156 | + return factory().createBytes(ary); |
| 2157 | + } |
2158 | 2158 | }
|
2159 | 2159 |
|
2160 | 2160 | @Specialization
|
2161 |
| - Object doGeneric(Object module, PythonNativeObject object, |
| 2161 | + Object doGeneric(Object module, PythonNativeObject object, @SuppressWarnings("unused") long size, |
2162 | 2162 | @Exclusive @Cached CExtNodes.GetNativeNullNode getNativeNullNode,
|
2163 | 2163 | @Exclusive @Cached GetByteArrayNode getByteArrayNode) {
|
2164 | 2164 | try {
|
|
0 commit comments