code: def expect_loop(self, timeout=-1):
          
          spawn = self.spawn
  
          if timeout is not None:
              end_time = time.time() + timeout
  
          try:
              incoming = spawn.buffer
              spawn._buffer = spawn.buffer_type()
              spawn._before = spawn.buffer_type()
              while True:
                  idx = self.new_data(incoming)
                  # Keep reading until exception or return.
                  if idx is not None:
                      return idx
                  # No match at this point
                  if (timeout is not None) and (timeout < 0):
                      return self.timeout()
                  # Still have time left, so read more data
                  incoming = spawn.read_nonblocking(spawn.maxread, timeout)
                  if self.spawn.delayafterread is not None:
                      time.sleep(self.spawn.delayafterread)
                  if timeout is not None:
                      timeout = end_time - time.time()
          except EOF as e:
              return self.eof(e)
          except TIMEOUT as e:
              return self.timeout(e)
          except:
              self.errored()
              raise
docs: Blocking expect