Пример #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)
Пример #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)
Пример #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
Пример #4
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)
Пример #5
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)
Пример #6
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
Пример #7
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
Пример #8
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)
Пример #9
0
def strtod(input):
    if len(input) > _INT_LIMIT:
        raise MemoryError
    end_ptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor="raw")
    try:
        # note: don't use the class scoped_view_charp here, it
        # break some tests because this function is used by the GC
        ll_input, flag = rffi.get_nonmovingbuffer_final_null(input)
        try:
            result = dg_strtod(ll_input, end_ptr)

            endpos = rffi.cast(lltype.Signed, end_ptr[0]) - rffi.cast(lltype.Signed, ll_input)
        finally:
            rffi.free_nonmovingbuffer(input, ll_input, flag)
    finally:
        lltype.free(end_ptr, flavor="raw")

    if endpos == 0 or endpos < len(input):
        raise ValueError("invalid input at position %d" % (endpos,))

    return result
Пример #10
0
    def _call(self, funcaddr, args_w):
        space = self.space
        cif_descr = self.cif_descr   # 'self' should have been promoted here
        size = cif_descr.exchange_size
        mustfree_max_plus_1 = 0
        buffer = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw')
        try:
            keepalives = [None] * len(args_w)    # None or strings
            for i in range(len(args_w)):
                data = rffi.ptradd(buffer, cif_descr.exchange_args[i])
                w_obj = args_w[i]
                argtype = self.fargs[i]
                if argtype.convert_argument_from_object(data, w_obj,
                                                        keepalives, i):
                    # argtype is a pointer type, and w_obj a list/tuple/str
                    mustfree_max_plus_1 = i + 1

            jit_libffi.jit_ffi_call(cif_descr,
                                    rffi.cast(rffi.VOIDP, funcaddr),
                                    buffer)

            resultdata = rffi.ptradd(buffer, cif_descr.exchange_result)
            w_res = self.ctitem.copy_and_convert_to_object(resultdata)
        finally:
            for i in range(mustfree_max_plus_1):
                argtype = self.fargs[i]
                if isinstance(argtype, W_CTypePointer):
                    data = rffi.ptradd(buffer, cif_descr.exchange_args[i])
                    flag = get_mustfree_flag(data)
                    raw_cdata = rffi.cast(rffi.CCHARPP, data)[0]
                    if flag == 1:
                        lltype.free(raw_cdata, flavor='raw')
                    elif flag >= 4:
                        value = keepalives[i]
                        assert value is not None
                        rffi.free_nonmovingbuffer(value, raw_cdata, chr(flag))
            lltype.free(buffer, flavor='raw')
            keepalive_until_here(args_w)
        return w_res
Пример #11
0
def strtod(input):
    if len(input) > _INT_LIMIT:
        raise MemoryError
    end_ptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
    try:
        # note: don't use the class scoped_view_charp here, it
        # break some tests because this function is used by the GC
        ll_input, flag = rffi.get_nonmovingbuffer_final_null(input)
        try:
            result = dg_strtod(ll_input, end_ptr)

            endpos = (rffi.cast(lltype.Signed, end_ptr[0]) -
                      rffi.cast(lltype.Signed, ll_input))
        finally:
            rffi.free_nonmovingbuffer(input, ll_input, flag)
    finally:
        lltype.free(end_ptr, flavor='raw')

    if endpos == 0 or endpos < len(input):
        raise ValueError("invalid input at position %d" % (endpos, ))

    return result
Пример #12
0
    def _call(self, funcaddr, args_w):
        space = self.space
        cif_descr = self.cif_descr   # 'self' should have been promoted here
        size = cif_descr.exchange_size
        mustfree_max_plus_1 = 0
        buffer = lltype.malloc(rffi.CCHARP.TO, size, flavor='raw')
        try:
            keepalives = [None] * len(args_w)    # None or strings
            for i in range(len(args_w)):
                data = rffi.ptradd(buffer, cif_descr.exchange_args[i])
                w_obj = args_w[i]
                argtype = self.fargs[i]
                if argtype.convert_argument_from_object(data, w_obj,
                                                        keepalives, i):
                    # argtype is a pointer type, and w_obj a list/tuple/str
                    mustfree_max_plus_1 = i + 1

            jit_libffi.jit_ffi_call(cif_descr,
                                    rffi.cast(rffi.VOIDP, funcaddr),
                                    buffer)

            resultdata = rffi.ptradd(buffer, cif_descr.exchange_result)
            w_res = self.ctitem.copy_and_convert_to_object(resultdata)
        finally:
            for i in range(mustfree_max_plus_1):
                argtype = self.fargs[i]
                if isinstance(argtype, W_CTypePointer):
                    data = rffi.ptradd(buffer, cif_descr.exchange_args[i])
                    flag = get_mustfree_flag(data)
                    raw_cdata = rffi.cast(rffi.CCHARPP, data)[0]
                    if flag == 1:
                        lltype.free(raw_cdata, flavor='raw')
                    elif flag >= 4:
                        value = keepalives[i]
                        assert value is not None
                        rffi.free_nonmovingbuffer(value, raw_cdata, chr(flag))
            lltype.free(buffer, flavor='raw')
            keepalive_until_here(args_w)
        return w_res
Пример #13
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)
Пример #14
0
 def __exit__(self, *args):
     # Deallocate. Can't forget to do this, or else we could fill the
     # GC with pinned crap.
     for datum, charp, pinned, copied in self.metabufs:
         rffi.free_nonmovingbuffer(datum, charp, pinned, copied)
     self.scoping.__exit__(*args)
Пример #15
0
Файл: ruv.py Проект: dckc/typhon
 def __exit__(self, *args):
     # Deallocate. Can't forget to do this, or else we could fill the
     # GC with pinned crap.
     for datum, charp, flag in self.metabufs:
         rffi.free_nonmovingbuffer(datum, charp, flag)
     self.scoping.__exit__(*args)
Пример #16
0
 def deallocate(self):
     # Deallocate. Can't forget to do this, or else we could fill the
     # GC with pinned crap.
     for datum, charp, flag in self.metabufs:
         rffi.free_nonmovingbuffer(datum, charp, flag)
     self.scoping.__exit__()