Esempio n. 1
0
    def descr_poll(self, space, timeout=-1.0, maxevents=-1):
        self.check_closed(space)
        if timeout < 0:
            timeout = -1.0
        else:
            timeout *= 1000.0

        if maxevents == -1:
            maxevents = FD_SETSIZE - 1
        elif maxevents < 1:
            raise oefmt(space.w_ValueError,
                        "maxevents must be greater than 0, not %d", maxevents)

        with lltype.scoped_alloc(rffi.CArray(epoll_event), maxevents) as evs:
            nfds = epoll_wait(self.epfd, evs, maxevents, int(timeout))
            if nfds < 0:
                raise exception_from_errno(space, space.w_IOError)

            elist_w = [None] * nfds
            for i in xrange(nfds):
                event = evs[i]
                elist_w[i] = space.newtuple(
                    [space.wrap(event.c_data.c_fd), space.wrap(event.c_events)]
                )
            return space.newlist(elist_w)
Esempio n. 2
0
    def descr_poll(self, space, timeout=-1.0, maxevents=-1):
        self.check_closed(space)
        if timeout < 0:
            timeout = -1.0
        else:
            timeout *= 1000.0

        if maxevents == -1:
            maxevents = FD_SETSIZE - 1
        elif maxevents < 1:
            raise operationerrfmt(space.w_ValueError,
                                  "maxevents must be greater than 0, not %d",
                                  maxevents)

        with lltype.scoped_alloc(rffi.CArray(epoll_event), maxevents) as evs:
            nfds = epoll_wait(self.epfd, evs, maxevents, int(timeout))
            if nfds < 0:
                raise exception_from_errno(space, space.w_IOError)

            elist_w = [None] * nfds
            for i in xrange(nfds):
                event = evs[i]
                elist_w[i] = space.newtuple([
                    space.wrap(event.c_data.c_fd),
                    space.wrap(event.c_events)
                ])
            return space.newlist(elist_w)
Esempio n. 3
0
    def descr__new__(space, w_subtype, sizehint=-1):
        if sizehint == -1:
            sizehint = FD_SETSIZE - 1
        elif sizehint < 0:
            raise operationerrfmt(space.w_ValueError, "sizehint must be greater than zero, got %d", sizehint)
        epfd = epoll_create(sizehint)
        if epfd < 0:
            raise exception_from_errno(space, space.w_IOError)

        return space.wrap(W_Epoll(space, epfd))
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
0
    def descr__new__(space, w_subtype, sizehint=-1):
        if sizehint == -1:
            sizehint = FD_SETSIZE - 1
        elif sizehint < 0:
            raise operationerrfmt(
                space.w_ValueError,
                "sizehint must be greater than zero, got %d", sizehint)
        epfd = epoll_create(sizehint)
        if epfd < 0:
            raise exception_from_errno(space, space.w_IOError)

        return space.wrap(W_Epoll(space, epfd))
Esempio n. 7
0
def setitimer(space, which, first, interval=0):
    with lltype.scoped_alloc(itimervalP.TO, 1) as new:

        timeval_from_double(first, new[0].c_it_value)
        timeval_from_double(interval, new[0].c_it_interval)

        with lltype.scoped_alloc(itimervalP.TO, 1) as old:

            ret = c_setitimer(which, new, old)
            if ret != 0:
                raise exception_from_errno(space, get_itimer_error(space))

            return itimer_retval(space, old[0])
Esempio n. 8
0
def setitimer(space, which, first, interval=0):
    with lltype.scoped_alloc(itimervalP.TO, 1) as new:

        timeval_from_double(first, new[0].c_it_value)
        timeval_from_double(interval, new[0].c_it_interval)

        with lltype.scoped_alloc(itimervalP.TO, 1) as old:

            ret = c_setitimer(which, new, old)
            if ret != 0:
                raise exception_from_errno(space, get_itimer_error(space))


            return itimer_retval(space, old[0])
Esempio n. 9
0
def setitimer(space, which, first, interval=0):
    """setitimer(which, seconds[, interval])
    Sets given itimer (one of ITIMER_REAL, ITIMER_VIRTUAL

    or ITIMER_PROF) to fire after value seconds and after
    that every interval seconds.
    The itimer can be cleared by setting seconds to zero.

    Returns old values as a tuple: (delay, interval).
    """
    with lltype.scoped_alloc(itimervalP.TO, 1) as new:

        timeval_from_double(first, new[0].c_it_value)
        timeval_from_double(interval, new[0].c_it_interval)

        with lltype.scoped_alloc(itimervalP.TO, 1) as old:

            ret = c_setitimer(which, new, old)
            if ret != 0:
                raise exception_from_errno(space, get_itimer_error(space))

            return itimer_retval(space, old[0])
Esempio n. 10
0
 def descr__new__(space, w_subtype):
     kqfd = syscall_kqueue()
     if kqfd < 0:
         raise exception_from_errno(space, space.w_IOError)
     return space.wrap(W_Kqueue(space, kqfd))
