Beispiel #1
0
def buffer_attach(space, py_obj, w_obj):
    """
    Fills a newly allocated PyBufferObject with the given (str) buffer object.
    """
    py_buf = rffi.cast(PyBufferObject, py_obj)
    py_buf.c_b_offset = 0
    rffi.setintfield(py_buf, 'c_b_readonly', 1)
    rffi.setintfield(py_buf, 'c_b_hash', -1)

    if isinstance(w_obj, SubBuffer):
        py_buf.c_b_offset = w_obj.offset
        w_obj = w_obj.buffer

    # If w_obj already allocated a fixed buffer, use it, and keep a
    # reference to w_obj.
    # Otherwise, b_base stays NULL, and we own the b_ptr.

    if isinstance(w_obj, StringBuffer):
        py_buf.c_b_base = lltype.nullptr(PyObject.TO)
        py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, rffi.str2charp(w_obj.value))
        py_buf.c_b_size = w_obj.getlength()
    elif isinstance(w_obj, ArrayBuffer):
        w_base = w_obj.array
        py_buf.c_b_base = make_ref(space, w_base)
        py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, w_obj.array._charbuf_start())
        py_buf.c_b_size = w_obj.getlength()
    else:
        raise OperationError(space.w_NotImplementedError, space.wrap(
            "buffer flavor not supported"))
Beispiel #2
0
    def poll(fddict, timeout=-1):
        """'fddict' maps file descriptors to interesting events.
        'timeout' is an integer in milliseconds, and NOT a float
        number of seconds, but it's the same in CPython.  Use -1 for infinite.
        Returns a list [(fd, events)].
        """
        numfd = len(fddict)
        pollfds = lltype.malloc(_c.pollfdarray, numfd, flavor='raw')
        try:
            i = 0
            for fd, events in fddict.iteritems():
                rffi.setintfield(pollfds[i], 'c_fd', fd)
                rffi.setintfield(pollfds[i], 'c_events', events)
                i += 1
            assert i == numfd

            ret = _c.poll(pollfds, numfd, timeout)

            if ret < 0:
                raise PollError(_c.geterrno())

            retval = []
            for i in range(numfd):
                pollfd = pollfds[i]
                fd = rffi.cast(lltype.Signed, pollfd.c_fd)
                revents = rffi.cast(lltype.Signed, pollfd.c_revents)
                if revents:
                    retval.append((fd, revents))
        finally:
            lltype.free(pollfds, flavor='raw')
        return retval
Beispiel #3
0
 def _select(self, for_writing):
     """Returns 0 when reading/writing is possible,
     1 when timing out and -1 on error."""
     timeout = self.timeout
     if timeout <= 0.0 or self.fd == _c.INVALID_SOCKET:
         # blocking I/O or no socket.
         return 0
     tv = rffi.make(_c.timeval)
     rffi.setintfield(tv, 'c_tv_sec', int(timeout))
     rffi.setintfield(tv, 'c_tv_usec',
                      int((timeout - int(timeout)) * 1000000))
     fds = lltype.malloc(_c.fd_set.TO, flavor='raw')
     _c.FD_ZERO(fds)
     _c.FD_SET(self.fd, fds)
     null = lltype.nullptr(_c.fd_set.TO)
     if for_writing:
         n = _c.select(self.fd + 1, null, fds, null, tv)
     else:
         n = _c.select(self.fd + 1, fds, null, null, tv)
     lltype.free(fds, flavor='raw')
     lltype.free(tv, flavor='raw')
     if n < 0:
         return -1
     if n == 0:
         return 1
     return 0
Beispiel #4
0
 def fill_from_object(self, space, w_address):
     # XXX a bit of code duplication
     _, w_port = space.unpackiterable(w_address, 2)
     port = space.int_w(w_port)
     a = self.lock(_c.sockaddr_in)
     rffi.setintfield(a, 'c_sin_port', htons(port))
     self.unlock()
Beispiel #5
0
def buffer_attach(space, py_obj, w_obj):
    """
    Fills a newly allocated PyBufferObject with the given (str) buffer object.
    """
    py_buf = rffi.cast(PyBufferObject, py_obj)
    py_buf.c_b_offset = 0
    rffi.setintfield(py_buf, 'c_b_readonly', 1)
    rffi.setintfield(py_buf, 'c_b_hash', -1)

    if isinstance(w_obj, SubBuffer):
        py_buf.c_b_offset = w_obj.offset
        w_obj = w_obj.buffer

    # If w_obj already allocated a fixed buffer, use it, and keep a
    # reference to w_obj.
    # Otherwise, b_base stays NULL, and we own the b_ptr.

    if isinstance(w_obj, StringBuffer):
        py_buf.c_b_base = lltype.nullptr(PyObject.TO)
        py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, rffi.str2charp(w_obj.value))
        py_buf.c_b_size = w_obj.getlength()
    elif isinstance(w_obj, ArrayBuffer):
        w_base = w_obj.array
        py_buf.c_b_base = make_ref(space, w_base)
        py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, w_obj.array._charbuf_start())
        py_buf.c_b_size = w_obj.getlength()
    else:
        raise OperationError(space.w_NotImplementedError,
                             space.wrap("buffer flavor not supported"))
Beispiel #6
0
def decompress(stream, data, flush=Z_SYNC_FLUSH, max_length=sys.maxint):
    """
    Feed more data into an inflate stream.  Returns a tuple (string,
    finished, unused_data_length).  The string contains (a part of) the
    decompressed data.  If flush != Z_NO_FLUSH, this also flushes the
    output data; see zlib.h or the documentation of the zlib module for
    the possible values of 'flush'.

    The 'string' is never longer than 'max_length'.  The
    'unused_data_length' is the number of unprocessed input characters,
    either because they are after the end of the compressed stream or
    because processing it would cause the 'max_length' to be exceeded.
    """
    # Warning, reentrant calls to the zlib with a given stream can cause it
    # to crash.  The caller of pypy.rlib.rzlib should use locks if needed.

    # _operate() does not support the Z_FINISH method of decompressing.
    # We can use Z_SYNC_FLUSH instead and manually check that we got to
    # the end of the data.
    if flush == Z_FINISH:
        flush = Z_SYNC_FLUSH
        should_finish = True
    else:
        should_finish = False
    while_doing = "while decompressing data"
    data, err, avail_in = _operate(stream, data, flush, max_length, _inflate,
                                   while_doing)
    if should_finish:
        # detect incomplete input
        rffi.setintfield(stream, 'c_avail_in', 0)
        err = _inflate(stream, Z_FINISH)
        if err < 0:
            raise RZlibError.fromstream(stream, err, while_doing)
    finished = (err == Z_STREAM_END)
    return data, finished, avail_in
Beispiel #7
0
 def fill_from_object(self, space, w_address):
     # XXX a bit of code duplication
     _, w_port = space.unpackiterable(w_address, 2)
     port = space.int_w(w_port)
     a = self.lock(_c.sockaddr_in)
     rffi.setintfield(a, 'c_sin_port', htons(port))
     self.unlock()
Beispiel #8
0
def frame_attach(space, py_obj, w_obj):
    "Fills a newly allocated PyFrameObject with a frame object"
    frame = space.interp_w(PyFrame, w_obj)
    py_frame = rffi.cast(PyFrameObject, py_obj)
    py_frame.c_f_code = rffi.cast(PyCodeObject, make_ref(space, frame.pycode))
    py_frame.c_f_globals = make_ref(space, frame.w_globals)
    rffi.setintfield(py_frame, 'c_f_lineno', frame.f_lineno)
