Commit d83df914 authored by Jason Madden's avatar Jason Madden

test__core_timer libuv.

parent 98965e0b
......@@ -7,7 +7,7 @@ from __future__ import absolute_import, print_function
import sys
import os
import signal as signalmodule
import functools
from gevent._ffi.loop import GEVENT_CORE_EVENTS
from gevent._ffi.loop import _NOARGS
......@@ -17,6 +17,15 @@ __all__ = [
]
def not_while_active(func):
@functools.wraps(func)
def nw(self, *args, **kwargs):
if self.active:
raise AttributeError("not while active")
func(self, *args, **kwargs)
return nw
class LazyOnClass(object):
@classmethod
......@@ -164,6 +173,12 @@ class watcher(object):
def _watcher_ffi_unref(self):
raise NotImplementedError()
def _watcher_ffi_start_unref(self):
self._watcher_ffi_unref()
def _watcher_ffi_stop_ref(self):
self._watcher_ffi_ref()
# A string identifying the type of libev object we watch, e.g., 'ev_io'
# This should be a class attribute.
_watcher_type = None
......@@ -243,12 +258,12 @@ class watcher(object):
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args or _NOARGS
self._watcher_ffi_unref()
self._watcher_ffi_start_unref()
self.loop._keepaliveset.add(self)
self._watcher_ffi_start()
def stop(self):
self._watcher_ffi_ref()
self._watcher_ffi_stop_ref()
self._watcher_ffi_stop()
self.loop._keepaliveset.discard(self)
......@@ -258,6 +273,7 @@ class watcher(object):
def _get_priority(self):
return None
@not_while_active
def _set_priority(self, priority):
pass
......@@ -274,16 +290,6 @@ class watcher(object):
watcher = AbstractWatcherType('watcher', (object,), dict(watcher.__dict__))
import functools
def not_while_active(func):
@functools.wraps(func)
def nw(self, *args, **kwargs):
if self.active:
raise AttributeError("not while active")
func(self, *args, **kwargs)
return nw
class IoMixin(object):
EVENT_MASK = 0
......
......@@ -121,9 +121,8 @@ class watcher(_base.watcher):
def _get_priority(self):
return libev.ev_priority(self._watcher)
@_base.not_while_active
def _set_priority(self, priority):
if libev.ev_is_active(self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(self._watcher, priority)
priority = property(_get_priority, _set_priority)
......@@ -151,9 +150,8 @@ class io(_base.IoMixin, watcher):
def _get_fd(self):
return vfd_get(self._watcher.fd)
@_base.not_while_active
def _set_fd(self, fd):
if libev.ev_is_active(self._watcher):
raise AttributeError("'io' watcher attribute 'fd' is read-only while watcher is active")
vfd = vfd_open(fd)
vfd_free(self._watcher.fd)
self._watcher_init(self._watcher, self._watcher_callback, vfd, self._watcher.events)
......@@ -163,9 +161,8 @@ class io(_base.IoMixin, watcher):
def _get_events(self):
return self._watcher.events
@_base.not_while_active
def _set_events(self, events):
if libev.ev_is_active(self._watcher):
raise AttributeError("'io' watcher attribute 'events' is read-only while watcher is active")
self._watcher_init(self._watcher, self._watcher_callback, self._watcher.fd, events)
events = property(_get_events, _set_events)
......
......@@ -41,6 +41,14 @@ class watcher(_base.watcher):
def _watcher_ffi_unref(self):
libuv.uv_unref(self._watcher)
def _watcher_ffi_start_unref(self):
# libev manipulates these refs at start and stop for
# some reason; we don't
pass
def _watcher_ffi_stop_ref(self):
pass
def _get_ref(self):
return libuv.uv_has_ref(self._watcher)
......@@ -134,6 +142,12 @@ class timer(_base.TimerMixin, watcher):
int(self._repeat * 1000))
def again(self, callback, *args, **kw):
if not self.active:
# If we've never been started, this is the same as starting us.
# libuv makes the distinction, libev doesn't.
self.start(callback, *args, **kw)
return
self._again = True
try:
self.start(callback, *args, **kw)
......
......@@ -16,7 +16,7 @@ def main():
x.start(f)
if hasattr(loop, '_keepaliveset'):
assert x in loop._keepaliveset
assert x.active, x.pending
assert x.active, ("active", x.active, "pending", x.pending)
try:
x.priority = 1
raise AssertionError('must not be able to change priority of active watcher')
......@@ -27,9 +27,10 @@ def main():
assert called == [1], called
assert x.callback is None, x.callback
assert x.args is None, x.args
assert x.priority == 0, x
x.priority = 1
assert x.priority == 1, x
if x.priority is not None:
assert x.priority == 0, (x, x.priority)
x.priority = 1
assert x.priority == 1, x
x.stop()
if hasattr(loop, '_keepaliveset'):
assert x not in loop._keepaliveset
......
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