Skip to content

Commit b796402

Browse files
committed
Fix PyBytes_FromStringAndSize
1 parent a30f647 commit b796402

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ PyTypeObject PyBytes_Type = PY_TRUFFLE_TYPE_WITH_ITEMSIZE("bytes", &PyType_Type,
5050

5151
typedef PyObject* (*fromStringAndSize_fun_t)(int8_t* str, int64_t sz);
5252

53+
UPCALL_ID(PyBytes_Size);
54+
Py_ssize_t PyBytes_Size(PyObject *bytes) {
55+
return UPCALL_CEXT_L(_jls_PyBytes_Size, native_to_java(bytes));
56+
}
57+
5358
UPCALL_ID(PyBytes_FromStringAndSize);
5459
UPCALL_ID(PyTruffle_Bytes_EmptyWithCapacity);
5560
PyObject* PyBytes_FromStringAndSize(const char* str, Py_ssize_t sz) {
@@ -71,22 +76,16 @@ char* PyBytes_AsString(PyObject *obj) {
7176
return (char*)(UPCALL_CEXT_NOCAST(_jls_PyTruffle_Bytes_AsString, native_to_java(obj), ERROR_MARKER));
7277
}
7378

74-
UPCALL_ID(PyBytes_AsStringCheckEmbeddedNull);
79+
UPCALL_ID(PyTruffle_Bytes_CheckEmbeddedNull);
7580
int PyBytes_AsStringAndSize(PyObject *obj, char **s, Py_ssize_t *len) {
76-
setlocale(LC_ALL, NULL);
77-
const char* encoding = nl_langinfo(CODESET);
78-
PyObject *result = UPCALL_CEXT_O(_jls_PyBytes_AsStringCheckEmbeddedNull, native_to_java(obj), polyglot_from_string(encoding, SRC_CS));
79-
if(result == NULL) {
80-
return -1;
81-
}
82-
83-
*s = (char *)as_char_pointer(result);
84-
81+
PyObject* resolved = native_to_java(obj);
82+
*s = (char*)(UPCALL_CEXT_NOCAST(_jls_PyTruffle_Bytes_AsString, resolved, ERROR_MARKER));
8583
if (len != NULL) {
86-
*len = polyglot_as_i64(polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Object_LEN", native_to_java(obj)));
84+
*len = UPCALL_CEXT_L(_jls_PyBytes_Size, resolved);
85+
return 0;
86+
} else {
87+
return UPCALL_CEXT_I(_jls_PyTruffle_Bytes_CheckEmbeddedNull, resolved);
8788
}
88-
89-
return 0;
9089
}
9190

9291
PyObject * PyBytes_FromFormat(const char *format, ...) {
@@ -261,11 +260,6 @@ void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart) {
261260
Py_DECREF(newpart);
262261
}
263262

264-
UPCALL_ID(PyBytes_Size);
265-
Py_ssize_t PyBytes_Size(PyObject *bytes) {
266-
return UPCALL_CEXT_L(_jls_PyBytes_Size, native_to_java(bytes));
267-
}
268-
269263
int bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags) {
270264
return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self), 1, flags);
271265
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@
171171
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
172172
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
173173
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
174+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.GetItemScalarNode;
175+
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.LenNode;
174176
import com.oracle.graal.python.builtins.objects.dict.PDict;
175177
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
176178
import com.oracle.graal.python.builtins.objects.frame.PFrame;
@@ -3829,4 +3831,28 @@ static Object doGeneric(VirtualFrame frame, String format, Object vaList,
38293831
}
38303832
}
38313833
}
3834+
3835+
@Builtin(name = "PyTruffle_Bytes_CheckEmbeddedNull", minNumOfPositionalArgs = 1)
3836+
@GenerateNodeFactory
3837+
abstract static class PyTruffleBytesCheckEmbeddedNull extends PythonUnaryBuiltinNode {
3838+
3839+
@Specialization
3840+
static int doBytes(PBytes bytes,
3841+
@Cached SequenceStorageNodes.LenNode lenNode,
3842+
@Cached GetItemScalarNode getItemScalarNode) {
3843+
3844+
SequenceStorage sequenceStorage = bytes.getSequenceStorage();
3845+
int len = lenNode.execute(sequenceStorage);
3846+
try {
3847+
for (int i = 0; i < len; i++) {
3848+
if (getItemScalarNode.executeInt(sequenceStorage, i) == 0) {
3849+
return -1;
3850+
}
3851+
}
3852+
} catch (ClassCastException e) {
3853+
throw CompilerDirectives.shouldNotReachHere("bytes object contains non-int value");
3854+
}
3855+
return 0;
3856+
}
3857+
}
38323858
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -283,23 +283,8 @@ def PyObject_LEN(obj):
283283
return len(obj)
284284

285285

286-
def PyTruffle_Object_LEN(obj):
287-
return len(to_java(obj))
288-
289-
290286
##################### BYTES
291287

292-
@may_raise(-1)
293-
def PyBytes_AsStringCheckEmbeddedNull(obj, encoding):
294-
if not PyBytes_Check(obj):
295-
raise TypeError('expected bytes, {!s} found'.format(type(obj)))
296-
result = obj.decode(encoding)
297-
for ch in obj:
298-
if ch == 0:
299-
raise ValueError('embedded null byte')
300-
return result
301-
302-
303288
def PyBytes_Size(obj):
304289
return PyObject_Size(obj)
305290

0 commit comments

Comments
 (0)