Beispiel #9
0
def frame_attach(space, py_obj, w_obj):
    "Fills a newly allocated PyFrameObject with a frame object"
    frame = space.interp_w(PyFrame, w_obj)
    py_frame = rffi.cast(PyFrameObject, py_obj)
    py_frame.c_f_code = rffi.cast(PyCodeObject, make_ref(space, frame.pycode))
    py_frame.c_f_globals = make_ref(space, frame.w_globals)
    rffi.setintfield(py_frame, 'c_f_lineno', frame.f_lineno)
Beispiel #10
0
 def _select(self, for_writing):
     """Returns 0 when reading/writing is possible,
     1 when timing out and -1 on error."""
     timeout = self.timeout
     if timeout <= 0.0 or self.fd == _c.INVALID_SOCKET:
         # blocking I/O or no socket.
         return 0
     tv = rffi.make(_c.timeval)
     rffi.setintfield(tv, 'c_tv_sec', int(timeout))
     rffi.setintfield(tv, 'c_tv_usec', int((timeout-int(timeout))
                                           * 1000000))
     fds = lltype.malloc(_c.fd_set.TO, flavor='raw')
     _c.FD_ZERO(fds)
     _c.FD_SET(self.fd, fds)
     null = lltype.nullptr(_c.fd_set.TO)
     if for_writing:
         n = _c.select(self.fd + 1, null, fds, null, tv)
     else:
         n = _c.select(self.fd + 1, fds, null, null, tv)
     lltype.free(fds, flavor='raw')
     lltype.free(tv, flavor='raw')
     if n < 0:
         return -1
     if n == 0:
         return 1
     return 0
Beispiel #11
0
    def _sem_timedwait_save(sem, deadline):
        delay = 0
        void = lltype.nullptr(rffi.VOIDP.TO)
        with lltype.scoped_alloc(TIMEVALP.TO, 1) as tvdeadline:
            while True:
                # poll
                if _sem_trywait(sem) == 0:
                    return 0
                elif rposix.get_errno() != errno.EAGAIN:
                    return -1

                now = gettimeofday()
                c_tv_sec = rffi.getintfield(deadline[0], 'c_tv_sec')
                c_tv_nsec = rffi.getintfield(deadline[0], 'c_tv_nsec')
                if (c_tv_sec < now[0]
                        or (c_tv_sec == now[0] and c_tv_nsec <= now[1])):
                    rposix.set_errno(errno.ETIMEDOUT)
                    return -1

                # calculate how much time is left
                difference = ((c_tv_sec - now[0]) * 1000000 +
                              (c_tv_nsec - now[1]))

                # check delay not too long -- maximum is 20 msecs
                if delay > 20000:
                    delay = 20000
                if delay > difference:
                    delay = difference
                delay += 1000

                # sleep
                rffi.setintfield(tvdeadline[0], 'c_tv_sec', delay / 1000000)
                rffi.setintfield(tvdeadline[0], 'c_tv_usec', delay % 1000000)
                if _select(0, void, void, void, tvdeadline) < 0:
                    return -1
Beispiel #12
0
def get_addr(hostname, socktype, protocol, port, address_to_fill):
    hostent = _c.gethostbyname(hostname)

    if not hostent:
        raise GAIError(EAI_FAIL)

    hname, aliases, address_list = gethost_common("", hostent)
        
    result = []

    for address in address_list:
        if address.family == _c.AF_INET:
            a = address.lock(_c.sockaddr_in)
            rffi.setintfield(a, 'c_sin_port', r_uint(port) & 0xffff)
            address.unlock()
        a = address.lock()
        addr = make_address(a, address.addrlen, address_to_fill)
        address.unlock()
        result.append((address.family,
                       socktype,
                       protocol,
                       "", # XXX canonname?
                       addr))

    return result
Beispiel #13
0
def get_addr(hostname, socktype, protocol, port, address_to_fill):
    hostent = _c.gethostbyname(hostname)

    if not hostent:
        raise GAIError(EAI_FAIL)

    hname, aliases, address_list = gethost_common("", hostent)

    result = []

    for address in address_list:
        if address.family == _c.AF_INET:
            a = address.lock(_c.sockaddr_in)
            rffi.setintfield(a, 'c_sin_port', port & 0xffff)
            address.unlock()
        a = address.lock()
        addr = make_address(a, address.addrlen, address_to_fill)
        address.unlock()
        result.append((
            address.family,
            socktype,
            protocol,
            "",  # XXX canonname?
            addr))

    return result
Beispiel #14
0
def decompress(stream, data, flush=Z_SYNC_FLUSH, max_length=sys.maxint):
    """
    Feed more data into an inflate stream.  Returns a tuple (string,
    finished, unused_data_length).  The string contains (a part of) the
    decompressed data.  If flush != Z_NO_FLUSH, this also flushes the
    output data; see zlib.h or the documentation of the zlib module for
    the possible values of 'flush'.

    The 'string' is never longer than 'max_length'.  The
    'unused_data_length' is the number of unprocessed input characters,
    either because they are after the end of the compressed stream or
    because processing it would cause the 'max_length' to be exceeded.
    """
    # Warning, reentrant calls to the zlib with a given stream can cause it
    # to crash.  The caller of pypy.rlib.rzlib should use locks if needed.

    # _operate() does not support the Z_FINISH method of decompressing.
    # We can use Z_SYNC_FLUSH instead and manually check that we got to
    # the end of the data.
    if flush == Z_FINISH:
        flush = Z_SYNC_FLUSH
        should_finish = True
    else:
        should_finish = False
    while_doing = "while decompressing data"
    data, err, avail_in = _operate(stream, data, flush, max_length, _inflate,
                                   while_doing)
    if should_finish:
        # detect incomplete input
        rffi.setintfield(stream, 'c_avail_in', 0)
        err = _inflate(stream, Z_FINISH)
        if err < 0:
            raise RZlibError.fromstream(stream, err, while_doing)
    finished = (err == Z_STREAM_END)
    return data, finished, avail_in
Beispiel #15
0
def _operate(stream, data, flush, max_length, cfunc, while_doing):
    """Common code for compress() and decompress().
    """
    # Prepare the input buffer for the stream
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(data)) as inbuf:
        for i in xrange(len(data)):
            inbuf[i] = data[i]
        stream.c_next_in = rffi.cast(Bytefp, inbuf)
        rffi.setintfield(stream, 'c_avail_in', len(data))

        # Prepare the output buffer
        with lltype.scoped_alloc(rffi.CCHARP.TO, OUTPUT_BUFFER_SIZE) as outbuf:
            # Strategy: we call deflate() to get as much output data as fits in
            # the buffer, then accumulate all output into a StringBuffer
            # 'result'.
            result = StringBuilder()

            while True:
                stream.c_next_out = rffi.cast(Bytefp, outbuf)
                bufsize = OUTPUT_BUFFER_SIZE
                if max_length < bufsize:
                    if max_length <= 0:
                        err = Z_OK
                        break
                    bufsize = max_length
                max_length -= bufsize
                rffi.setintfield(stream, 'c_avail_out', bufsize)
                err = cfunc(stream, flush)
                if err == Z_OK or err == Z_STREAM_END:
                    # accumulate data into 'result'
                    avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
                    result.append_charpsize(outbuf, bufsize - avail_out)
                    # if the output buffer is full, there might be more data
                    # so we need to try again.  Otherwise, we're done.
                    if avail_out > 0:
                        break
                    # We're also done if we got a Z_STREAM_END (which should
                    # only occur when flush == Z_FINISH).
                    if err == Z_STREAM_END:
                        break
                    else:
                        continue
                elif err == Z_BUF_ERROR:
                    avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
                    # When compressing, we will only get Z_BUF_ERROR if
                    # the output buffer was full but there wasn't more
                    # output when we tried again, so it is not an error
                    # condition.
                    if avail_out == bufsize:
                        break

                # fallback case: report this error
                raise RZlibError.fromstream(stream, err, while_doing)

    # When decompressing, if the compressed stream of data was truncated,
    # then the zlib simply returns Z_OK and waits for more.  If it is
    # complete it returns Z_STREAM_END.
    return (result.build(),
            err,
            rffi.cast(lltype.Signed, stream.c_avail_in))
