示例#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)
示例#2
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)
示例#3
0
 def send(self, data, flags=0):
     """Send a data string to the socket.  For the optional flags
     argument, see the Unix manual.  Return the number of bytes
     sent; this may be less than len(data) if the network is busy."""
     dataptr = rffi.get_nonmovingbuffer(data)
     try:
         return self.send_raw(dataptr, len(data), flags)
     finally:
         rffi.free_nonmovingbuffer(data, dataptr)
示例#4
0
文件: rsocket.py 项目: alkorzt/pypy
 def send(self, data, flags=0):
     """Send a data string to the socket.  For the optional flags
     argument, see the Unix manual.  Return the number of bytes
     sent; this may be less than len(data) if the network is busy."""
     dataptr = rffi.get_nonmovingbuffer(data)
     try:
         return self.send_raw(dataptr, len(data), flags)
     finally:
         rffi.free_nonmovingbuffer(data, dataptr)
示例#5
0
文件: rzlib.py 项目: xx312022850/pypy
def adler32(string, start=ADLER32_DEFAULT_START):
    """
    Compute the Adler-32 checksum of the string, possibly with the given
    start value, and return it as a unsigned 32 bit integer.
    """
    bytes = rffi.get_nonmovingbuffer(string)
    try:
        checksum = _adler32(start, rffi.cast(Bytefp, bytes), len(string))
    finally:
        rffi.free_nonmovingbuffer(string, bytes)
    return checksum
示例#6
0
文件: rzlib.py 项目: Debug-Orz/Sypy
def adler32(string, start=ADLER32_DEFAULT_START):
    """
    Compute the Adler-32 checksum of the string, possibly with the given
    start value, and return it as a unsigned 32 bit integer.
    """
    bytes = rffi.get_nonmovingbuffer(string)
    try:
        checksum = _adler32(start, rffi.cast(Bytefp, bytes), len(string))
    finally:
        rffi.free_nonmovingbuffer(string, bytes)
    return checksum
示例#7
0
 def sendall(self, data, flags=0):
     """Send a data string to the socket.  For the optional flags
     argument, see the Unix manual.  This calls send() repeatedly
     until all data is sent.  If an error occurs, it's impossible
     to tell how much data has been sent."""
     dataptr = rffi.get_nonmovingbuffer(data)
     try:
         remaining = len(data)
         p = dataptr
         while remaining > 0:
             res = self.send_raw(p, remaining, flags)
             p = rffi.ptradd(p, res)
             remaining -= res
     finally:
         rffi.free_nonmovingbuffer(data, dataptr)
示例#8
0
文件: rsocket.py 项目: alkorzt/pypy
 def sendall(self, data, flags=0):
     """Send a data string to the socket.  For the optional flags
     argument, see the Unix manual.  This calls send() repeatedly
     until all data is sent.  If an error occurs, it's impossible
     to tell how much data has been sent."""
     dataptr = rffi.get_nonmovingbuffer(data)
     try:
         remaining = len(data)
         p = dataptr
         while remaining > 0:
             res = self.send_raw(p, remaining, flags)
             p = rffi.ptradd(p, res)
             remaining -= res
     finally:
         rffi.free_nonmovingbuffer(data, dataptr)
示例#9
0
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)
示例#10
0
def PyObject_Print(space, w_obj, fp, flags):
    """Print an object o, on file fp.  Returns -1 on error.  The flags argument
    is used to enable certain printing options.  The only option currently
    supported is Py_PRINT_RAW; if given, the str() of the object is written
    instead of the repr()."""
    if rffi.cast(lltype.Signed, flags) & Py_PRINT_RAW:
        w_str = space.str(w_obj)
    else:
        w_str = space.repr(w_obj)

    count = space.len_w(w_str)
    data = space.str_w(w_str)
    buf = rffi.get_nonmovingbuffer(data)
    try:
        fwrite(buf, 1, count, fp)
    finally:
        rffi.free_nonmovingbuffer(data, buf)
    return 0
示例#11
0
文件: object.py 项目: purepython/pypy
def PyObject_Print(space, w_obj, fp, flags):
    """Print an object o, on file fp.  Returns -1 on error.  The flags argument
    is used to enable certain printing options.  The only option currently
    supported is Py_PRINT_RAW; if given, the str() of the object is written
    instead of the repr()."""
    if rffi.cast(lltype.Signed, flags) & Py_PRINT_RAW:
        w_str = space.str(w_obj)
    else:
        w_str = space.repr(w_obj)

    count = space.len_w(w_str)
    data = space.str_w(w_str)
    buf = rffi.get_nonmovingbuffer(data)
    try:
        fwrite(buf, 1, count, fp)
    finally:
        rffi.free_nonmovingbuffer(data, buf)
    return 0
示例#12
0
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)
示例#13
0
def multibytecodec_encerror(encodebuf, e, errors,
                            errorcb, namecb, unicodedata):
    if e > 0:
        reason = "illegal multibyte sequence"
        esize = e
    elif e == MBERR_TOOFEW:
        reason = "incomplete multibyte sequence"
        esize = pypy_cjk_enc_inbuf_remaining(encodebuf)
    elif e == MBERR_NOMEMORY:
        raise MemoryError
    else:
        raise RuntimeError
    #
    # compute the string to use as a replacement -> 'replace', and
    # the current position in the input 'unicodedata' -> 'end'
    start = pypy_cjk_enc_inbuf_consumed(encodebuf)
    end = start + esize
    if errors == "strict":
        raise EncodeDecodeError(start, end, reason)
    elif errors == "ignore":
        replace = ""
    elif errors == "replace":
        codec = pypy_cjk_enc_getcodec(encodebuf)
        try:
            replace = encode(codec, u"?")
        except EncodeDecodeError:
            replace = "?"
    else:
        assert errorcb
        replace, end = errorcb(errors, namecb, reason,
                               unicodedata, start, end)
    inbuf = rffi.get_nonmovingbuffer(replace)
    try:
        r = pypy_cjk_enc_replace_on_error(encodebuf, inbuf, len(replace), end)
    finally:
        rffi.free_nonmovingbuffer(replace, inbuf)
    if r == MBERR_NOMEMORY:
        raise MemoryError
