def test_string_buffer(self, space, api): py_str = new_empty_str(space, 10) c_buf = py_str.c_ob_type.c_tp_as_buffer assert c_buf py_obj = rffi.cast(PyObject, py_str) assert c_buf.c_bf_getsegcount(py_obj, lltype.nullptr(Py_ssize_tP.TO)) == 1 ref = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw') assert c_buf.c_bf_getsegcount(py_obj, ref) == 1 assert ref[0] == 10 lltype.free(ref, flavor='raw') ref = lltype.malloc(rffi.VOIDPP.TO, 1, flavor='raw') assert c_buf.c_bf_getreadbuffer(py_obj, 0, ref) == 10 lltype.free(ref, flavor='raw') Py_DecRef(space, py_obj)
def test_string_resize(self, space, api): py_str = new_empty_str(space, 10) ar = lltype.malloc(PyObjectP.TO, 1, flavor='raw') py_str.c_buffer[0] = 'a' py_str.c_buffer[1] = 'b' py_str.c_buffer[2] = 'c' ar[0] = rffi.cast(PyObject, py_str) api._PyString_Resize(ar, 3) py_str = rffi.cast(PyStringObject, ar[0]) assert py_str.c_size == 3 assert py_str.c_buffer[1] == 'b' assert py_str.c_buffer[3] == '\x00' # the same for growing ar[0] = rffi.cast(PyObject, py_str) api._PyString_Resize(ar, 10) py_str = rffi.cast(PyStringObject, ar[0]) assert py_str.c_size == 10 assert py_str.c_buffer[1] == 'b' assert py_str.c_buffer[10] == '\x00' Py_DecRef(space, ar[0]) lltype.free(ar, flavor='raw')