Beispiel #1
0
    def str_decode_mbcs(s, size, errors, final=False, errorhandler=None):
        if size == 0:
            return u"", 0

        if errorhandler is None:
            errorhandler = raise_unicode_exception_decode

        # Skip trailing lead-byte unless 'final' is set
        if not final and is_dbcs_lead_byte(s[size - 1]):
            size -= 1

        dataptr = rffi.get_nonmovingbuffer(s)
        try:
            # first get the size of the result
            usize = MultiByteToWideChar(CP_ACP, 0, dataptr, size,
                                        lltype.nullptr(rffi.CWCHARP.TO), 0)
            if usize == 0:
                raise rwin32.lastWindowsError()

            raw_buf, gc_buf = rffi.alloc_unicodebuffer(usize)
            try:
                # do the conversion
                if MultiByteToWideChar(CP_ACP, 0, dataptr, size, raw_buf,
                                       usize) == 0:
                    raise rwin32.lastWindowsError()

                return (rffi.unicode_from_buffer(raw_buf, gc_buf, usize,
                                                 usize), size)
            finally:
                rffi.keep_unicodebuffer_alive_until_here(raw_buf, gc_buf)
        finally:
            rffi.free_nonmovingbuffer(s, dataptr)
    def str_decode_mbcs(s, size, errors, final=False,
                        errorhandler=None):
        if size == 0:
            return u"", 0

        if errorhandler is None:
            errorhandler = raise_unicode_exception_decode

        # Skip trailing lead-byte unless 'final' is set
        if not final and is_dbcs_lead_byte(s[size-1]):
            size -= 1

        dataptr = rffi.get_nonmovingbuffer(s)
        try:
            # first get the size of the result
            usize = MultiByteToWideChar(CP_ACP, 0,
                                        dataptr, size,
                                        lltype.nullptr(rffi.CWCHARP.TO), 0)
            if usize == 0:
                raise rwin32.lastWindowsError()

            raw_buf, gc_buf = rffi.alloc_unicodebuffer(usize)
            try:
                # do the conversion
                if MultiByteToWideChar(CP_ACP, 0,
                                       dataptr, size, raw_buf, usize) == 0:
                    raise rwin32.lastWindowsError()

                return (rffi.unicode_from_buffer(raw_buf, gc_buf, usize, usize),
                        size)
            finally:
                rffi.keep_unicodebuffer_alive_until_here(raw_buf, gc_buf)
        finally:
            rffi.free_nonmovingbuffer(s, dataptr)
Beispiel #3
0
    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')
Beispiel #4
0
    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')