Skip to content

Commit d6ecf4e

Browse files
committed
When str is a polyglot object already in PyBytes_FromString(AndSize), we need the size argument to be able to take the subrange
1 parent c3866a7 commit d6ecf4e

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

graalpython/com.oracle.graal.python.cext/src/bytesobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ UPCALL_ID(PyBytes_FromStringAndSize);
5252
UPCALL_ID(PyTruffle_Bytes_EmptyWithCapacity);
5353
PyObject* PyBytes_FromStringAndSize(const char* str, Py_ssize_t sz) {
5454
if (str != NULL) {
55-
return UPCALL_CEXT_O(_jls_PyBytes_FromStringAndSize, polyglot_from_i8_array(str, sz));
55+
return UPCALL_CEXT_O(_jls_PyBytes_FromStringAndSize, polyglot_from_i8_array(str, sz), sz);
5656
}
5757
return UPCALL_CEXT_O(_jls_PyTruffle_Bytes_EmptyWithCapacity, sz);
5858
}
5959

6060
PyObject * PyBytes_FromString(const char *str) {
6161
if (str != NULL) {
62-
return UPCALL_CEXT_O(_jls_PyBytes_FromStringAndSize, polyglot_from_i8_array(str, strlen(str)));
62+
return UPCALL_CEXT_O(_jls_PyBytes_FromStringAndSize, polyglot_from_i8_array(str, strlen(str)), strlen(str));
6363
}
6464
return UPCALL_CEXT_O(_jls_PyTruffle_Bytes_EmptyWithCapacity, 0);
6565
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/TruffleCextBuiltins.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.nio.charset.CharsetEncoder;
5656
import java.nio.charset.CodingErrorAction;
5757
import java.nio.charset.StandardCharsets;
58+
import java.util.Arrays;
5859
import java.util.List;
5960

6061
import com.oracle.graal.python.PythonLanguage;
@@ -70,8 +71,8 @@
7071
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
7172
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins;
7273
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
73-
import com.oracle.graal.python.builtins.objects.bytes.PByteArray;
7474
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
75+
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
7576
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CByteArrayWrapper;
7677
import com.oracle.graal.python.builtins.objects.cext.CArrayWrappers.CStringWrapper;
7778
import com.oracle.graal.python.builtins.objects.cext.CExtNodes;
@@ -2136,29 +2137,28 @@ protected static boolean isPSequence(Object object) {
21362137
}
21372138
}
21382139

2139-
@Builtin(name = "PyBytes_FromStringAndSize", minNumOfPositionalArgs = 2, declaresExplicitSelf = true)
2140+
@Builtin(name = "PyBytes_FromStringAndSize", minNumOfPositionalArgs = 3, declaresExplicitSelf = true)
21402141
@GenerateNodeFactory
21412142
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
21432144
// managed, when the PySequenceArrayWrapper that we used never went
21442145
// native, and during the upcall to here it was simply unwrapped again
21452146
// with the ToJava (rather than mapped from a native pointer back into a
21462147
// PythonNativeObject)
21472148

21482149
@Specialization
2149-
Object doGeneric(@SuppressWarnings("unused") Object module, PByteArray object,
2150+
Object doGeneric(@SuppressWarnings("unused") Object module, PIBytesLike object, long size,
21502151
@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+
}
21582158
}
21592159

21602160
@Specialization
2161-
Object doGeneric(Object module, PythonNativeObject object,
2161+
Object doGeneric(Object module, PythonNativeObject object, @SuppressWarnings("unused") long size,
21622162
@Exclusive @Cached CExtNodes.GetNativeNullNode getNativeNullNode,
21632163
@Exclusive @Cached GetByteArrayNode getByteArrayNode) {
21642164
try {

0 commit comments

Comments
 (0)