Commit 82f34140 authored by Jason Madden's avatar Jason Madden

Move start into the watcher base.

parent b2ef20c4
...@@ -665,11 +665,9 @@ cdef public class callback [object PyGeventCallbackObject, type PyGeventCallback ...@@ -665,11 +665,9 @@ cdef public class callback [object PyGeventCallbackObject, type PyGeventCallback
# bit #2 set if ev_unref() was called and we must call ev_ref() later # bit #2 set if ev_unref() was called and we must call ev_ref() later
# bit #3 set if user wants to call ev_unref() before start() # bit #3 set if user wants to call ev_unref() before start()
#define WATCHER_BASE(TYPE) \ #define WATCHER(TYPE) \
cdef libev.ev_##TYPE _watcher \ cdef libev.ev_##TYPE _watcher \
\ \
\
\
def stop(self): \ def stop(self): \
_check_loop(self.loop) \ _check_loop(self.loop) \
if self._flags & 2: \ if self._flags & 2: \
...@@ -689,27 +687,11 @@ cdef public class callback [object PyGeventCallbackObject, type PyGeventCallback ...@@ -689,27 +687,11 @@ cdef public class callback [object PyGeventCallbackObject, type PyGeventCallback
self.args = args \ self.args = args \
LIBEV_UNREF \ LIBEV_UNREF \
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents) \ libev.ev_feed_event(self.loop._ptr, &self._watcher, revents) \
PYTHON_INCREF PYTHON_INCREF \
#define START(TYPE) def start(self, object callback, *args): \
_check_loop(self.loop) \
if callback is None: \
raise TypeError('callback must be callable, not None') \
self.callback = callback \
self.args = args \
LIBEV_UNREF \
libev.ev_##TYPE##_start(self.loop._ptr, &self._watcher) \
PYTHON_INCREF
#define WATCHER(TYPE) WATCHER_BASE(TYPE) \
\ \
START(TYPE) \ cdef void _do_libev_start(self): \
libev.ev_##TYPE##_start(self.loop._ptr, &self._watcher)
#define COMMA ,
cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Type]: cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Type]:
...@@ -780,6 +762,20 @@ cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Ty ...@@ -780,6 +762,20 @@ cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Ty
def pending(self): def pending(self):
return True if libev.ev_is_pending(self.__watcher) else False return True if libev.ev_is_pending(self.__watcher) else False
def start(self, object callback, *args):
_check_loop(self.loop)
if callback is None:
raise TypeError("Expected a callable, not None")
self.callback = callback
self.args = args
LIBEV_UNREF
self._do_libev_start()
PYTHON_INCREF
cdef void _do_libev_start(self):
# This is not allowed to fail, and must be implemented by subclasses.
return
def __repr__(self): def __repr__(self):
if Py_ReprEnter(<PyObjectPtr>self) != 0: if Py_ReprEnter(<PyObjectPtr>self) != 0:
return "<...>" return "<...>"
...@@ -814,21 +810,12 @@ cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Ty ...@@ -814,21 +810,12 @@ cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Ty
cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]: cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]:
WATCHER_BASE(io) WATCHER(io)
def start(self, object callback, *args, pass_events=False): def start(self, object callback, *args, pass_events=False):
_check_loop(self.loop)
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
if pass_events: if pass_events:
self.args = (GEVENT_CORE_EVENTS, ) + args args = (GEVENT_CORE_EVENTS, ) + args
else: watcher.start(self, callback, *args)
self.args = args
LIBEV_UNREF
libev.ev_io_start(self.loop._ptr, &self._watcher)
PYTHON_INCREF
cpdef __posix_cinit(self, loop loop, int fd, int events, ref=True, priority=None): cpdef __posix_cinit(self, loop loop, int fd, int events, ref=True, priority=None):
if fd < 0: if fd < 0:
...@@ -904,24 +891,16 @@ cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]: ...@@ -904,24 +891,16 @@ cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]:
cdef public class timer(watcher) [object PyGeventTimerObject, type PyGeventTimer_Type]: cdef public class timer(watcher) [object PyGeventTimerObject, type PyGeventTimer_Type]:
WATCHER_BASE(timer) WATCHER(timer)
update_loop_time_on_start = False update_loop_time_on_start = False
def start(self, object callback, *args, update=None): def start(self, object callback, *args, update=None):
_check_loop(self.loop)
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
LIBEV_UNREF
if update is None: if update is None:
update = self.update_loop_time_on_start update = self.update_loop_time_on_start
if update: if update:
libev.ev_now_update(self.loop._ptr) libev.ev_now_update(self.loop._ptr)
libev.ev_timer_start(self.loop._ptr, &self._watcher) watcher.start(self, callback, *args)
PYTHON_INCREF
def __cinit__(self, loop loop, double after=0.0, double repeat=0.0, ref=True, priority=None): def __cinit__(self, loop loop, double after=0.0, double repeat=0.0, ref=True, priority=None):
if repeat < 0.0: if repeat < 0.0:
...@@ -1007,10 +986,7 @@ cdef public class fork(watcher) [object PyGeventForkObject, type PyGeventFork_Ty ...@@ -1007,10 +986,7 @@ cdef public class fork(watcher) [object PyGeventForkObject, type PyGeventFork_Ty
cdef public class async_(watcher) [object PyGeventAsyncObject, type PyGeventAsync_Type]: cdef public class async_(watcher) [object PyGeventAsyncObject, type PyGeventAsync_Type]:
WATCHER_BASE(async) WATCHER(async)
START(async)
@property @property
def pending(self): def pending(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment