def method_ready(self, space): retval = None try: retval = poll({self.fd: POLLIN}, 0) except PollError: return space.w_nil return space.newbool(len(retval) > 0)
def poll(self, space, w_timeout): if space.is_w(w_timeout, space.w_None): timeout = -1 else: # we want to be compatible with cpython and also accept things # that can be casted to integer (I think) try: # compute the integer w_timeout = space.int(w_timeout) except OperationError: raise oefmt(space.w_TypeError, "timeout must be an integer or None") timeout = space.c_int_w(w_timeout) if self.running: raise oefmt(space.w_RuntimeError, "concurrent poll() invocation") self.running = True try: retval = rpoll.poll(self.fddict, timeout) except rpoll.PollError as e: w_errortype = space.fromcache(Cache).w_error message = e.get_msg() raise OperationError(w_errortype, space.newtuple([space.wrap(e.errno), space.wrap(message)])) finally: self.running = False retval_w = [] for fd, revents in retval: retval_w.append(space.newtuple([space.wrap(fd), space.wrap(revents)])) return space.newlist(retval_w)
def poll(self, space, w_timeout): if space.is_w(w_timeout, space.w_None): timeout = -1 else: # we want to be compatible with cpython and also accept things # that can be casted to integer (I think) try: # compute the integer w_timeout = space.int(w_timeout) except OperationError: raise oefmt(space.w_TypeError, "timeout must be an integer or None") timeout = space.c_int_w(w_timeout) if self.running: raise oefmt(space.w_RuntimeError, "concurrent poll() invocation") self.running = True try: retval = rpoll.poll(self.fddict, timeout) except rpoll.PollError as e: w_errortype = space.fromcache(Cache).w_error message = e.get_msg() raise OperationError( w_errortype, space.newtuple([space.newint(e.errno), space.newtext(message)])) finally: self.running = False retval_w = [] for fd, revents in retval: retval_w.append( space.newtuple([space.newint(fd), space.newint(revents)])) return space.newlist(retval_w)
def checkwait(space, w_sock, writing): """If the socket has a timeout, do a select()/poll() on the socket. The argument writing indicates the direction. Returns one of the possibilities in the timeout_state enum (above).""" w_timeout = space.call_method(w_sock, "gettimeout") if space.is_w(w_timeout, space.w_None): return SOCKET_IS_BLOCKING elif space.float_w(w_timeout) == 0.0: return SOCKET_IS_NONBLOCKING sock_timeout = space.float_w(w_timeout) sock_fd = space.int_w(space.call_method(w_sock, "fileno")) # guard against closed socket if sock_fd < 0: return SOCKET_HAS_BEEN_CLOSED # see if the socket is ready # Prefer poll, if available, since you can poll() any fd # which can't be done with select(). if HAVE_RPOLL: if writing: fddict = {sock_fd: rpoll.POLLOUT} else: fddict = {sock_fd: rpoll.POLLIN} # socket's timeout is in seconds, poll's timeout in ms timeout = int(sock_timeout * 1000 + 0.5) try: ready = rpoll.poll(fddict, timeout) except rpoll.PollError, e: message = e.get_msg() raise ssl_error(space, message, e.errno)
def poll(self, space, w_timeout): """ Polls the set of registered file descriptors, returning a list containing any descriptors that have events or errors to report. the timeout parameter is in milliseconds""" if space.is_w(w_timeout, space.w_None): timeout = -1 end_time = 0 elif space.isinstance_w(w_timeout, space.w_float) or space.isinstance_w( w_timeout, space.w_int): if space.is_true(space.lt(w_timeout, space.newint(0))): timeout = -1 end_time = 0 else: timeout = space.c_int_w(space.int(w_timeout)) end_time = timeutils.monotonic(space) + timeout * 0.001 else: raise oefmt(space.w_TypeError, "timeout must be an integer or None") if self.running: raise oefmt(space.w_RuntimeError, "concurrent poll() invocation") while True: self.running = True try: retval = rpoll.poll(self.fddict, timeout) except rpoll.PollError as e: if e.errno == errno.EINTR: space.getexecutioncontext().checksignals() timeout = int( (end_time - timeutils.monotonic(space)) * 1000.0 + 0.999) # round up if timeout < 0: timeout = 0 continue message, lgt = e.get_msg_utf8() raise OperationError( space.w_OSError, space.newtuple( [space.newint(e.errno), space.newtext(message, lgt)])) finally: self.running = False break retval_w = [] for fd, revents in retval: retval_w.append( space.newtuple([space.newint(fd), space.newint(revents)])) return space.newlist(retval_w)
def poll(self, space, w_timeout): """WARNING: the timeout parameter is in **milliseconds**!""" if space.is_w(w_timeout, space.w_None): timeout = -1 end_time = 0 else: # we want to be compatible with cpython and also accept things # that can be casted to integer (I think) try: # compute the integer w_timeout = space.int(w_timeout) except OperationError: raise oefmt(space.w_TypeError, "timeout must be an integer or None") timeout = space.c_int_w(w_timeout) end_time = timeutils.monotonic(space) + timeout * 0.001 if self.running: raise oefmt(space.w_RuntimeError, "concurrent poll() invocation") while True: self.running = True try: retval = rpoll.poll(self.fddict, timeout) except rpoll.PollError as e: if e.errno == errno.EINTR: space.getexecutioncontext().checksignals() timeout = int( (end_time - timeutils.monotonic(space)) * 1000.0 + 0.999) # round up if timeout < 0: timeout = 0 continue message = e.get_msg_unicode() raise OperationError( space.w_OSError, space.newtuple( [space.newint(e.errno), space.newunicode(message)])) finally: self.running = False break retval_w = [] for fd, revents in retval: retval_w.append( space.newtuple([space.newint(fd), space.newint(revents)])) return space.newlist(retval_w)
def check_socket_and_wait_for_timeout(space, w_sock, writing): """If the socket has a timeout, do a select()/poll() on the socket. The argument writing indicates the direction. Returns one of the possibilities in the timeout_state enum (above).""" w_timeout = space.call_method(w_sock, "gettimeout") if space.is_w(w_timeout, space.w_None): return SOCKET_IS_BLOCKING elif space.float_w(w_timeout) == 0.0: return SOCKET_IS_NONBLOCKING sock_timeout = space.float_w(w_timeout) sock_fd = space.int_w(space.call_method(w_sock, "fileno")) # guard against closed socket if sock_fd < 0: return SOCKET_HAS_BEEN_CLOSED # see if the socket is ready # Prefer poll, if available, since you can poll() any fd # which can't be done with select(). if HAVE_RPOLL: if writing: fddict = {sock_fd: rpoll.POLLOUT} else: fddict = {sock_fd: rpoll.POLLIN} # socket's timeout is in seconds, poll's timeout in ms timeout = int(sock_timeout * 1000 + 0.5) ready = rpoll.poll(fddict, timeout) else: if MAX_FD_SIZE is not None and sock_fd >= MAX_FD_SIZE: return SOCKET_TOO_LARGE_FOR_SELECT if writing: r, w, e = rpoll.select([], [sock_fd], [], sock_timeout) ready = w else: r, w, e = rpoll.select([sock_fd], [], [], sock_timeout) ready = r if ready: return SOCKET_OPERATION_OK else: return SOCKET_HAS_TIMED_OUT