* Fix IndexError in PollScheduler._next_poll_event() by raising
authorZac Medico <zmedico@gentoo.org>
Fri, 13 Aug 2010 13:31:59 +0000 (06:31 -0700)
committerZac Medico <zmedico@gentoo.org>
Fri, 13 Aug 2010 13:31:59 +0000 (06:31 -0700)
  StopIteration instead.
* Add an optional timeout parameter to PollScheduler._schedule_wait()
  (this will be used in a later commit).

pym/_emerge/PollScheduler.py

index 4914b286a6b89e19597376a20c1b06afed32ed23..704ddc3171a387436710ed883a1e0c2196782d50 100644 (file)
@@ -3,6 +3,7 @@
 
 import logging
 import select
+import time
 
 from portage.util import writemsg_level
 
@@ -112,6 +113,8 @@ class PollScheduler(object):
                """
                if not self._poll_event_queue:
                        self._poll(timeout)
+                       if not self._poll_event_queue:
+                               raise StopIteration()
                return self._poll_event_queue.pop()
 
        def _poll_loop(self):
@@ -187,7 +190,7 @@ class PollScheduler(object):
                del self._poll_event_handlers[f]
                del self._poll_event_handler_ids[reg_id]
 
-       def _schedule_wait(self, wait_ids):
+       def _schedule_wait(self, wait_ids, timeout=None):
                """
                Schedule until wait_id is not longer registered
                for poll() events.
@@ -201,12 +204,18 @@ class PollScheduler(object):
                if isinstance(wait_ids, int):
                        wait_ids = frozenset([wait_ids])
 
+               start_time = None
+               if timeout is not None:
+                       start_time = 1000 * time.time()
                try:
                        while wait_ids.intersection(handler_ids):
-                               f, event = self._next_poll_event()
+                               f, event = self._next_poll_event(timeout=timeout)
                                handler, reg_id = event_handlers[f]
                                handler(f, event)
                                event_handled = True
+                               if timeout is not None:
+                                       if 1000 * time.time() - start_time >= timeout:
+                                               break
                except StopIteration:
                        event_handled = True