Esempio n. 11
0
    def descr_control(self, space, w_changelist, max_events, w_timeout=None):

        self.check_closed(space)

        if max_events < 0:
            raise operationerrfmt(
                space.w_ValueError,
                "Length of eventlist must be 0 or positive, got %d",
                max_events)

        if space.is_w(w_changelist, space.w_None):
            changelist_len = 0
        else:
            changelist_len = space.len_w(w_changelist)

        with lltype.scoped_alloc(rffi.CArray(kevent),
                                 changelist_len) as changelist:
            with lltype.scoped_alloc(rffi.CArray(kevent),
                                     max_events) as eventlist:
                with lltype.scoped_alloc(timespec) as timeout:

                    if not space.is_w(w_timeout, space.w_None):
                        _timeout = space.float_w(w_timeout)
                        if _timeout < 0:
                            raise operationerrfmt(
                                space.w_ValueError,
                                "Timeout must be None or >= 0, got %s",
                                str(_timeout))
                        sec = int(_timeout)
                        nsec = int(1e9 * (_timeout - sec))
                        rffi.setintfield(timeout, 'c_tv_sec', sec)
                        rffi.setintfield(timeout, 'c_tv_nsec', nsec)
                        ptimeout = timeout
                    else:
                        ptimeout = lltype.nullptr(timespec)

                    if not space.is_w(w_changelist, space.w_None):
                        i = 0
                        for w_ev in space.listview(w_changelist):
                            ev = space.interp_w(W_Kevent, w_ev)
                            changelist[i].c_ident = ev.event.c_ident
                            changelist[i].c_filter = ev.event.c_filter
                            changelist[i].c_flags = ev.event.c_flags
                            changelist[i].c_fflags = ev.event.c_fflags
                            changelist[i].c_data = ev.event.c_data
                            changelist[i].c_udata = ev.event.c_udata
                            i += 1
                        pchangelist = changelist
                    else:
                        pchangelist = lltype.nullptr(rffi.CArray(kevent))

                    nfds = syscall_kevent(self.kqfd, pchangelist,
                                          changelist_len, eventlist,
                                          max_events, ptimeout)
                    if nfds < 0:
                        raise exception_from_errno(space, space.w_IOError)
                    else:
                        elist_w = [None] * nfds
                        for i in xrange(nfds):

                            evt = eventlist[i]

                            w_event = W_Kevent(space)
                            w_event.event = lltype.malloc(kevent, flavor="raw")
                            w_event.event.c_ident = evt.c_ident
                            w_event.event.c_filter = evt.c_filter
                            w_event.event.c_flags = evt.c_flags
                            w_event.event.c_fflags = evt.c_fflags
                            w_event.event.c_data = evt.c_data
                            w_event.event.c_udata = evt.c_udata

                            elist_w[i] = w_event

                    return space.newlist(elist_w)
Esempio n. 12
0
    def descr_control(self, space, w_changelist, max_events, w_timeout):

        self.check_closed(space)

        if max_events < 0:
            raise oefmt(space.w_ValueError,
                        "Length of eventlist must be 0 or positive, got %d",
                        max_events)

        if space.is_w(w_changelist, space.w_None):
            changelist_len = 0
        else:
            changelist_len = space.len_w(w_changelist)

        with lltype.scoped_alloc(rffi.CArray(kevent), changelist_len) as changelist:
            with lltype.scoped_alloc(rffi.CArray(kevent), max_events) as eventlist:
                with lltype.scoped_alloc(timespec) as timeout:

                    if not space.is_w(w_timeout, space.w_None):
                        _timeout = space.float_w(w_timeout)
                        if _timeout < 0:
                            raise oefmt(space.w_ValueError,
                                        "Timeout must be None or >= 0, got %s",
                                        str(_timeout))
                        sec = int(_timeout)
                        nsec = int(1e9 * (_timeout - sec))
                        rffi.setintfield(timeout, 'c_tv_sec', sec)
                        rffi.setintfield(timeout, 'c_tv_nsec', nsec)
                        ptimeout = timeout
                    else:
                        ptimeout = lltype.nullptr(timespec)

                    if not space.is_w(w_changelist, space.w_None):
                        i = 0
                        for w_ev in space.listview(w_changelist):
                            ev = space.interp_w(W_Kevent, w_ev)
                            changelist[i].c_ident = ev.ident
                            changelist[i].c_filter = ev.filter
                            changelist[i].c_flags = ev.flags
                            changelist[i].c_fflags = ev.fflags
                            changelist[i].c_data = ev.data
                            changelist[i].c_udata = ev.udata
                            i += 1
                        pchangelist = changelist
                    else:
                        pchangelist = lltype.nullptr(rffi.CArray(kevent))

                    nfds = syscall_kevent(self.kqfd,
                                          pchangelist,
                                          changelist_len,
                                          eventlist,
                                          max_events,
                                          ptimeout)
                    if nfds < 0:
                        raise exception_from_errno(space, space.w_IOError)
                    else:
                        elist_w = [None] * nfds
                        for i in xrange(nfds):

                            evt = eventlist[i]

                            w_event = W_Kevent(space)
                            w_event.ident = evt.c_ident
                            w_event.filter = evt.c_filter
                            w_event.flags = evt.c_flags
                            w_event.fflags = evt.c_fflags
                            w_event.data = evt.c_data
                            w_event.udata = evt.c_udata

                            elist_w[i] = w_event

                    return space.newlist(elist_w)
Esempio n. 13
0
 def descr__new__(space, w_subtype):
     kqfd = syscall_kqueue()
     if kqfd < 0:
         raise exception_from_errno(space, space.w_IOError)
     return space.wrap(W_Kqueue(space, kqfd))
Esempio n. 14
0
 def clock_getres(space, clk_id):
     with lltype.scoped_alloc(TIMESPEC) as tp:
         ret = c_clock_getres(clk_id, tp)
         if ret != 0:
             raise exception_from_errno(space, space.w_IOError)
         return space.wrap(int(tp.c_tv_sec) + 1e-9 * int(tp.c_tv_nsec))
Esempio n. 15
0
 def clock_getres(space, clk_id):
     with lltype.scoped_alloc(TIMESPEC) as tp:
         ret = c_clock_getres(clk_id, tp)
         if ret != 0:
             raise exception_from_errno(space, space.w_IOError)
         return space.wrap(tp.c_tv_sec + tp.c_tv_nsec * 1e-9)