Beispiel #16
0
    def poll(fddict, timeout=-1):
        """'fddict' maps file descriptors to interesting events.
        'timeout' is an integer in milliseconds, and NOT a float
        number of seconds, but it's the same in CPython.  Use -1 for infinite.
        Returns a list [(fd, events)].
        """
        numfd = len(fddict)
        pollfds = lltype.malloc(_c.pollfdarray, numfd, flavor='raw')
        try:
            i = 0
            for fd, events in fddict.iteritems():
                rffi.setintfield(pollfds[i], 'c_fd', fd)
                rffi.setintfield(pollfds[i], 'c_events', events)
                i += 1
            assert i == numfd

            ret = _c.poll(pollfds, numfd, timeout)

            if ret < 0:
                raise PollError(_c.geterrno())

            retval = []
            for i in range(numfd):
                pollfd = pollfds[i]
                fd      = rffi.cast(lltype.Signed, pollfd.c_fd)
                revents = rffi.cast(lltype.Signed, pollfd.c_revents)
                if revents:
                    retval.append((fd, revents))
        finally:
            lltype.free(pollfds, flavor='raw')
        return retval
 def draw_pixel(self, x, y, color):
     color = self.colors[color]
     start_x = x * self.scale
     start_y = y * self.scale
     dstrect = self.blit_rect
     rffi.setintfield(dstrect, 'c_x',  start_x)
     rffi.setintfield(dstrect, 'c_y',  start_y)
     RSDL.FillRect(self.screen, dstrect, color)
Beispiel #18
0
def _operate(stream, data, flush, max_length, cfunc, while_doing):
    """Common code for compress() and decompress().
    """
    # Prepare the input buffer for the stream
    with lltype.scoped_alloc(rffi.CCHARP.TO, len(data)) as inbuf:
        for i in xrange(len(data)):
            inbuf[i] = data[i]
        stream.c_next_in = rffi.cast(Bytefp, inbuf)
        rffi.setintfield(stream, 'c_avail_in', len(data))

        # Prepare the output buffer
        with lltype.scoped_alloc(rffi.CCHARP.TO, OUTPUT_BUFFER_SIZE) as outbuf:
            # Strategy: we call deflate() to get as much output data as fits in
            # the buffer, then accumulate all output into a StringBuffer
            # 'result'.
            result = StringBuilder()

            while True:
                stream.c_next_out = rffi.cast(Bytefp, outbuf)
                bufsize = OUTPUT_BUFFER_SIZE
                if max_length < bufsize:
                    if max_length <= 0:
                        err = Z_OK
                        break
                    bufsize = max_length
                max_length -= bufsize
                rffi.setintfield(stream, 'c_avail_out', bufsize)
                err = cfunc(stream, flush)
                if err == Z_OK or err == Z_STREAM_END:
                    # accumulate data into 'result'
                    avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
                    result.append_charpsize(outbuf, bufsize - avail_out)
                    # if the output buffer is full, there might be more data
                    # so we need to try again.  Otherwise, we're done.
                    if avail_out > 0:
                        break
                    # We're also done if we got a Z_STREAM_END (which should
                    # only occur when flush == Z_FINISH).
                    if err == Z_STREAM_END:
                        break
                    else:
                        continue
                elif err == Z_BUF_ERROR:
                    avail_out = rffi.cast(lltype.Signed, stream.c_avail_out)
                    # When compressing, we will only get Z_BUF_ERROR if
                    # the output buffer was full but there wasn't more
                    # output when we tried again, so it is not an error
                    # condition.
                    if avail_out == bufsize:
                        break

                # fallback case: report this error
                raise RZlibError.fromstream(stream, err, while_doing)

    # When decompressing, if the compressed stream of data was truncated,
    # then the zlib simply returns Z_OK and waits for more.  If it is
    # complete it returns Z_STREAM_END.
    return (result.build(), err, rffi.cast(lltype.Signed, stream.c_avail_in))
Beispiel #19
0
 def from_in6_addr(in6_addr):
     result = instantiate(INET6Address)
     # store the malloc'ed data into 'result' as soon as possible
     # to avoid leaks if an exception occurs inbetween
     sin = lltype.malloc(_c.sockaddr_in6, flavor='raw', zero=True)
     result.setdata(sin, sizeof(_c.sockaddr_in6))
     rffi.setintfield(sin, 'c_sin6_family', AF_INET)
     rffi.structcopy(sin.c_sin6_addr, in6_addr)
     return result
Beispiel #20
0
 def from_in6_addr(in6_addr):
     result = instantiate(INET6Address)
     # store the malloc'ed data into 'result' as soon as possible
     # to avoid leaks if an exception occurs inbetween
     sin = lltype.malloc(_c.sockaddr_in6, flavor='raw', zero=True)
     result.setdata(sin, sizeof(_c.sockaddr_in6))
     rffi.setintfield(sin, 'c_sin6_family', AF_INET)
     rffi.structcopy(sin.c_sin6_addr, in6_addr)
     return result
Beispiel #21
0
 def fakeimpl(arg):
     st = getattr(os, name)(arg)
     fields = [TYPE for fieldname, TYPE in LL_STAT_FIELDS]
     TP = TUPLE_TYPE(fields)
     ll_tup = lltype.malloc(TP.TO)
     for i, (fieldname, TYPE) in enumerate(LL_STAT_FIELDS):
         val = getattr(st, fieldname)
         rffi.setintfield(ll_tup, 'item%d' % i, int(val))
     return ll_tup
Beispiel #22
0
 def fakeimpl(arg):
     st = getattr(os, name)(arg)
     fields = [TYPE for fieldname, TYPE in LL_STAT_FIELDS]
     TP = TUPLE_TYPE(fields)
     ll_tup = lltype.malloc(TP.TO)
     for i, (fieldname, TYPE) in enumerate(LL_STAT_FIELDS):
         val = getattr(st, fieldname)
         rffi.setintfield(ll_tup, 'item%d' % i, int(val))
     return ll_tup
