def _SelectViaPoll(_, rfds, wfds, efds, timeout):
  """poll() based replacement for pexpect.spawn.__select().

  As mentioned in the module docstring, this is required since Python's select
  is unable to wait for events on high-numbered file descriptors.  The API is
  as per select.select(), however if we are interrupted by a signal, we wait
  again for the remaining time.

  Args:
    _: An object, self, unused.
    rfds: A list, file descriptors to check for read.
    wfds: A list, file descriptors to check for write.
    efds: A list, file descriptors to check for exceptions.
    timeout: A float, timeout (seconds).

  Returns:
    A tuple of three lists, being the descriptors in each of the incoming lists
    which are ready for read, write or have an exception, respectively.
  """
  if wfds or efds:
    logging.fatal('Unexpected code change in pexpect: __select '
                  'called with wfds=%s efds=%s', wfds, efds)
  p = select.poll()
  for fd in rfds:
    p.register(fd, select.POLLIN)

  # See pexpect.spawn.__select for timeout handling logic; this is the same
  # in select() and poll(), except that the timeout argument to poll() is
  # in milliseconds.  poll() raises the same exception on timeout as select().
  if timeout is not None:
    end_time = time.time() + timeout
  while True:
    try:
      fdstate = p.poll(int(timeout * 1000) if timeout is not None else None)
      # Build a list of descriptors which select() would return as 'available
      # for read' (which includes EOF conditions which may be indicated as
      # POLLIN, POLLHUP or POLLIN|POLLHUP, depending on the type of file
      # descriptor).
      rrfds = []
      for fd, state in fdstate:
        if state & select.POLLIN or state & select.POLLHUP:
          rrfds.append(fd)
      return (rrfds, [], [])
    except select.error as e:
      if e[0] == errno.EINTR:
        if timeout is not None:
          timeout = end_time - time.time()
          if timeout < 0:
            return ([], [], [])
      else:
        raise

# Override pexpect.spawn.__select as mentioned in module docstring.
dataset/ETHPy150Open google/capirca/tools/ldpush/pexpect_connection.py/_SelectViaPoll/original