示例#14
0
def multibytecodec_encerror(encodebuf, e, errors,
                            errorcb, namecb, unicodedata):
    if e > 0:
        reason = "illegal multibyte sequence"
        esize = e
    elif e == MBERR_TOOFEW:
        reason = "incomplete multibyte sequence"
        esize = pypy_cjk_enc_inbuf_remaining(encodebuf)
    elif e == MBERR_NOMEMORY:
        raise MemoryError
    else:
        raise RuntimeError
    #
    # compute the string to use as a replacement -> 'replace', and
    # the current position in the input 'unicodedata' -> 'end'
    start = pypy_cjk_enc_inbuf_consumed(encodebuf)
    end = start + esize
    if errors == "strict":
        raise EncodeDecodeError(start, end, reason)
    elif errors == "ignore":
        replace = ""
    elif errors == "replace":
        codec = pypy_cjk_enc_getcodec(encodebuf)
        try:
            replace = encode(codec, u"?")
        except EncodeDecodeError:
            replace = "?"
    else:
        assert errorcb
        replace, end = errorcb(errors, namecb, reason,
                               unicodedata, start, end)
    inbuf = rffi.get_nonmovingbuffer(replace)
    try:
        r = pypy_cjk_enc_replace_on_error(encodebuf, inbuf, len(replace), end)
    finally:
        rffi.free_nonmovingbuffer(replace, inbuf)
    if r == MBERR_NOMEMORY:
        raise MemoryError
示例#15
0
文件: c_codecs.py 项目: ieure/pypy
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)
示例#16
0
def _Pattern_match_search(vm, anchored):
    mod = vm.get_funcs_mod()
    (self, s_o, sp_o),_ = vm.decode_args(mand="!S", opt="I", self_of=Pattern)
    assert isinstance(self, Pattern)
    assert isinstance(s_o, Con_String)
    
    ovect_size = (1 + self.num_caps) * 3
    ovect = lltype.malloc(rffi.INTP.TO, ovect_size, flavor="raw")
    if anchored:
        flags = PCRE_ANCHORED
    else:
        flags = 0
    sp = translate_idx_obj(vm, sp_o, len(s_o.v))
    rs = rffi.get_nonmovingbuffer(s_o.v)
    r = int(pcre_exec(self.cp, None, rs, len(s_o.v), sp, flags, ovect, ovect_size))
    rffi.free_nonmovingbuffer(s_o.v, rs)
    if r < 0:
        if r == PCRE_ERROR_NOMATCH:
            lltype.free(ovect, flavor="raw")
            return vm.get_builtin(BUILTIN_FAIL_OBJ)
        else:
            raise Exception("XXX")
            
    return Match(vm, mod.get_defn(vm, "Match"), ovect, self.num_caps, s_o)
示例#17
0
 def inet_ntop(family, packed):
     "packed string -> human-readable string"
     if family == AF_INET:
         srcsize = sizeof(_c.in_addr)
         dstsize = _c.INET_ADDRSTRLEN
     elif AF_INET6 is not None and family == AF_INET6:
         srcsize = sizeof(_c.in6_addr)
         dstsize = _c.INET6_ADDRSTRLEN
     else:
         raise RSocketError("unknown address family")
     if len(packed) != srcsize:
         raise ValueError("packed IP wrong length for inet_ntop")
     srcbuf = rffi.get_nonmovingbuffer(packed)
     try:
         dstbuf = mallocbuf(dstsize)
         try:
             res = _c.inet_ntop(family, srcbuf, dstbuf, dstsize)
             if not res:
                 raise last_error()
             return rffi.charp2str(res)
         finally:
             lltype.free(dstbuf, flavor='raw')
     finally:
         rffi.free_nonmovingbuffer(packed, srcbuf)
示例#18
0
文件: rsocket.py 项目: alkorzt/pypy
 def inet_ntop(family, packed):
     "packed string -> human-readable string"
     if family == AF_INET:
         srcsize = sizeof(_c.in_addr)
         dstsize = _c.INET_ADDRSTRLEN
     elif AF_INET6 is not None and family == AF_INET6:
         srcsize = sizeof(_c.in6_addr)
         dstsize = _c.INET6_ADDRSTRLEN
     else:
         raise RSocketError("unknown address family")
     if len(packed) != srcsize:
         raise ValueError("packed IP wrong length for inet_ntop")
     srcbuf = rffi.get_nonmovingbuffer(packed)
     try:
         dstbuf = mallocbuf(dstsize)
         try:
             res = _c.inet_ntop(family, srcbuf, dstbuf, dstsize)
             if not res:
                 raise last_error()
             return rffi.charp2str(res)
         finally:
             lltype.free(dstbuf, flavor='raw')
     finally:
         rffi.free_nonmovingbuffer(packed, srcbuf)