X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/026f5f3a6274709b7863c619c979dd32f0ee686f..6f9f028c43f77c76a0ac6d4f4f7223e5c6e2ba64:/eventloop.py?ds=sidebyside diff --git a/eventloop.py b/eventloop.py index 7da492d..932e1b8 100644 --- a/eventloop.py +++ b/eventloop.py @@ -6,10 +6,35 @@ A simple event loop. Please see EventLoop class documentation for more info. """ -from select import poll, POLLIN, POLLPRI, POLLERR +import select +from select import POLLIN, POLLPRI, POLLERR __ALL__ = ('EventLoop') +class LoopInterruptedError(RuntimeError): + r""" + LoopInterruptedError(select_error) -> LoopInterruptedError instance. + + This class is raised when the event loop is interrupted in an unexpected + way. It wraps a select error, which can be accessed using the 'select_error' + attribute. + """ + + def __init__(self, select_error): + r"""Initialize the object. + + See the class documentation for more info. + """ + self.select_error = select_error + + def __repr__(self): + r"repr(obj) -> Object representation." + return 'LoopInterruptedError(select_error=%r)' % self.select_error + + def __str__(self): + r"str(obj) -> String representation." + return 'Loop interrupted: %s' % self.select_error + class EventLoop: r"""EventLoop(file[, handler]) -> EventLoop instance @@ -53,7 +78,7 @@ class EventLoop: See EventLoop class documentation for more info. """ - self.poll = poll() + self.poll = select.poll() self._stop = False self.__register(file) self.handler = handler @@ -101,7 +126,10 @@ class EventLoop: then only 1 event is processed and then this method returns. """ while True: - res = self.poll.poll() + try: + res = self.poll.poll() + except select.error, e: + raise LoopInterruptedError(e) if self.handler is not None: self.handler(self) else: