def PyUnicode_EncodeASCII(space, s, size, errors): """Encode the Py_UNICODE buffer of the given size using ASCII and return a Python string object. Return NULL if an exception was raised by the codec. """ w_s = space.wrap(rffi.wcharpsize2unicode(s, size)) return space.call_method(w_s, 'encode', space.wrap('ascii'))
def test_copy(self, space, api): w_x = space.wrap(u"abcd\u0660") target_chunk, _ = rffi.alloc_unicodebuffer(space.int_w(space.len(w_x))) #lltype.malloc(Py_UNICODE, space.int_w(space.len(w_x)), flavor='raw') x_chunk = api.PyUnicode_AS_UNICODE(w_x) api.Py_UNICODE_COPY(target_chunk, x_chunk, 4) w_y = space.wrap(rffi.wcharpsize2unicode(target_chunk, 4)) assert space.eq_w(w_y, space.wrap(u"abcd")) size = api.PyUnicode_GET_SIZE(w_x) api.Py_UNICODE_COPY(target_chunk, x_chunk, size) w_y = space.wrap(rffi.wcharpsize2unicode(target_chunk, size)) assert space.eq_w(w_y, w_x) lltype.free(target_chunk, flavor='raw')
def PyUnicode_EncodeXXX(space, s, size, errors): """Encode the Py_UNICODE buffer of the given size and return a Python string object. Return NULL if an exception was raised by the codec.""" w_u = space.wrap(rffi.wcharpsize2unicode(s, size)) if errors: w_errors = space.wrap(rffi.charp2str(errors)) else: w_errors = space.w_None return space.call_method(w_u, 'encode', space.wrap(encoding), w_errors)
def unicode_realize(space, py_obj): """ Creates the unicode in the interpreter. The PyUnicodeObject buffer must not be modified after this call. """ py_uni = rffi.cast(PyUnicodeObject, py_obj) s = rffi.wcharpsize2unicode(py_uni.c_buffer, py_uni.c_size) w_obj = space.wrap(s) track_reference(space, py_obj, w_obj) return w_obj
def PyUnicode_EncodeXXX(space, s, size, errors): """Encode the Py_UNICODE buffer of the given size and return a Python string object. Return NULL if an exception was raised by the codec.""" w_u = space.wrap(rffi.wcharpsize2unicode(s, size)) if errors: w_errors = space.wrap(rffi.charp2str(errors)) else: w_errors = space.w_None return space.call_method(w_u, "encode", space.wrap(encoding), w_errors)
def PyUnicode_EncodeMBCS(space, wchar_p, length, errors): """Encode the Py_UNICODE buffer of the given size using MBCS and return a Python string object. Return NULL if an exception was raised by the codec. """ w_unicode = space.wrap(rffi.wcharpsize2unicode(wchar_p, length)) if errors: w_errors = space.wrap(rffi.charp2str(errors)) else: w_errors = space.w_None return space.call_method(w_unicode, "encode", space.wrap("mbcs"), w_errors)
def PyUnicode_FromUnicode(space, wchar_p, length): """Create a Unicode Object from the Py_UNICODE buffer u of the given size. u may be NULL which causes the contents to be undefined. It is the user's responsibility to fill in the needed data. The buffer is copied into the new object. If the buffer is not NULL, the return value might be a shared object. Therefore, modification of the resulting Unicode object is only allowed when u is NULL.""" if wchar_p: s = rffi.wcharpsize2unicode(wchar_p, length) return make_ref(space, space.wrap(s)) else: return rffi.cast(PyObject, new_empty_unicode(space, length))
def decodeex(decodebuf, stringdata, errors="strict", errorcb=None, namecb=None, ignore_error=0): inleft = len(stringdata) inbuf = rffi.get_nonmovingbuffer(stringdata) try: if pypy_cjk_dec_init(decodebuf, inbuf, inleft) < 0: raise MemoryError while True: r = pypy_cjk_dec_chunk(decodebuf) if r == 0 or r == ignore_error: break multibytecodec_decerror(decodebuf, r, errors, errorcb, namecb, stringdata) src = pypy_cjk_dec_outbuf(decodebuf) length = pypy_cjk_dec_outlen(decodebuf) return rffi.wcharpsize2unicode(src, length) # finally: rffi.free_nonmovingbuffer(stringdata, inbuf)
def decode(codec, stringdata, errors="strict", errorcb=None, namecb=None): inleft = len(stringdata) inbuf = rffi.get_nonmovingbuffer(stringdata) try: decodebuf = pypy_cjk_dec_init(codec, inbuf, inleft) if not decodebuf: raise MemoryError try: while True: r = pypy_cjk_dec_chunk(decodebuf) if r == 0: break multibytecodec_decerror(decodebuf, r, errors, errorcb, namecb, stringdata) src = pypy_cjk_dec_outbuf(decodebuf) length = pypy_cjk_dec_outlen(decodebuf) return rffi.wcharpsize2unicode(src, length) # finally: pypy_cjk_dec_free(decodebuf) # finally: rffi.free_nonmovingbuffer(stringdata, inbuf)
def wcharp2rawunicode(space, address, maxlength=-1): if maxlength == -1: return wcharp2unicode(space, address) s = rffi.wcharpsize2unicode(rffi.cast(rffi.CWCHARP, address), maxlength) return space.wrap(s)
def array_tounicode__Array(space, self): return space.wrap(rffi.wcharpsize2unicode(self.buffer, self.len))