* fix timeout=0.0
authorTero Marttila <terom@fixme.fi>
Mon, 31 Aug 2009 21:36:15 +0300
changeset 41 02f7c0539843
parent 40 a5d498bbf40a
child 42 0ff56f7216ee
* fix timeout=0.0
* fix loop() return value for case ret == 0, ret > 1
* change event_base to raise EventCallErrors (to be renamed...?)
* move some event docstrings to __call__
qmsk/net/lib/event2/base.pyx
qmsk/net/lib/event2/event.pxd
qmsk/net/lib/event2/event.pyx
--- a/qmsk/net/lib/event2/base.pyx	Mon Aug 31 21:34:54 2009 +0300
+++ b/qmsk/net/lib/event2/base.pyx	Mon Aug 31 21:36:15 2009 +0300
@@ -1,6 +1,24 @@
 from qmsk.net.lib.event2.base cimport *
 from qmsk.net.lib.event2.event cimport build_timeout
 
+class EventError (Exception) :
+    """
+        Base class of errors raised by lib.event2 code
+    """
+
+    pass
+
+class EventCallError (EventError) :
+    """
+        Some libevent function returned an error code.
+    """
+    
+    def __init__ (self, func, msg=None) :
+        super(EventCallError, self).__init__(func)
+
+        self.func = func
+        self.msg = msg
+
 cdef class event_base :
 
     def __init__ (self) :
@@ -9,7 +27,7 @@
         self.ev_base = lib.event_base_new()
 
         if self.ev_base == NULL :
-            raise Exception("event_base_new")
+            raise EventCallError("event_base_new")
 
     def reinit (self) :
         """
@@ -19,7 +37,7 @@
         """
 
         if lib.event_reinit(self.ev_base) < 0 :
-            raise Exception("event_reinit: could not re-add all events")
+            raise EventCallError("event_reinit", "could not re-add all events")
 
     property method :
         def __get__ (self) :
@@ -29,13 +47,14 @@
 
             return lib.event_base_get_method(self.ev_base)
 
-
     def loop (self, once = False, nonblock = False) :
         """
             Run the event loop.
 
                 once        - only run the event loop once, at most
                 nonblock    - do not block waiting for events
+
+            Returns True if succesfull, False if no events were registered
         """
 
         cdef int flags = 0
@@ -48,8 +67,16 @@
             flags |= lib.EVLOOP_NONBLOCK
 
         # event_base_loop()
-        if lib.event_base_loop(self.ev_base, flags) < 0 :
-            raise Exception("event_base_loop")
+        ret = lib.event_base_loop(self.ev_base, flags)
+
+        if ret < 0 :
+            raise EventCallError("event_base_loop")
+
+        elif ret > 0 :
+            return False
+
+        else :
+            return True
 
     def loopexit (self, timeout = None) :
         """
@@ -62,7 +89,7 @@
         cdef lib.timeval tv
 
         if lib.event_base_loopexit(self.ev_base, build_timeout(&tv, timeout)) < 0 :
-            raise Exception("event_base_loopexit")
+            raise EventCallError("event_base_loopexit")
 
     def loopbreak (self) :
         """
@@ -73,7 +100,7 @@
         """
 
         if lib.event_base_loopbreak(self.ev_base) < 0 :
-            raise Exception("event_base_loopbreak")
+            raise EventCallError("event_base_loopbreak")
 
     def __dealloc__ (self) :
         """
--- a/qmsk/net/lib/event2/event.pxd	Mon Aug 31 21:34:54 2009 +0300
+++ b/qmsk/net/lib/event2/event.pxd	Mon Aug 31 21:36:15 2009 +0300
@@ -20,12 +20,7 @@
             fd          - OS file descriptor to watch, or -1
             events      - bitmask of EV_* flags that represents the events to wait for
 
-        When the event fires, it will "call" this event object, so __call__ gets the following parameters:
-            fd          - OS file descriptor the event occured on, or -1
-            mask        - bitmask of EV_* flags that represents the triggered event
-    
-        The default implementation of __call__ does nothing. The method's return value will be ignored, and should be
-        None. Any errors raised by the callback will be printed out as warning messages, and ignored.
+        When the event fires, it will "call" this event object. See __call__().
     """
 
     # the underlying event object
--- a/qmsk/net/lib/event2/event.pyx	Mon Aug 31 21:34:54 2009 +0300
+++ b/qmsk/net/lib/event2/event.pyx	Mon Aug 31 21:36:15 2009 +0300
@@ -9,7 +9,7 @@
 
     cdef double t
 
-    if timeout :
+    if timeout is not None :
         t = timeout
 
         tv.tv_sec = <int>(t)
@@ -119,7 +119,14 @@
 
     def __call__ (self, lib.evutil_socket_t fd, short mask) :
         """
-            The method invoked by the internal libevent callback.
+            The method invoked by the internal libevent callback when the event becomes active.
+            
+                fd          - OS file descriptor the event occured on, or -1
+                mask        - bitmask of EV_* flags that represents the triggered event
+
+
+            The default implementation of __call__ does nothing. The method's return value will be ignored, and should
+            be None. Any errors raised by the callback will be printed out as warning messages, and ignored.
         """
 
         pass