qmsk/net/lib/event2/event.pyx
changeset 47 b45a6648931c
parent 46 64b4ffb44754
parent 42 0ff56f7216ee
child 50 da394bb715af
--- a/qmsk/net/lib/event2/event.pyx	Tue Sep 01 00:04:26 2009 +0300
+++ b/qmsk/net/lib/event2/event.pyx	Tue Sep 01 00:04:54 2009 +0300
@@ -9,7 +9,7 @@
 
     cdef double t
 
-    if timeout :
+    if timeout is not None :
         t = timeout
 
         tv.tv_sec = <int>(t)
@@ -39,7 +39,11 @@
 
 cdef class event :
 
-    def __init__ (self, event_base base, lib.evutil_socket_t fd, short events) :
+    def __init__ (self, event_base base not None, lib.evutil_socket_t fd, short events) :
+
+        # libevent seems to segfault otherwise...
+        if events & (lib.EV_READ | lib.EV_WRITE) and fd < 0 :
+            raise ValueError(fd)
 
         # construct event using our internal callback, with self as the arg
         self.ev = lib.event_new(base.ev_base, fd, events, ev_callback, <void *>self)
@@ -48,7 +52,7 @@
             raise Exception("event_new")
     
 
-    def assign (self, event_base base, lib.evutil_socket_t fd, short events) :
+    def assign (self, event_base base not None, lib.evutil_socket_t fd, short events) :
         """
             Re-assign our event parameters to the given ones.
 
@@ -98,10 +102,13 @@
         lib.event_active(self.ev, fd, mask)
 
 
-    def pending (self, short mask) :
+    def pending (self, short mask = 0xffff) :
         """
             Returns a bool indicating if this event is pending (that is, has been .add()'d).
 
+            For convenience, this defaults to testing all possible flags, and so will return true if any events are
+            pending.
+
             XXX: support returning timeout value?
         """
 
@@ -119,7 +126,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
@@ -131,8 +145,9 @@
 
             XXX: what happens if event_base's __dealloc__ is triggered, but there are still event objects alive?
         """
-
-        lib.event_free(self.ev)
+        
+        if self.ev != NULL :
+            lib.event_free(self.ev)
 
 class CallbackEvent (event) :
     """