Skip to content

Commit be16e64

Browse files
committed
at least on cpython 3.4, the PyBytes_Len and PyBytes_AsString functions are forgiving and deal with unicode objects, too
1 parent b15d80c commit be16e64

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,14 +957,23 @@ Object doUnicode(String s, long elementSize, long elements, Object errorMarker)
957957
@GenerateNodeFactory
958958
abstract static class PyTruffle_Bytes_AsString extends NativeBuiltin {
959959
@Specialization
960-
Object doUnicode(PBytes bytes, @SuppressWarnings("unused") Object errorMarker) {
960+
Object doBytes(PBytes bytes, @SuppressWarnings("unused") Object errorMarker) {
961961
// according to Python's documentation, the last byte is always '0x00'
962962
byte[] store = bytes.getInternalByteArray();
963963
byte[] nativeBytes = Arrays.copyOf(store, store.length + 1);
964964
assert nativeBytes[nativeBytes.length - 1] == 0;
965965
return getContext().getEnv().asGuestValue(nativeBytes);
966966
}
967967

968+
@Specialization
969+
Object doUnicode(PString str, @SuppressWarnings("unused") Object errorMarker) {
970+
// according to Python's documentation, the last byte is always '0x00'
971+
byte[] store = str.getValue().getBytes();
972+
byte[] nativeBytes = Arrays.copyOf(store, store.length + 1);
973+
assert nativeBytes[nativeBytes.length - 1] == 0;
974+
return getContext().getEnv().asGuestValue(nativeBytes);
975+
}
976+
968977
@Fallback
969978
Object doUnicode(@SuppressWarnings("unused") Object o, Object errorMarker) {
970979
return errorMarker;

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def PyBytes_AsStringCheckEmbeddedNull(obj, encoding):
193193

194194

195195
def PyBytes_Size(obj):
196-
assert isinstance(obj, bytes)
197196
return PyObject_Size(obj)
198197

199198

0 commit comments

Comments
 (0)