Skip to content

Commit b37ca66

Browse files
committed
[GR-12448] Fix PyBytes_FromStringAndSize.
PullRequest: graalpython/268
2 parents 7097f9b + 3bc45bc commit b37ca66

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,19 @@
4949
PyTypeObject PyBytes_Type = PY_TRUFFLE_TYPE("bytes", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_BYTES_SUBCLASS, PyBytesObject_SIZE);
5050

5151
UPCALL_ID(PyBytes_FromStringAndSize);
52+
UPCALL_ID(PyTruffle_Bytes_EmptyWithCapacity);
5253
PyObject* PyBytes_FromStringAndSize(const char* str, Py_ssize_t sz) {
53-
setlocale(LC_ALL, NULL);
54-
const char* encoding = nl_langinfo(CODESET);
55-
void *jstr = str != NULL ? polyglot_from_string_n(str, sz, SRC_CS) : to_java(NULL);
56-
return UPCALL_CEXT_O(_jls_PyBytes_FromStringAndSize, jstr, sz, polyglot_from_string(encoding, SRC_CS));
54+
if (str != NULL) {
55+
return UPCALL_CEXT_O(_jls_PyBytes_FromStringAndSize, polyglot_from_i8_array(str, sz));
56+
}
57+
return UPCALL_CEXT_O(_jls_PyTruffle_Bytes_EmptyWithCapacity, sz);
5758
}
5859

5960
PyObject * PyBytes_FromString(const char *str) {
60-
setlocale(LC_ALL, NULL);
61-
const char* encoding = nl_langinfo(CODESET);
62-
return UPCALL_CEXT_O(_jls_PyBytes_FromStringAndSize, polyglot_from_string(str, SRC_CS), 0, polyglot_from_string(encoding, SRC_CS));
61+
if (str != NULL) {
62+
return UPCALL_CEXT_O(_jls_PyBytes_FromStringAndSize, polyglot_from_i8_array(str, strlen(str)));
63+
}
64+
return UPCALL_CEXT_O(_jls_PyTruffle_Bytes_EmptyWithCapacity, 0);
6365
}
6466

6567
UPCALL_ID(PyTruffle_Bytes_AsString);

graalpython/com.oracle.graal.python.test/src/tests/test_memoryview.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ def test_tobytes():
4646
b = b"hello"
4747
v = memoryview(b)
4848
assert v.tobytes() == b
49+
50+
b = b"\xff\x00\x00"
51+
v = memoryview(b)
52+
assert v.tobytes() == b

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,47 +1646,42 @@ Object call(PBuiltinFunction function) {
16461646
}
16471647
}
16481648

1649-
@Builtin(name = "PyTruffle_Bytes_EmptyWithCapacity", fixedNumOfPositionalArgs = 2)
1649+
@Builtin(name = "PyTruffle_Bytes_EmptyWithCapacity", fixedNumOfPositionalArgs = 1)
16501650
@GenerateNodeFactory
1651-
abstract static class PyTruffle_Bytes_EmptyWithCapacity extends NativeBuiltin {
1651+
abstract static class PyTruffle_Bytes_EmptyWithCapacity extends PythonUnaryBuiltinNode {
16521652

16531653
@Specialization
1654-
PBytes doInt(int size, @SuppressWarnings("unused") Object errorMarker) {
1654+
PBytes doInt(int size) {
16551655
return factory().createBytes(new byte[size]);
16561656
}
16571657

16581658
@Specialization(rewriteOn = ArithmeticException.class)
1659-
PBytes doLong(long size, Object errorMarker) {
1660-
return doInt(PInt.intValueExact(size), errorMarker);
1659+
PBytes doLong(long size) {
1660+
return doInt(PInt.intValueExact(size));
16611661
}
16621662

16631663
@Specialization(replaces = "doLong")
1664-
PBytes doLongOvf(long size, Object errorMarker) {
1664+
PBytes doLongOvf(long size) {
16651665
try {
1666-
return doInt(PInt.intValueExact(size), errorMarker);
1666+
return doInt(PInt.intValueExact(size));
16671667
} catch (ArithmeticException e) {
16681668
throw raiseIndexError();
16691669
}
16701670
}
16711671

16721672
@Specialization(rewriteOn = ArithmeticException.class)
1673-
PBytes doPInt(PInt size, Object errorMarker) {
1674-
return doInt(size.intValueExact(), errorMarker);
1673+
PBytes doPInt(PInt size) {
1674+
return doInt(size.intValueExact());
16751675
}
16761676

16771677
@Specialization(replaces = "doPInt")
1678-
PBytes doPIntOvf(PInt size, Object errorMarker) {
1678+
PBytes doPIntOvf(PInt size) {
16791679
try {
1680-
return doInt(size.intValueExact(), errorMarker);
1680+
return doInt(size.intValueExact());
16811681
} catch (ArithmeticException e) {
16821682
throw raiseIndexError();
16831683
}
16841684
}
1685-
1686-
@Fallback
1687-
Object doGeneric(Object size, Object errorMarker) {
1688-
return raiseNative(errorMarker, TypeError, "expected 'int', but was '%p'", size);
1689-
}
16901685
}
16911686

16921687
@Builtin(name = "PyTruffle_Upcall", minNumOfPositionalArgs = 3, takesVarArgs = true, declaresExplicitSelf = true)
@@ -2095,4 +2090,17 @@ protected static boolean isPSequence(Object object) {
20952090
return object instanceof PList || object instanceof PTuple;
20962091
}
20972092
}
2093+
2094+
@Builtin(name = "PyBytes_FromStringAndSize", fixedNumOfPositionalArgs = 1)
2095+
@GenerateNodeFactory
2096+
abstract static class PyBytes_FromStringAndSize extends NativeBuiltin {
2097+
2098+
@Specialization
2099+
PBytes doGeneric(PythonNativeObject object) {
2100+
if (object.object instanceof TruffleObject) {
2101+
return factory().createBytes(getByteArray((TruffleObject) object.object));
2102+
}
2103+
throw raise(TypeError, "invalid pointer: %s", object.object);
2104+
}
2105+
}
20982106
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,6 @@ def PyTruffle_Object_LEN(obj):
215215

216216
##################### BYTES
217217

218-
def PyBytes_FromStringAndSize(string, size, encoding):
219-
if string is not None and string is not Py_NoValue():
220-
return bytes(string, encoding)
221-
assert size >= 0;
222-
return PyTruffle_Bytes_EmptyWithCapacity(size, native_null)
223-
224-
225218
def PyBytes_AsStringCheckEmbeddedNull(obj, encoding):
226219
if not PyBytes_Check(obj):
227220
raise TypeError('expected bytes, {!s} found'.format(type(obj)))

0 commit comments

Comments
 (0)