Beispiel #23
0
def select(inl, outl, excl, timeout=-1.0):
    nfds = 0
    if inl: 
        ll_inl = lltype.malloc(_c.fd_set.TO, flavor='raw')
        _c.FD_ZERO(ll_inl)
        for i in inl:
            _c.FD_SET(i, ll_inl)
            if i > nfds:
                nfds = i
    else:
        ll_inl = lltype.nullptr(_c.fd_set.TO)
    if outl: 
        ll_outl = lltype.malloc(_c.fd_set.TO, flavor='raw')
        _c.FD_ZERO(ll_outl)
        for i in outl:
            _c.FD_SET(i, ll_outl)
            if i > nfds:
                nfds = i
    else:
        ll_outl = lltype.nullptr(_c.fd_set.TO)
    if excl: 
        ll_excl = lltype.malloc(_c.fd_set.TO, flavor='raw')
        _c.FD_ZERO(ll_excl)
        for i in excl:
            _c.FD_SET(i, ll_excl)
            if i > nfds:
                nfds = i
    else:
        ll_excl = lltype.nullptr(_c.fd_set.TO)
    if timeout != -1.0:
        ll_timeval = rffi.make(_c.timeval)
        rffi.setintfield(ll_timeval, 'c_tv_sec', int(timeout))
        rffi.setintfield(ll_timeval, 'c_tv_usec', int((timeout-int(timeout))
                                                  * 1000000))
    else:
        ll_timeval = lltype.nullptr(_c.timeval)
    try:
        res = _c.select(nfds + 1, ll_inl, ll_outl, ll_excl, ll_timeval)
        if res == -1:
            raise SelectError(_c.geterrno())
        if res == 0:
            return ([], [], [])
        else:
            return (
                [i for i in inl if _c.FD_ISSET(i, ll_inl)],
                [i for i in outl if _c.FD_ISSET(i, ll_outl)],
                [i for i in excl if _c.FD_ISSET(i, ll_excl)])
    finally:
        if ll_inl:
            lltype.free(ll_inl, flavor='raw')
        if ll_outl:
            lltype.free(ll_outl, flavor='raw')
        if ll_excl:
            lltype.free(ll_excl, flavor='raw')
        if ll_timeval:
            lltype.free(ll_timeval, flavor='raw')
Beispiel #24
0
def select(inl, outl, excl, timeout=-1.0):
    nfds = 0
    if inl: 
        ll_inl = lltype.malloc(_c.fd_set.TO, flavor='raw')
        _c.FD_ZERO(ll_inl)
        for i in inl:
            _c.FD_SET(i, ll_inl)
            if i > nfds:
                nfds = i
    else:
        ll_inl = lltype.nullptr(_c.fd_set.TO)
    if outl: 
        ll_outl = lltype.malloc(_c.fd_set.TO, flavor='raw')
        _c.FD_ZERO(ll_outl)
        for i in outl:
            _c.FD_SET(i, ll_outl)
            if i > nfds:
                nfds = i
    else:
        ll_outl = lltype.nullptr(_c.fd_set.TO)
    if excl: 
        ll_excl = lltype.malloc(_c.fd_set.TO, flavor='raw')
        _c.FD_ZERO(ll_excl)
        for i in excl:
            _c.FD_SET(i, ll_excl)
            if i > nfds:
                nfds = i
    else:
        ll_excl = lltype.nullptr(_c.fd_set.TO)
    if timeout != -1.0:
        ll_timeval = rffi.make(_c.timeval)
        rffi.setintfield(ll_timeval, 'c_tv_sec', int(timeout))
        rffi.setintfield(ll_timeval, 'c_tv_usec', int((timeout-int(timeout))
                                                  * 1000000))
    else:
        ll_timeval = lltype.nullptr(_c.timeval)
    try:
        res = _c.select(nfds + 1, ll_inl, ll_outl, ll_excl, ll_timeval)
        if res == -1:
            raise SelectError(_c.geterrno())
        if res == 0:
            return ([], [], [])
        else:
            return (
                [i for i in inl if _c.FD_ISSET(i, ll_inl)],
                [i for i in outl if _c.FD_ISSET(i, ll_outl)],
                [i for i in excl if _c.FD_ISSET(i, ll_excl)])
    finally:
        if ll_inl:
            lltype.free(ll_inl, flavor='raw')
        if ll_outl:
            lltype.free(ll_outl, flavor='raw')
        if ll_excl:
            lltype.free(ll_excl, flavor='raw')
        if ll_timeval:
            lltype.free(ll_timeval, flavor='raw')
Beispiel #25
0
    def descr__init__(self, space, w_ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0):
        ident = space.c_filedescriptor_w(w_ident)

        self.event = lltype.malloc(kevent, flavor="raw")
        rffi.setintfield(self.event, "c_ident", ident)
        rffi.setintfield(self.event, "c_filter", filter)
        rffi.setintfield(self.event, "c_flags", flags)
        rffi.setintfield(self.event, "c_fflags", fflags)
        rffi.setintfield(self.event, "c_data", data)
        self.event.c_udata = rffi.cast(rffi.VOIDP, udata)
Beispiel #26
0
def code_attach(space, py_obj, w_obj):
    py_code = rffi.cast(PyCodeObject, py_obj)
    assert isinstance(w_obj, PyCode)
    py_code.c_co_name = make_ref(space, space.wrap(w_obj.co_name))
    co_flags = 0
    for name, value in ALL_CODE_FLAGS:
        if w_obj.co_flags & getattr(pycode, name):
            co_flags |= value
    rffi.setintfield(py_code, 'c_co_flags', co_flags)
    rffi.setintfield(py_code, 'c_co_argcount', w_obj.co_argcount)
Beispiel #27
0
def makeipv4addr(s_addr, result=None):
    if result is None:
        result = instantiate(INETAddress)
    elif result.family != AF_INET:
        raise RSocketError("address family mismatched")
    sin = lltype.malloc(_c.sockaddr_in, flavor='raw', zero=True)
    result.setdata(sin, sizeof(_c.sockaddr_in))
    rffi.setintfield(sin, 'c_sin_family', AF_INET)  # PLAT sin_len
    rffi.setintfield(sin.c_sin_addr, 'c_s_addr', s_addr)
    return result
Beispiel #28
0
def code_attach(space, py_obj, w_obj):
    py_code = rffi.cast(PyCodeObject, py_obj)
    assert isinstance(w_obj, PyCode)
    py_code.c_co_name = make_ref(space, space.wrap(w_obj.co_name))
    co_flags = 0
    for name, value in ALL_CODE_FLAGS:
        if w_obj.co_flags & getattr(pycode, name):
            co_flags |= value
    rffi.setintfield(py_code, 'c_co_flags', co_flags)
    rffi.setintfield(py_code, 'c_co_argcount', w_obj.co_argcount)
Beispiel #29
0
def makeipv4addr(s_addr, result=None):
    if result is None:
        result = instantiate(INETAddress)
    elif result.family != AF_INET:
        raise RSocketError("address family mismatched")
    sin = lltype.malloc(_c.sockaddr_in, flavor='raw', zero=True)
    result.setdata(sin, sizeof(_c.sockaddr_in))
    rffi.setintfield(sin, 'c_sin_family', AF_INET)   # PLAT sin_len
    rffi.setintfield(sin.c_sin_addr, 'c_s_addr', s_addr)
    return result
