Example #1
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, rffi.cast(rffi.VOIDP, 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)
Example #2
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)
Example #3
0
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
Example #4
0
 def __enter__(self):
     bufs = self.scoping.__enter__()
     self.metabufs = []
     for i, datum in enumerate(self.data):
         # get_nonmovingbuffer tries its hardest to avoid copies. Don't
         # forget that we have to deallocate each one later.
         assert datum is not None
         charp, pinned, copied = rffi.get_nonmovingbuffer(datum)
         bufs[i].c_base = charp
         rffi.setintfield(bufs[i], "c_len", len(datum))
         # Store the original strs to keep them alive and make iteration
         # easier later.
         self.metabufs.append((datum, charp, pinned, copied))
     return bufs
Example #5
0
File: ruv.py Project: dckc/typhon
 def __enter__(self):
     bufs = self.scoping.__enter__()
     self.metabufs = []
     for i, datum in enumerate(self.data):
         # get_nonmovingbuffer tries its hardest to avoid copies. Don't
         # forget that we have to deallocate each one later.
         assert datum is not None
         charp, flag = rffi.get_nonmovingbuffer(datum)
         bufs[i].c_base = charp
         rffi.setintfield(bufs[i], "c_len", len(datum))
         # Store the original strs to keep them alive and make iteration
         # easier later.
         self.metabufs.append((datum, charp, flag))
     return bufs
Example #6
0
 def write(self, value):
     assert value is not None
     ll_file = self.ll_file
     if not ll_file:
         raise ValueError("I/O operation on closed file")
     assert value is not None
     ll_value = rffi.get_nonmovingbuffer(value)
     try:
         # note that since we got a nonmoving buffer, it is either raw
         # or already cannot move, so the arithmetics below are fine
         length = len(value)
         bytes = c_fwrite(ll_value, 1, length, ll_file)
         if bytes != length:
             errno = rposix.get_errno()
             raise OSError(errno, os.strerror(errno))
     finally:
         rffi.free_nonmovingbuffer(value, ll_value)
Example #7
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)
Example #8
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
        retu, rets, end = errorcb(errors, namecb, reason,
                                  unicodedata, start, end)
        if rets is not None:
            # py3k only
            replace = rets
        else:
            assert retu is not None
            codec = pypy_cjk_enc_getcodec(encodebuf)
            replace = encode(codec, retu, "strict", errorcb, namecb)
    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
Example #9
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
Example #10
0
 def sendall(self, data, flags=0, signal_checker=None):
     """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:
             try:
                 res = self.send_raw(p, remaining, flags)
                 p = rffi.ptradd(p, res)
                 remaining -= res
             except CSocketError, e:
                 if e.errno != _c.EINTR:
                     raise
             if signal_checker:
                 signal_checker.check()
     finally:
         rffi.free_nonmovingbuffer(data, dataptr)
Example #11
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, flag = 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, flag)
    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)