def pthread_kill(space, tid, signum): "Send a signal to a thread." ret = c_pthread_kill(tid, signum) if widen(ret) < 0: raise exception_from_saved_errno(space, space.w_OSError) # the signal may have been send to the current thread space.getexecutioncontext().checksignals()
def clock_getres(space, clk_id): with lltype.scoped_alloc(rtime.TIMESPEC) as tp: ret = rtime.c_clock_getres(clk_id, tp) if ret != 0: raise exception_from_saved_errno(space, space.w_IOError) t = float(rffi.getintfield(tp, "c_tv_sec")) + float(rffi.getintfield(tp, "c_tv_nsec")) * 0.000000001 return space.wrap(t)
def clock_getres(space, clk_id): with lltype.scoped_alloc(TIMESPEC) as timespec: ret = c_clock_getres(clk_id, timespec) if ret != 0: raise exception_from_saved_errno(space, space.w_OSError) secs = _timespec_to_seconds(timespec) return space.newfloat(secs)
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_saved_errno(space, space.w_IOError) elist_w = [None] * nfds for i in xrange(nfds): event = evs[i] elist_w[i] = space.newtuple([ space.newint(event.c_data.c_fd), space.newint(event.c_events) ]) return space.newlist(elist_w)
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_saved_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)
def epoll_ctl(self, space, ctl, w_fd, eventmask, ignore_ebadf=False): fd = space.c_filedescriptor_w(w_fd) result = pypy_epoll_ctl(self.epfd, ctl, fd, rffi.cast(rffi.UINT, eventmask)) if ignore_ebadf and get_saved_errno() == errno.EBADF: result = 0 if result < 0: raise exception_from_saved_errno(space, space.w_IOError)
def clock_getres(space, clk_id): with lltype.scoped_alloc(rtime.TIMESPEC) as tp: ret = rtime.c_clock_getres(clk_id, tp) if ret != 0: raise exception_from_saved_errno(space, space.w_IOError) t = (float(rffi.getintfield(tp, 'c_tv_sec')) + float(rffi.getintfield(tp, 'c_tv_nsec')) * 0.000000001) return space.newfloat(t)
def sigwait(space, w_signals): with SignalMask(space, w_signals) as sigset: with lltype.scoped_alloc(rffi.INTP.TO, 1) as signum_ptr: ret = c_sigwait(sigset, signum_ptr) if ret != 0: raise exception_from_saved_errno(space, space.w_OSError) signum = signum_ptr[0] return space.newint(signum)
def descr__new__(space, w_subtype): kqfd = syscall_kqueue() if kqfd < 0: raise exception_from_saved_errno(space, space.w_IOError) try: rposix.set_inheritable(kqfd, False) except OSError as e: raise wrap_oserror(space, e) return W_Kqueue(space, kqfd)
def descr__new__(space, w_subtype, sizehint=0, flags=0): if sizehint < 0: # 'sizehint' is otherwise ignored raise oefmt(space.w_ValueError, "sizehint must be greater than zero, got %d", sizehint) epfd = epoll_create1(flags | EPOLL_CLOEXEC) if epfd < 0: raise exception_from_saved_errno(space, space.w_IOError) return W_Epoll(space, epfd)
def clock_settime(space, clk_id, secs): with lltype.scoped_alloc(TIMESPEC) as timespec: integer_secs = rffi.cast(TIMESPEC.c_tv_sec, secs) frac = secs - widen(integer_secs) rffi.setintfield(timespec, 'c_tv_sec', integer_secs) rffi.setintfield(timespec, 'c_tv_nsec', int(frac * 1e9)) ret = c_clock_settime(clk_id, timespec) if ret != 0: raise exception_from_saved_errno(space, space.w_OSError)
def pthread_sigmask(space, how, w_signals): with SignalMask(space, w_signals) as sigset: with lltype.scoped_alloc(c_sigset_t.TO) as previous: ret = c_pthread_sigmask(how, sigset, previous) if ret != 0: raise exception_from_saved_errno(space, space.w_OSError) # if signals was unblocked, signal handlers have been called space.getexecutioncontext().checksignals() return _sigset_to_signals(space, previous)
def siginterrupt(space, signum, flag): """siginterrupt(sig, flag) -> None change system call restart behaviour: if flag is False, system calls will be restarted when interrupted by signal sig, else system calls will be interrupted. """ check_signum_in_range(space, signum) if rffi.cast(lltype.Signed, c_siginterrupt(signum, flag)) < 0: raise exception_from_saved_errno(space, space.w_OSError)
def descr__new__(space, w_subtype, sizehint=-1): if sizehint == -1: sizehint = FD_SETSIZE - 1 elif sizehint < 0: raise oefmt(space.w_ValueError, "sizehint must be greater than zero, got %d", sizehint) epfd = epoll_create(sizehint) if epfd < 0: raise exception_from_saved_errno(space, space.w_IOError) return space.wrap(W_Epoll(space, epfd))
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_saved_errno() == errno.EBADF: result = 0 if result < 0: raise exception_from_saved_errno(space, space.w_IOError)
def descr__new__(space, w_subtype, sizehint=-1): if sizehint == -1: sizehint = FD_SETSIZE - 1 elif sizehint < 0: raise oefmt(space.w_ValueError, "sizehint must be greater than zero, got %d", sizehint) epfd = epoll_create(sizehint) if epfd < 0: raise exception_from_saved_errno(space, space.w_IOError) return W_Epoll(space, epfd)
def descr__new__(space, w_subtype, sizehint=-1, flags=0): if sizehint == -1: sizehint = FD_SETSIZE - 1 elif sizehint <= 0: # 'sizehint' is otherwise ignored raise oefmt(space.w_ValueError, "sizehint must be positive or -1") epfd = epoll_create1(flags | EPOLL_CLOEXEC) if epfd < 0: raise exception_from_saved_errno(space, space.w_IOError) return W_Epoll(space, epfd)
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_saved_errno() == errno.EBADF: result = 0 if result < 0: raise exception_from_saved_errno(space, space.w_IOError)
def cloexec_pipe(space): """cloexec_pipe() -> (read_end, write_end) Create a pipe whose ends have the cloexec flag set.""" with lltype.scoped_alloc(rffi.CArrayPtr(rffi.INT).TO, 2) as fds: res = c_cloexec_pipe(fds) if res != 0: raise exception_from_saved_errno(space, space.w_OSError) return space.newtuple([space.wrap(fds[0]), space.wrap(fds[1]), ])
def cloexec_pipe(space): """cloexec_pipe() -> (read_end, write_end) Create a pipe whose ends have the cloexec flag set.""" with lltype.scoped_alloc(rffi.CArrayPtr(rffi.INT).TO, 2) as fds: res = c_cloexec_pipe(fds) if res != 0: raise exception_from_saved_errno(space, space.w_OSError) return space.newtuple([ space.newint(fds[0]), space.newint(fds[1]), ])
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_saved_errno(space, get_itimer_error(space)) return itimer_retval(space, old[0])
def sleep(space, w_secs): ns = timestamp_w(space, w_secs) if not (ns >= 0): raise oefmt(space.w_ValueError, "sleep length must be non-negative") end_time = _monotonic(space) + float(ns) / SECS_TO_NS while True: if _WIN: # as decreed by Guido, only the main thread can be # interrupted. main_thread = space.fromcache(State).main_thread interruptible = (main_thread == thread.get_ident()) millisecs = ns // MS_TO_NS if millisecs == 0 or not interruptible: rtime.sleep(float(ns) / SECS_TO_NS) break interrupt_event = space.fromcache(State).get_interrupt_event() rwin32.ResetEvent(interrupt_event) rc = rwin32.WaitForSingleObject(interrupt_event, millisecs) if rc != rwin32.WAIT_OBJECT_0: break else: void = lltype.nullptr(rffi.VOIDP.TO) with lltype.scoped_alloc(TIMEVAL) as t: seconds = ns // SECS_TO_NS us = (ns % SECS_TO_NS) // US_TO_NS rffi.setintfield(t, 'c_tv_sec', int(seconds)) rffi.setintfield(t, 'c_tv_usec', int(us)) res = rffi.cast(rffi.LONG, c_select(0, void, void, void, t)) if res == 0: break # normal path if rposix.get_saved_errno() != EINTR: raise exception_from_saved_errno(space, space.w_OSError) space.getexecutioncontext().checksignals() secs = end_time - _monotonic(space) # retry if secs <= 0: break
def descr_poll(self, space, timeout=-1.0, maxevents=-1): self.check_closed(space) if timeout < 0: end_time = 0.0 itimeout = -1 else: end_time = timeutils.monotonic(space) + timeout itimeout = int(timeout * 1000.0 + 0.999) 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: while True: nfds = epoll_wait(self.epfd, evs, maxevents, itimeout) if nfds < 0: if get_saved_errno() == errno.EINTR: space.getexecutioncontext().checksignals() if itimeout >= 0: timeout = end_time - timeutils.monotonic(space) timeout = max(timeout, 0.0) itimeout = int(timeout * 1000.0 + 0.999) continue raise exception_from_saved_errno(space, space.w_IOError) break elist_w = [None] * nfds for i in xrange(nfds): event = evs[i] elist_w[i] = space.newtuple( [space.newint(event.c_data.c_fd), space.newint(event.c_events)] ) return space.newlist(elist_w)
def PyFile_AsFile(space, w_p): """Return the file object associated with p as a FILE*. If the caller will ever use the returned FILE* object while the GIL is released it must also call the PyFile_IncUseCount() and PyFile_DecUseCount() functions as appropriate.""" if not PyFile_Check(space, w_p): raise oefmt(space.w_IOError, 'first argument must be an open file') assert isinstance(w_p, W_File) w_p.stream.flush_buffers() try: fd = space.int_w(space.call_method(w_p, 'fileno')) mode = w_p.mode except OperationError as e: raise oefmt(space.w_IOError, 'could not call fileno') if (fd < 0 or not mode or mode[0] not in ['r', 'w', 'a', 'U'] or ('U' in mode and ('w' in mode or 'a' in mode))): raise oefmt(space.w_IOError, 'invalid fileno or mode') ret = c_fdopen(fd, mode) if not ret: raise exception_from_saved_errno(space, space.w_IOError) # XXX fix this once use-file-star-for-file lands c_setvbuf(ret, lltype.nullptr(rffi.CCHARP.TO), _IONBF, 0) return ret
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_saved_errno( space, space.w_OSError) 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)
def descr__new__(space, w_subtype): kqfd = syscall_kqueue() if kqfd < 0: raise exception_from_saved_errno(space, space.w_IOError) return W_Kqueue(space, kqfd)
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_saved_errno(space, space.w_OSError) 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)
def descr__new__(space, w_subtype): kqfd = syscall_kqueue() if kqfd < 0: raise exception_from_saved_errno(space, space.w_IOError) return space.wrap(W_Kqueue(space, kqfd))
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_saved_errno(space, space.w_IOError) return space.wrap(int(tp.c_tv_sec) + 1e-9 * int(tp.c_tv_nsec))
def sigpending(space): with lltype.scoped_alloc(c_sigset_t.TO) as mask: ret = c_sigpending(mask) if ret != 0: raise exception_from_saved_errno(space, space.w_OSError) return _sigset_to_signals(space, mask)
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)) fill_timespec(_timeout, timeout) timeout_at = timeutils.monotonic(space) + _timeout ptimeout = timeout else: timeout_at = 0.0 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)) while True: nfds = syscall_kevent(self.kqfd, pchangelist, changelist_len, eventlist, max_events, ptimeout) if nfds >= 0: break if rposix.get_saved_errno() != errno.EINTR: raise exception_from_saved_errno( space, space.w_OSError) space.getexecutioncontext().checksignals() if ptimeout: _timeout = (timeout_at - timeutils.monotonic(space)) if _timeout < 0.0: _timeout = 0.0 fill_timespec(_timeout, ptimeout) 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)