Beispiel #30
0
    def flush(self):
        if not self.running:
            raise OperationError(
                self.space.w_ValueError,
                self.space.wrap("this object was already flushed"))
        self.running = False

        out_bufsize = SMALLCHUNK
        out_buf = lltype.malloc(rffi.CCHARP.TO,
                                out_bufsize,
                                flavor='raw',
                                zero=True)

        try:

            self.bzs.c_next_out = out_buf
            rffi.setintfield(self.bzs, 'c_avail_out', out_bufsize)

            total_out = _bzs_total_out(self.bzs)

            temp = []
            while True:
                bzerror = BZ2_bzCompress(self.bzs, BZ_FINISH)
                if bzerror == BZ_STREAM_END:
                    break
                elif bzerror != BZ_FINISH_OK:
                    _catch_bz2_error(self.space, bzerror)

                if rffi.getintfield(self.bzs, 'c_avail_out') == 0:
                    data = "".join(
                        [out_buf[i] for i in range(_bzs_total_out(self.bzs))])
                    temp.append(data)

                    out_bufsize = _new_buffer_size(out_bufsize)
                    out_buf = lltype.malloc(rffi.CCHARP.TO,
                                            out_bufsize,
                                            flavor='raw',
                                            zero=True)
                    self.bzs.c_next_out = out_buf
                    rffi.setintfield(self.bzs, 'c_avail_out', out_bufsize)

            if temp:
                return self.space.wrap("".join(temp))

            if rffi.getintfield(self.bzs, 'c_avail_out'):
                size = _bzs_total_out(self.bzs) - total_out
                res = "".join([out_buf[i] for i in range(size)])
                return self.space.wrap(res)

            total_out = _bzs_total_out(self.bzs)
            res = "".join([out_buf[i] for i in range(total_out)])
            return self.space.wrap(res)
        finally:
            lltype.free(out_buf, flavor='raw')
Beispiel #31
0
def test_unknown_addr_as_object():    
    c_addr = lltype.malloc(rsocket._c.sockaddr, flavor='raw')
    c_addr.c_sa_data[0] = 'c'
    rffi.setintfield(c_addr, 'c_sa_family', 15)
    # XXX what size to pass here? for the purpose of this test it has
    #     to be short enough so we have some data, 1 sounds good enough
    #     + sizeof USHORT
    w_obj = rsocket.Address(c_addr, 1 + 2).as_object(-1, space)
    assert space.is_true(space.isinstance(w_obj, space.w_tuple))
    assert space.int_w(space.getitem(w_obj, space.wrap(0))) == 15
    assert space.str_w(space.getitem(w_obj, space.wrap(1))) == 'c'
Beispiel #32
0
    def epoll_ctl(self, space, ctl, w_fd, eventmask, ignore_ebadf=False):
        fd = space.c_filedescriptor_w(w_fd)
        with lltype.scoped_alloc(epoll_event) as ev:
            ev.c_events = rffi.cast(rffi.UINT, eventmask)
            rffi.setintfield(ev.c_data, "c_fd", fd)

            result = epoll_ctl(self.epfd, ctl, fd, ev)
            if ignore_ebadf and get_errno() == errno.EBADF:
                result = 0
            if result < 0:
                raise exception_from_errno(space, space.w_IOError)
Beispiel #33
0
    def epoll_ctl(self, space, ctl, w_fd, eventmask, ignore_ebadf=False):
        fd = space.c_filedescriptor_w(w_fd)
        with lltype.scoped_alloc(epoll_event) as ev:
            ev.c_events = rffi.cast(rffi.UINT, eventmask)
            rffi.setintfield(ev.c_data, 'c_fd', fd)

            result = epoll_ctl(self.epfd, ctl, fd, ev)
            if ignore_ebadf and get_errno() == errno.EBADF:
                result = 0
            if result < 0:
                raise exception_from_errno(space, space.w_IOError)
Beispiel #34
0
def test_unknown_addr_as_object():
    c_addr = lltype.malloc(rsocket._c.sockaddr, flavor='raw')
    c_addr.c_sa_data[0] = 'c'
    rffi.setintfield(c_addr, 'c_sa_family', 15)
    # XXX what size to pass here? for the purpose of this test it has
    #     to be short enough so we have some data, 1 sounds good enough
    #     + sizeof USHORT
    w_obj = rsocket.Address(c_addr, 1 + 2).as_object(-1, space)
    assert space.is_true(space.isinstance(w_obj, space.w_tuple))
    assert space.int_w(space.getitem(w_obj, space.wrap(0))) == 15
    assert space.str_w(space.getitem(w_obj, space.wrap(1))) == 'c'
Beispiel #35
0
def mallocrect(x, y, w, h):
    p = lltype.malloc(RSDL.Rect, flavor='raw')
    rffi.setintfield(p, 'c_x', x)
    rffi.setintfield(p, 'c_y', y)
    rffi.setintfield(p, 'c_w', w)
    rffi.setintfield(p, 'c_h', h)
    return p
Beispiel #36
0
            def time_sleep_llimpl(secs):
                void = lltype.nullptr(rffi.VOIDP.TO)
                t = lltype.malloc(self.TIMEVAL, flavor='raw')
                try:
                    frac = math.fmod(secs, 1.0)
                    rffi.setintfield(t, 'c_tv_sec', int(secs))
                    rffi.setintfield(t, 'c_tv_usec', int(frac*1000000.0))

                    if rffi.cast(rffi.LONG, c_select(0, void, void, void, t)) != 0:
                        errno = rposix.get_errno()
                        if errno != EINTR:
                            raise OSError(rposix.get_errno(), "Select failed")
                finally:
                    lltype.free(t, flavor='raw')
Beispiel #37
0
def mallocrect(x, y, w, h):
    p = lltype.malloc(RSDL.Rect, flavor='raw')
    rffi.setintfield(p, 'c_x', x)
    rffi.setintfield(p, 'c_y', y)
    rffi.setintfield(p, 'c_w', w)
    rffi.setintfield(p, 'c_h', h)
    return p
 def posix_fakeimpl(arg):
     st = getattr(os, name)(arg)
     fields = [TYPE for fieldname, TYPE in STAT_FIELDS]
     TP = TUPLE_TYPE(fields)
     ll_tup = lltype.malloc(TP.TO)
     for i, (fieldname, TYPE) in enumerate(STAT_FIELDS):
         val = getattr(st, fieldname)
         if isinstance(TYPE, lltype.Number):
             rffi.setintfield(ll_tup, 'item%d' % i, int(val))
         elif TYPE is lltype.Float:
             setattr(ll_tup, 'item%d' % i, float(val))
         else:
             setattr(ll_tup, 'item%d' % i, val)
     return ll_tup
Beispiel #39
0
def mktime(space, w_tup):
    """mktime(tuple) -> floating point number

    Convert a time tuple in local time to seconds since the Epoch."""

    buf = _gettmarg(space, w_tup, allowNone=False)
    rffi.setintfield(buf, "c_tm_wday", -1)
    tt = c_mktime(buf)
    # A return value of -1 does not necessarily mean an error, but tm_wday
    # cannot remain set to -1 if mktime succeeds.
    if tt == -1 and rffi.getintfield(buf, "c_tm_wday") == -1:
        raise OperationError(space.w_OverflowError, space.wrap("mktime argument out of range"))

    return space.wrap(float(tt))
Beispiel #40
0
    def test_blit_rect(self):
        surface = RSDL.CreateRGBSurface(0, 150, 50, 32,
                                        r_uint(0x000000FF),
                                        r_uint(0x0000FF00),
                                        r_uint(0x00FF0000),
                                        r_uint(0xFF000000))
        fmt = surface.c_format
        color = RSDL.MapRGB(fmt, 255, 0, 0)
        RSDL.FillRect(surface, lltype.nullptr(RSDL.Rect), color)
        
        paintrect = RSDL_helper.mallocrect(75, 0, 150, 50)
        dstrect = lltype.malloc(RSDL.Rect, flavor='raw')
        try:
            color = RSDL.MapRGB(fmt, 255, 128, 0)
            RSDL.FillRect(surface, paintrect, color)

            rffi.setintfield(dstrect, 'c_x',  10)
            rffi.setintfield(dstrect, 'c_y',  10)
            rffi.setintfield(dstrect, 'c_w', 150)
            rffi.setintfield(dstrect, 'c_h',  50)
            RSDL.BlitSurface(surface, lltype.nullptr(RSDL.Rect), self.screen, dstrect)
            RSDL.Flip(self.screen)
        finally:
            lltype.free(dstrect, flavor='raw')
            lltype.free(paintrect, flavor='raw')
        RSDL.FreeSurface(surface)
        self.check("Half Red/Orange rectangle(150px * 50px) at the top left, 10 pixels from the border")
