Commit 98ef59ed authored by Denis Bilenko's avatar Denis Bilenko

simplify code in core.pyx

parent 324e0a12
...@@ -149,7 +149,7 @@ cdef extern from "libevent.h": ...@@ -149,7 +149,7 @@ cdef extern from "libevent.h":
cdef void __event_handler(int fd, short evtype, void *arg) with gil: cdef void __event_handler(int fd, short evtype, void *arg) with gil:
cdef event self = <event>arg cdef event self = <event>arg
try: try:
self._callback(self, evtype) self.callback(self, evtype)
except: except:
traceback.print_exc() traceback.print_exc()
try: try:
...@@ -171,12 +171,13 @@ cdef class event: ...@@ -171,12 +171,13 @@ cdef class event:
- *arg* -- optional object, which will be made available as :attr:`arg` property. - *arg* -- optional object, which will be made available as :attr:`arg` property.
""" """
cdef event_t ev cdef event_t ev
cdef object _callback, _arg cdef public object callback
cdef public object arg
cdef int _incref # 1 if we already INCREFed this object once (because libevent references it) cdef int _incref # 1 if we already INCREFed this object once (because libevent references it)
def __init__(self, short evtype, int handle, callback, arg=None): def __init__(self, short evtype, int handle, callback, arg=None):
self._callback = callback self.callback = callback
self._arg = arg self.arg = arg
self._incref = 0 self._incref = 0
cdef void* c_self = <void*>self cdef void* c_self = <void*>self
if evtype == 0 and not handle: if evtype == 0 and not handle:
...@@ -194,27 +195,6 @@ cdef class event: ...@@ -194,27 +195,6 @@ cdef class event:
Py_DECREF(self) Py_DECREF(self)
self._incref -= 1 self._incref -= 1
property callback:
"""User callback that will be called once the event is signalled.
The prototype: ``callback(event instance, evtype)``.
"""
def __get__(self):
return self._callback
def __set__(self, new):
self._callback = new
property arg:
"""Optional object set and read only by the user."""
def __get__(self):
return self._arg
def __set__(self, new):
self._arg = new
property pending: property pending:
"""Return True if the event is still scheduled to run.""" """Return True if the event is still scheduled to run."""
...@@ -317,7 +297,7 @@ cdef class event: ...@@ -317,7 +297,7 @@ cdef class event:
else: else:
events_str = '' events_str = ''
return '<%s at %s%s fd=%s%s flags=%s cb=%s arg=%s>' % \ return '<%s at %s%s fd=%s%s flags=%s cb=%s arg=%s>' % \
(type(self).__name__, hex(id(self)), pending, self.fd, events_str, self.flags_str, self._callback, self._arg) (type(self).__name__, hex(id(self)), pending, self.fd, events_str, self.flags_str, self.callback, self.arg)
def __str__(self): def __str__(self):
if self.pending: if self.pending:
...@@ -328,8 +308,8 @@ cdef class event: ...@@ -328,8 +308,8 @@ cdef class event:
events_str = ' %s' % self.events_str events_str = ' %s' % self.events_str
else: else:
events_str = '' events_str = ''
cb = str(self._callback).replace('\n', '\n' + ' ' * 8) cb = str(self.callback).replace('\n', '\n' + ' ' * 8)
arg = pformat(self._arg, indent=2).replace('\n', '\n' + ' ' * 8) arg = pformat(self.arg, indent=2).replace('\n', '\n' + ' ' * 8)
return '%s%s fd=%s%s flags=%s\n cb = %s\n arg = %s' % \ return '%s%s fd=%s%s flags=%s\n cb = %s\n arg = %s' % \
(type(self).__name__, pending, self.fd, events_str, self.flags_str, cb, arg) (type(self).__name__, pending, self.fd, events_str, self.flags_str, cb, arg)
...@@ -376,8 +356,8 @@ class readwrite_event(event): ...@@ -376,8 +356,8 @@ class readwrite_event(event):
cdef void __simple_handler(int fd, short evtype, void *arg) with gil: cdef void __simple_handler(int fd, short evtype, void *arg) with gil:
cdef event self = <event>arg cdef event self = <event>arg
try: try:
args, kwargs = self._arg args, kwargs = self.arg
self._callback(*args, **kwargs) self.callback(*args, **kwargs)
except: except:
traceback.print_exc() traceback.print_exc()
try: try:
...@@ -394,8 +374,8 @@ cdef class timer(event): ...@@ -394,8 +374,8 @@ cdef class timer(event):
"""Create a new scheduled timer""" """Create a new scheduled timer"""
def __init__(self, float seconds, callback, *args, **kwargs): def __init__(self, float seconds, callback, *args, **kwargs):
self._callback = callback self.callback = callback
self._arg = (args, kwargs) self.arg = (args, kwargs)
evtimer_set(&self.ev, __simple_handler, <void*>self) evtimer_set(&self.ev, __simple_handler, <void*>self)
self.add(seconds) self.add(seconds)
...@@ -404,8 +384,8 @@ cdef class signal(event): ...@@ -404,8 +384,8 @@ cdef class signal(event):
"""Create a new persistent signal event""" """Create a new persistent signal event"""
def __init__(self, int signalnum, callback, *args, **kwargs): def __init__(self, int signalnum, callback, *args, **kwargs):
self._callback = callback self.callback = callback
self._arg = (args, kwargs) self.arg = (args, kwargs)
event_set(&self.ev, signalnum, EV_SIGNAL|EV_PERSIST, __simple_handler, <void*>self) event_set(&self.ev, signalnum, EV_SIGNAL|EV_PERSIST, __simple_handler, <void*>self)
self.add() self.add()
...@@ -414,8 +394,8 @@ cdef class active_event(event): ...@@ -414,8 +394,8 @@ cdef class active_event(event):
"""An event that is scheduled to run in the current loop iteration""" """An event that is scheduled to run in the current loop iteration"""
def __init__(self, callback, *args, **kwargs): def __init__(self, callback, *args, **kwargs):
self._callback = callback self.callback = callback
self._arg = (args, kwargs) self.arg = (args, kwargs)
evtimer_set(&self.ev, __simple_handler, <void*>self) evtimer_set(&self.ev, __simple_handler, <void*>self)
self._addref() self._addref()
event_active(&self.ev, EV_TIMEOUT, 1) event_active(&self.ev, EV_TIMEOUT, 1)
......
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