示例#1
0
    def recvfrom_into_w(self, space, w_buffer, nbytes=0, flags=0):
        """recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)

        Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.
        """
        rwbuffer = space.writebuf_w(w_buffer)
        lgt = rwbuffer.getlength()
        if nbytes == 0:
            nbytes = lgt
        elif nbytes > lgt:
            raise oefmt(space.w_ValueError,
                        "nbytes is greater than the length of the buffer")
        try:
            rwbuffer.get_raw_address()
        except ValueError:
            rawbuf = RawByteBuffer(nbytes)
        else:
            rawbuf = rwbuffer
        while True:
            try:
                readlgt, addr = self.sock.recvfrom_into(rawbuf, nbytes, flags)
                break
            except SocketError as e:
                converted_error(space, e, eintr_retry=True)
        if rawbuf is not rwbuffer:
            rwbuffer.setslice(0, rawbuf.getslice(0, 1, readlgt))
        if addr:
            try:
                w_addr = addr_as_object(addr, self.sock.fd, space)
            except SocketError as e:
                raise converted_error(space, e)
        else:
            w_addr = space.w_None
        return space.newtuple([space.newint(readlgt), w_addr])
示例#2
0
    def recv_into_w(self, space, w_buffer, nbytes=0, flags=0):
        """recv_into(buffer, [nbytes[, flags]]) -> nbytes_read

        A version of recv() that stores its data into a buffer rather than creating
        a new string.  Receive up to buffersize bytes from the socket.  If buffersize
        is not specified (or 0), receive up to the size available in the given buffer.

        See recv() for documentation about the flags.
        """
        rwbuffer = space.writebuf_w(w_buffer)
        lgt = rwbuffer.getlength()
        if nbytes < 0:
            raise oefmt(space.w_ValueError, "negative buffersize in recv_into")
        if nbytes == 0:
            nbytes = lgt
        if lgt < nbytes:
            raise oefmt(space.w_ValueError,
                        "buffer too small for requested bytes")
        try:
            rwbuffer.get_raw_address()
        except ValueError:
            rawbuf = RawByteBuffer(nbytes)
        else:
            rawbuf = rwbuffer

        while True:
            try:
                nbytes_read = self.sock.recvinto(rawbuf, nbytes, flags)
                break
            except SocketError as e:
                converted_error(space, e, eintr_retry=True)
        if rawbuf is not rwbuffer:
            rwbuffer.setslice(0, rawbuf.getslice(0, 1, nbytes_read))
        return space.newint(nbytes_read)