Beispiel #41
0
    def flush(self):
        if not self.running:
            raise OperationError(self.space.w_ValueError,
                self.space.wrap("this object was already flushed"))
        self.running = False
        
        out_bufsize = SMALLCHUNK
        out_buf = lltype.malloc(rffi.CCHARP.TO, out_bufsize, flavor='raw',
                                zero=True)

        try:
    
            self.bzs.c_next_out = out_buf
            rffi.setintfield(self.bzs, 'c_avail_out', out_bufsize)
        
            total_out = _bzs_total_out(self.bzs)
            
            temp = []
            while True:
                bzerror = BZ2_bzCompress(self.bzs, BZ_FINISH)
                if bzerror == BZ_STREAM_END:
                    break
                elif bzerror != BZ_FINISH_OK:
                    _catch_bz2_error(self.space, bzerror)
                
                if rffi.getintfield(self.bzs, 'c_avail_out') == 0:
                    data = "".join([out_buf[i] for i in range(_bzs_total_out(self.bzs))])
                    temp.append(data)
                    
                    out_bufsize = _new_buffer_size(out_bufsize)
                    out_buf = lltype.malloc(rffi.CCHARP.TO, out_bufsize,
                                            flavor='raw', zero=True)
                    self.bzs.c_next_out = out_buf
                    rffi.setintfield(self.bzs, 'c_avail_out', out_bufsize)
        

            if rffi.getintfield(self.bzs, 'c_avail_out'):
                size = _bzs_total_out(self.bzs) - total_out
                res = "".join([out_buf[i] for i in range(size)])
            else:
                total_out = _bzs_total_out(self.bzs)
                res = "".join([out_buf[i] for i in range(total_out)])
            if not temp:
                return self.space.wrap(res)
            else:
                temp.append(res)
                return self.space.wrap("".join(temp))
        finally:
            lltype.free(out_buf, flavor='raw')
Beispiel #42
0
def mktime(space, w_tup):
    """mktime(tuple) -> floating point number

    Convert a time tuple in local time to seconds since the Epoch."""

    buf = _gettmarg(space, w_tup, allowNone=False)
    rffi.setintfield(buf, "c_tm_wday", -1)
    tt = c_mktime(buf)
    # A return value of -1 does not necessarily mean an error, but tm_wday
    # cannot remain set to -1 if mktime succeeds.
    if tt == -1 and rffi.getintfield(buf, "c_tm_wday") == -1:
        raise OperationError(space.w_OverflowError,
                             space.wrap("mktime argument out of range"))

    return space.wrap(float(tt))
Beispiel #43
0
            def time_sleep_llimpl(secs):
                void = lltype.nullptr(rffi.VOIDP.TO)
                t = lltype.malloc(self.TIMEVAL, flavor='raw')
                try:
                    frac = math.fmod(secs, 1.0)
                    rffi.setintfield(t, 'c_tv_sec', int(secs))
                    rffi.setintfield(t, 'c_tv_usec', int(frac * 1000000.0))

                    if rffi.cast(rffi.LONG, c_select(0, void, void, void,
                                                     t)) != 0:
                        errno = rposix.get_errno()
                        if errno != EINTR:
                            raise OSError(rposix.get_errno(), "Select failed")
                finally:
                    lltype.free(t, flavor='raw')
Beispiel #44
0
 def GetVersionEx():
     info = lltype.malloc(OSVERSIONINFO, flavor='raw')
     rffi.setintfield(info, 'c_dwOSVersionInfoSize',
                      rffi.sizeof(OSVERSIONINFO))
     try:
         if not _GetVersionEx(info):
             raise lastWindowsError()
         return (rffi.cast(lltype.Signed, info.c_dwMajorVersion),
                 rffi.cast(lltype.Signed, info.c_dwMinorVersion),
                 rffi.cast(lltype.Signed, info.c_dwBuildNumber),
                 rffi.cast(lltype.Signed, info.c_dwPlatformId),
                 rffi.charp2str(rffi.cast(rffi.CCHARP,
                                          info.c_szCSDVersion)))
     finally:
         lltype.free(info, flavor='raw')
Beispiel #45
0
def get_new_method_def(space):
    state = space.fromcache(State)
    if state.new_method_def:
        return state.new_method_def
    from pypy.module.cpyext.modsupport import PyMethodDef
    ptr = lltype.malloc(PyMethodDef, flavor="raw", zero=True,
                        immortal=True)
    ptr.c_ml_name = rffi.str2charp("__new__")
    lltype.render_immortal(ptr.c_ml_name)
    rffi.setintfield(ptr, 'c_ml_flags', METH_VARARGS | METH_KEYWORDS)
    ptr.c_ml_doc = rffi.str2charp(
        "T.__new__(S, ...) -> a new object with type S, a subtype of T")
    lltype.render_immortal(ptr.c_ml_doc)
    state.new_method_def = ptr
    return ptr
Beispiel #46
0
 def __init__(self, path):
     sun = lltype.malloc(_c.sockaddr_un, flavor='raw', zero=True)
     baseofs = offsetof(_c.sockaddr_un, 'c_sun_path')
     self.setdata(sun, baseofs + len(path))
     rffi.setintfield(sun, 'c_sun_family', AF_UNIX)
     if _c.linux and path.startswith('\x00'):
         # Linux abstract namespace extension
         if len(path) > sizeof(_c.sockaddr_un.c_sun_path):
             raise RSocketError("AF_UNIX path too long")
     else:
         # regular NULL-terminated string
         if len(path) >= sizeof(_c.sockaddr_un.c_sun_path):
             raise RSocketError("AF_UNIX path too long")
         sun.c_sun_path[len(path)] = '\x00'
     for i in range(len(path)):
         sun.c_sun_path[i] = path[i]
Beispiel #47
0
 def __init__(self, path):
     sun = lltype.malloc(_c.sockaddr_un, flavor='raw', zero=True)
     baseofs = offsetof(_c.sockaddr_un, 'c_sun_path')
     self.setdata(sun, baseofs + len(path))
     rffi.setintfield(sun, 'c_sun_family', AF_UNIX)
     if _c.linux and path.startswith('\x00'):
         # Linux abstract namespace extension
         if len(path) > sizeof(_c.sockaddr_un.c_sun_path):
             raise RSocketError("AF_UNIX path too long")
     else:
         # regular NULL-terminated string
         if len(path) >= sizeof(_c.sockaddr_un.c_sun_path):
             raise RSocketError("AF_UNIX path too long")
         sun.c_sun_path[len(path)] = '\x00'
     for i in range(len(path)):
         sun.c_sun_path[i] = path[i]
Beispiel #48
0
 def posix_fakeimpl(arg):
     if s_arg == traits.str0:
         arg = hlstr(arg)
     st = getattr(os, name)(arg)
     fields = [TYPE for fieldname, TYPE in STAT_FIELDS]
     TP = TUPLE_TYPE(fields)
     ll_tup = lltype.malloc(TP.TO)
     for i, (fieldname, TYPE) in enumerate(STAT_FIELDS):
         val = getattr(st, fieldname)
         if isinstance(TYPE, lltype.Number):
             rffi.setintfield(ll_tup, 'item%d' % i, int(val))
         elif TYPE is lltype.Float:
             setattr(ll_tup, 'item%d' % i, float(val))
         else:
             setattr(ll_tup, 'item%d' % i, val)
     return ll_tup
Beispiel #49
0
    def decompress(self, data):
        """decompress(data) -> string

        Provide more data to the decompressor object. It will return chunks
        of decompressed data whenever possible. If you try to decompress data
        after the end of stream is found, EOFError will be raised. If any data
        was found after the end of stream, it'll be ignored and saved in
        unused_data attribute."""

        if data == '':
            return self.space.wrap('')
        if not self.running:
            raise OperationError(
                self.space.w_EOFError,
                self.space.wrap("end of stream was already found"))

        in_bufsize = len(data)

        with lltype.scoped_alloc(rffi.CCHARP.TO, in_bufsize) as in_buf:
            for i in range(in_bufsize):
                in_buf[i] = data[i]
            self.bzs.c_next_in = in_buf
            rffi.setintfield(self.bzs, 'c_avail_in', in_bufsize)

            with OutBuffer(self.bzs) as out:
                while True:
                    bzerror = BZ2_bzDecompress(self.bzs)
                    if bzerror == BZ_STREAM_END:
                        if rffi.getintfield(self.bzs, 'c_avail_in') != 0:
                            unused = [
                                self.bzs.c_next_in[i] for i in range(
                                    rffi.getintfield(self.bzs, 'c_avail_in'))
                            ]
                            self.unused_data = "".join(unused)
                        self.running = False
                        break
                    if bzerror != BZ_OK:
                        _catch_bz2_error(self.space, bzerror)

                    if rffi.getintfield(self.bzs, 'c_avail_in') == 0:
                        break
                    elif rffi.getintfield(self.bzs, 'c_avail_out') == 0:
                        out.prepare_next_chunk()

                res = out.make_result_string()
                return self.space.wrap(res)
Beispiel #50
0
        def _connect(self, address):
            """Connect the socket to a remote address."""
            addr = address.lock()
            res = _c.socketconnect(self.fd, addr, address.addrlen)
            address.unlock()
            errno = _c.geterrno()
            timeout = self.timeout
            if timeout > 0.0 and res < 0 and errno == _c.EWOULDBLOCK:
                tv = rffi.make(_c.timeval)
                rffi.setintfield(tv, 'c_tv_sec', int(timeout))
                rffi.setintfield(tv, 'c_tv_usec',
                                 int((timeout-int(timeout)) * 1000000))
                fds = lltype.malloc(_c.fd_set.TO, flavor='raw')
                _c.FD_ZERO(fds)
                _c.FD_SET(self.fd, fds)
                fds_exc = lltype.malloc(_c.fd_set.TO, flavor='raw')
                _c.FD_ZERO(fds_exc)
                _c.FD_SET(self.fd, fds_exc)
                null = lltype.nullptr(_c.fd_set.TO)

                try:
                    n = _c.select(self.fd + 1, null, fds, fds_exc, tv)

                    if n > 0:
                        if _c.FD_ISSET(self.fd, fds):
                            # socket writable == connected
                            return (0, False)
                        else:
                            # per MS docs, call getsockopt() to get error
                            assert _c.FD_ISSET(self.fd, fds_exc)
                            return (self.getsockopt_int(_c.SOL_SOCKET,
                                                        _c.SO_ERROR), False)
                    elif n == 0:
                        return (_c.EWOULDBLOCK, True)
                    else:
                        return (_c.geterrno(), False)

                finally:
                    lltype.free(fds, flavor='raw')
                    lltype.free(fds_exc, flavor='raw')
                    lltype.free(tv, flavor='raw')

            if res == 0:
                errno = 0
            return (errno, False)
Beispiel #51
0
        def _connect(self, address):
            """Connect the socket to a remote address."""
            addr = address.lock()
            res = _c.socketconnect(self.fd, addr, address.addrlen)
            address.unlock()
            errno = _c.geterrno()
            timeout = self.timeout
            if timeout > 0.0 and res < 0 and errno == _c.EWOULDBLOCK:
                tv = rffi.make(_c.timeval)
                rffi.setintfield(tv, 'c_tv_sec', int(timeout))
                rffi.setintfield(tv, 'c_tv_usec',
                                 int((timeout - int(timeout)) * 1000000))
                fds = lltype.malloc(_c.fd_set.TO, flavor='raw')
                _c.FD_ZERO(fds)
                _c.FD_SET(self.fd, fds)
                fds_exc = lltype.malloc(_c.fd_set.TO, flavor='raw')
                _c.FD_ZERO(fds_exc)
                _c.FD_SET(self.fd, fds_exc)
                null = lltype.nullptr(_c.fd_set.TO)

                try:
                    n = _c.select(self.fd + 1, null, fds, fds_exc, tv)

                    if n > 0:
                        if _c.FD_ISSET(self.fd, fds):
                            # socket writable == connected
                            return (0, False)
                        else:
                            # per MS docs, call getsockopt() to get error
                            assert _c.FD_ISSET(self.fd, fds_exc)
                            return (self.getsockopt_int(
                                _c.SOL_SOCKET, _c.SO_ERROR), False)
                    elif n == 0:
                        return (_c.EWOULDBLOCK, True)
                    else:
                        return (_c.geterrno(), False)

                finally:
                    lltype.free(fds, flavor='raw')
                    lltype.free(fds_exc, flavor='raw')
                    lltype.free(tv, flavor='raw')

            if res == 0:
                errno = 0
            return (errno, False)
Beispiel #52
0
    def decompress(self, data):
        """decompress(data) -> string

        Provide more data to the decompressor object. It will return chunks
        of decompressed data whenever possible. If you try to decompress data
        after the end of stream is found, EOFError will be raised. If any data
        was found after the end of stream, it'll be ignored and saved in
        unused_data attribute."""

        if data == '':
            return self.space.wrap('')
        if not self.running:
            raise OperationError(self.space.w_EOFError,
                self.space.wrap("end of stream was already found"))

        in_bufsize = len(data)

        with lltype.scoped_alloc(rffi.CCHARP.TO, in_bufsize) as in_buf:
            for i in range(in_bufsize):
                in_buf[i] = data[i]
            self.bzs.c_next_in = in_buf
            rffi.setintfield(self.bzs, 'c_avail_in', in_bufsize)

            with OutBuffer(self.bzs) as out:
                while True:
                    bzerror = BZ2_bzDecompress(self.bzs)
                    if bzerror == BZ_STREAM_END:
                        if rffi.getintfield(self.bzs, 'c_avail_in') != 0:
                            unused = [self.bzs.c_next_in[i]
                                      for i in range(
                                          rffi.getintfield(self.bzs,
                                                           'c_avail_in'))]
                            self.unused_data = "".join(unused)
                        self.running = False
                        break
                    if bzerror != BZ_OK:
                        _catch_bz2_error(self.space, bzerror)

                    if rffi.getintfield(self.bzs, 'c_avail_in') == 0:
                        break
                    elif rffi.getintfield(self.bzs, 'c_avail_out') == 0:
                        out.prepare_next_chunk()

                res = out.make_result_string()
                return self.space.wrap(res)
Beispiel #53
0
def compress(space, data, compresslevel=9):
    """compress(data [, compresslevel=9]) -> string

    Compress data in one shot. If you want to compress data sequentially,
    use an instance of BZ2Compressor instead. The compresslevel parameter, if
    given, must be a number between 1 and 9."""

    if compresslevel < 1 or compresslevel > 9:
        raise OperationError(
            space.w_ValueError,
            space.wrap("compresslevel must be between 1 and 9"))

    with lltype.scoped_alloc(bz_stream.TO, zero=True) as bzs:
        in_bufsize = len(data)

        with lltype.scoped_alloc(rffi.CCHARP.TO, in_bufsize) as in_buf:
            for i in range(in_bufsize):
                in_buf[i] = data[i]
            bzs.c_next_in = in_buf
            rffi.setintfield(bzs, 'c_avail_in', in_bufsize)

            # conforming to bz2 manual, this is large enough to fit compressed
            # data in one shot. We will check it later anyway.
            with OutBuffer(bzs,
                           in_bufsize + (in_bufsize / 100 + 1) + 600) as out:

                bzerror = BZ2_bzCompressInit(bzs, compresslevel, 0, 0)
                if bzerror != BZ_OK:
                    _catch_bz2_error(space, bzerror)

                while True:
                    bzerror = BZ2_bzCompress(bzs, BZ_FINISH)
                    if bzerror == BZ_STREAM_END:
                        break
                    elif bzerror != BZ_FINISH_OK:
                        BZ2_bzCompressEnd(bzs)
                        _catch_bz2_error(space, bzerror)

                    if rffi.getintfield(bzs, 'c_avail_out') == 0:
                        out.prepare_next_chunk()

                res = out.make_result_string()
                BZ2_bzCompressEnd(bzs)
                return space.wrap(res)
Beispiel #54
0
def buffer_attach(space, py_obj, w_obj):
    """
    Fills a newly allocated PyBufferObject with the given (str) buffer object.
    """
    py_buf = rffi.cast(PyBufferObject, py_obj)
    py_buf.c_b_offset = 0
    rffi.setintfield(py_buf, 'c_b_readonly', 1)
    rffi.setintfield(py_buf, 'c_b_hash', -1)

    if isinstance(w_obj, SubBuffer):
        py_buf.c_b_offset = w_obj.offset
        w_obj = w_obj.buffer

    if isinstance(w_obj, StringBuffer):
        py_buf.c_b_base = rffi.cast(PyObject, 0) # space.wrap(w_obj.value)
        py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, rffi.str2charp(w_obj.as_str()))
        py_buf.c_b_size = w_obj.getlength()
    else:
        raise Exception("Fail fail fail fail fail")
Beispiel #55
0
def buffer_attach(space, py_obj, w_obj):
    """
    Fills a newly allocated PyBufferObject with the given (str) buffer object.
    """
    py_buf = rffi.cast(PyBufferObject, py_obj)
    py_buf.c_b_offset = 0
    rffi.setintfield(py_buf, 'c_b_readonly', 1)
    rffi.setintfield(py_buf, 'c_b_hash', -1)

    if isinstance(w_obj, SubBuffer):
        py_buf.c_b_offset = w_obj.offset
        w_obj = w_obj.buffer

    if isinstance(w_obj, StringBuffer):
        py_buf.c_b_base = rffi.cast(PyObject, 0)  # space.wrap(w_obj.value)
        py_buf.c_b_ptr = rffi.cast(rffi.VOIDP, rffi.str2charp(w_obj.as_str()))
        py_buf.c_b_size = w_obj.getlength()
    else:
        raise Exception("Fail fail fail fail fail")
Beispiel #56
0
def compress(space, data, compresslevel=9):
    """compress(data [, compresslevel=9]) -> string

    Compress data in one shot. If you want to compress data sequentially,
    use an instance of BZ2Compressor instead. The compresslevel parameter, if
    given, must be a number between 1 and 9."""

    if compresslevel < 1 or compresslevel > 9:
        raise OperationError(space.w_ValueError,
            space.wrap("compresslevel must be between 1 and 9"))

    with lltype.scoped_alloc(bz_stream.TO, zero=True) as bzs:
        in_bufsize = len(data)

        with lltype.scoped_alloc(rffi.CCHARP.TO, in_bufsize) as in_buf:
            for i in range(in_bufsize):
                in_buf[i] = data[i]
            bzs.c_next_in = in_buf
            rffi.setintfield(bzs, 'c_avail_in', in_bufsize)

            # conforming to bz2 manual, this is large enough to fit compressed
            # data in one shot. We will check it later anyway.
            with OutBuffer(bzs,
                           in_bufsize + (in_bufsize / 100 + 1) + 600) as out:

                bzerror = BZ2_bzCompressInit(bzs, compresslevel, 0, 0)
                if bzerror != BZ_OK:
                    _catch_bz2_error(space, bzerror)

                while True:
                    bzerror = BZ2_bzCompress(bzs, BZ_FINISH)
                    if bzerror == BZ_STREAM_END:
                        break
                    elif bzerror != BZ_FINISH_OK:
                        BZ2_bzCompressEnd(bzs)
                        _catch_bz2_error(space, bzerror)

                    if rffi.getintfield(bzs, 'c_avail_out') == 0:
                        out.prepare_next_chunk()

                res = out.make_result_string()
                BZ2_bzCompressEnd(bzs)
                return space.wrap(res)
Beispiel #57
0
 def GetVersionEx():
     info = lltype.malloc(OSVERSIONINFOEX, flavor='raw')
     rffi.setintfield(info, 'c_dwOSVersionInfoSize',
                      rffi.sizeof(OSVERSIONINFOEX))
     try:
         if not _GetVersionEx(info):
             raise lastWindowsError()
         return (rffi.cast(lltype.Signed, info.c_dwMajorVersion),
                 rffi.cast(lltype.Signed, info.c_dwMinorVersion),
                 rffi.cast(lltype.Signed, info.c_dwBuildNumber),
                 rffi.cast(lltype.Signed, info.c_dwPlatformId),
                 rffi.charp2str(rffi.cast(rffi.CCHARP,
                                          info.c_szCSDVersion)),
                 rffi.cast(lltype.Signed, info.c_wServicePackMajor),
                 rffi.cast(lltype.Signed, info.c_wServicePackMinor),
                 rffi.cast(lltype.Signed, info.c_wSuiteMask),
                 rffi.cast(lltype.Signed, info.c_wProductType))
     finally:
         lltype.free(info, flavor='raw')
Beispiel #58
0
    def descr__init__(self,
                      space,
                      w_ident,
                      filter=KQ_FILTER_READ,
                      flags=KQ_EV_ADD,
                      fflags=0,
                      data=0,
                      udata=r_uint(0)):
        if space.isinstance_w(w_ident, space.w_long):
            ident = space.uint_w(w_ident)
        else:
            ident = r_uint(space.c_filedescriptor_w(w_ident))

        self.event = lltype.malloc(kevent, flavor="raw")
        rffi.setintfield(self.event, "c_ident", ident)
        rffi.setintfield(self.event, "c_filter", filter)
        rffi.setintfield(self.event, "c_flags", flags)
        rffi.setintfield(self.event, "c_fflags", fflags)
        rffi.setintfield(self.event, "c_data", data)
        self.event.c_udata = rffi.cast(rffi.VOIDP, udata)