Commits

Denis Bilenko committed 6cad155

initial commit

  • Participants

Comments (0)

Files changed (58)

+syntax: glob
+*~
+*.pyc
+*.orig
+dist
+gevent.egg-info
+build
+htmlreports
+results.*.db
+gevent/core.so
+
+recursive-include greentest *.py *.crt *.key
+recursive-include examples *.py
+include MANIFEST.in
+= eventlet =
+
+Eventlet is a networking library written in Python. It achieves high
+scalability by using non-blocking io while at the same time retaining
+high programmer usability by using coroutines to make the non-blocking
+io operations appear blocking at the source code level.
+
+The wiki at http://wiki.secondlife.com/wiki/Eventlet is likely to be a
+more current source of information than this README.  Questions,
+patches, and general discussion go to the eventlet mailing list:
+https://lists.secondlife.com/cgi-bin/mailman/listinfo/eventletdev
+
+== requirements ===
+
+Eventlet runs on Python version 2.3 or greater, with the following dependenceis:
+* http://cheeseshop.python.org/pypi/greenlet
+* (if running python versions < 2.4) collections.py from the 2.4 distribution or later
+
+== limitations ==
+
+* Not enough test coverage -- the goal is 100%, but we are not there yet.
+* Eventlet does not currently run on stackless using tasklets, though
+it is a goal to do so in the future.
+
+== getting started ==
+
+% python
+>>> from eventlet import api
+>>> help(api)
+
+Also, look at the examples in the examples directory.
+
+== eventlet history ==
+
+eventlet began life as Donovan Preston was talking to Bob Ippolito
+about coroutine-based non-blocking networking frameworks in
+Python. Most non-blocking frameworks require you to run the "main
+loop" in order to perform all network operations, but Donovan wondered
+if a library written using a trampolining style could get away with
+transparently running the main loop any time i/o was required,
+stopping the main loop once no more i/o was scheduled. Bob spent a few
+days during PyCon 2005 writing a proof-of-concept. He named it
+eventlet, after the coroutine implementation it used,
+[[greenlet]]. Donovan began using eventlet as a light-weight network
+library for his spare-time project Pavel, and also began writing some
+unittests.
+
+* http://svn.red-bean.com/bob/eventlet/trunk/
+* http://soundfarmer.com/Pavel/trunk/
+
+When Donovan started at Linden Lab in May of 2006, he added eventlet
+as an svn external in the indra/lib/python directory, to be a
+dependency of the yet-to-be-named [[backbone]] project (at the time,
+it was named restserv). However, including eventlet as an svn external
+meant that any time the externally hosted project had hosting issues,
+Linden developers were not able to perform svn updates. Thus, the
+eventlet source was imported into the linden source tree at the same
+location, and became a fork.
+
+Bob Ippolito has ceased working on eventlet and has stated his desire
+for Linden to take its fork forward to the open source world as "the"
+eventlet.

examples/connect.py

+# Copyright (c) 2008-2009 AG Projects
+# Author: Denis Bilenko
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+"""Spawn multiple workers and collect their results"""
+
+import gevent
+from gevent import proc
+from gevent import socket
+
+def geturl(url):
+    c = socket.GreenSocket()
+    c.connect((url, 80))
+    print '%s connected' % url
+    gevent.sleep(0.05)
+    c.sendall('GET /\r\n\r\n')
+    print '%s sent request' % url
+    return c.recv(1024)
+
+urls = ['www.google.com', 'www.yandex.ru', 'www.python.org']
+jobs = [proc.spawn(geturl, x) for x in urls]
+print 'spawned %s jobs' % len(jobs)
+
+# collect the results from workers
+results = proc.waitall(jobs)
+# Note, that any exception in the workers will be reraised by waitall
+# unless trap_errors argument specifies otherwise
+
+for url, result in zip(urls, results):
+    print '%s: %s' % (url, repr(result)[:50])
+

examples/echoserver.py

+#! /usr/bin/env python
+
+# Copyright (c) 2007, Linden Research, Inc.
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+# 
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+"""Simple server that listens on port 6000 and echos back every input to
+the client.  To try out the server, start it up by running this file.
+
+Connect to it with:
+  telnet localhost 6000
+
+You terminate your connection by terminating telnet (typically Ctrl-]
+and then 'quit')
+
+"""
+import gevent
+from gevent import socket
+
+def handle_socket(reader, writer):
+    print "client connected"
+    while True:
+        # pass through every non-eof line
+        x = reader.readline()
+        if not x: break
+        writer.write(x)
+        print "echoed", x
+    print "client disconnected"
+
+print "server socket listening on port 6000"
+server = socket.tcp_listener(('0.0.0.0', 6000))
+while True:
+    try:
+        new_sock, address = server.accept()
+    except KeyboardInterrupt:
+        break
+    # handle every new connection with a new coroutine
+    gevent.spawn(handle_socket, new_sock.makefile('r'), new_sock.makefile('w'))
+
+"""This is a simple example of running a wsgi application with gevent.
+For a more fully-featured server which supports multiple processes,
+multiple threads, and graceful code reloading, see:
+
+http://pypi.python.org/pypi/Spawning/
+"""
+from gevent import wsgi, socket
+
+def hello_world(env, start_response):
+    if env['PATH_INFO'] != '/':
+        start_response('404 Not Found', [('Content-Type', 'text/plain')])
+        return ['Not Found\r\n']
+    else:
+        start_response('200 OK', [('Content-Type', 'text/plain')])
+        return ["Hello World!\r\n"]
+
+wsgi.server(socket.tcp_listener(('', 8080)), hello_world)
+

gevent/__init__.py

+import sys
+import os
+import traceback
+
+from gevent import core
+from py.magic import greenlet
+
+version_info = (0, 9, 0)
+__version__ = '0.9.0'
+
+__all__ = ['Greenlet',
+           'getcurrent',
+           'GreenletExit',
+           'MAIN',
+           'TimeoutError',
+           'spawn',
+           'kill',
+           'get_hub',
+           'sleep',
+           'wait_reader',
+           'wait_writer',
+           'timeout',
+           'with_timeout']
+
+
+libevent_version = core.get_version()
+libevent_headers_version = core.get_header_version()
+if libevent_version != libevent_headers_version:
+    import warnings
+    msg = "version mismatch: system libevent version is %r but compiled with %r" % (libevent_version, libevent_headers_version)
+    warnings.warn(msg, UserWarning, stacklevel=2)
+
+
+_threadlocal = None
+Greenlet = greenlet
+getcurrent = greenlet.getcurrent
+GreenletExit = greenlet.GreenletExit
+MAIN = greenlet.getcurrent()
+
+def timer(*args, **kwargs):
+    return core.timer(*args, **kwargs)
+
+class TimeoutError(Exception):
+    """Exception raised if an asynchronous operation times out"""
+
+
+def spawn(function, *args, **kwargs):
+    """Create a new greenlet that will run `function(*args)'.
+    The current greenlet won't be unscheduled. Keyword arguments aren't
+    supported (limitation of greenlet), use spawn() to work around that.
+    """
+    g = Greenlet(lambda : function(*args, **kwargs))
+    g.parent = get_hub().greenlet
+    timer(0, g.switch)
+    return g
+
+
+def spawn_later(seconds, function, *args, **kwargs):
+    """Create a new greenlet that will run `function(*args)'.
+    The current greenlet won't be unscheduled. Keyword arguments aren't
+    supported (limitation of greenlet), use spawn() to work around that.
+    """
+    g = Greenlet(lambda : function(*args, **kwargs))
+    g.parent = get_hub().greenlet
+    timer(seconds, g.switch)
+    return g
+
+
+def kill(g, *throw_args):
+    timer(0, g.throw, *throw_args)
+    if getcurrent() is not get_hub().greenlet:
+        sleep(0)
+
+
+def get_hub():
+    global _threadlocal
+    try:
+        hub = _threadlocal.hub
+    except AttributeError:
+        # do not import anything that can be monkey-patched at top level
+        import threading
+        _threadlocal = threading.local()
+        hub = _threadlocal.hub = Hub()
+    return hub
+
+
+def sleep(seconds=0):
+    """Yield control to another eligible coroutine until at least *seconds* have
+    elapsed.
+
+    *seconds* may be specified as an integer, or a float if fractional seconds
+    are desired. Calling sleep with *seconds* of 0 is the canonical way of
+    expressing a cooperative yield. For example, if one is looping over a
+    large list performing an expensive calculation without calling any socket
+    methods, it's a good idea to call ``sleep(0)`` occasionally; otherwise
+    nothing else will run.
+    """
+    hub = get_hub()
+    assert hub.greenlet is not greenlet.getcurrent(), 'do not call blocking functions from the mainloop'
+    t = timer(seconds, greenlet.getcurrent().switch)
+    try:
+        hub.switch()
+    finally:
+        t.cancel()
+
+
+class Hub(object):
+
+    def __init__(self):
+        self.greenlet = Greenlet(self.run)
+        self.keyboard_interrupt_signal = None
+
+    def switch(self):
+        cur = getcurrent()
+        assert cur is not self.greenlet, 'Cannot switch to MAINLOOP from MAINLOOP'
+        switch_out = getattr(cur, 'switch_out', None)
+        if switch_out is not None:
+            try:
+                switch_out()
+            except:
+                traceback.print_exception(*sys.exc_info())
+        if self.greenlet.dead:
+            self.greenlet = Greenlet(self.run)
+        return self.greenlet.switch()
+
+    def run(self, *args, **kwargs):
+        if self.keyboard_interrupt_signal is None:
+            self.keyboard_interrupt_signal = signal(2, MAIN.throw, KeyboardInterrupt)
+        while True:
+            result = core.dispatch()
+            if result>0:
+                return 'Hub.run() has finished because there are no events registered'
+            elif result<0:
+                return 'Hub.run() has finished because there was an error'
+            return result
+
+
+def signal(signalnum, handler, *args, **kwargs):
+    def deliver_exception_to_MAIN():
+        try:
+            handler(*args, **kwargs)
+        except:
+            MAIN.throw(*sys.exc_info())
+    return core.signal(signalnum, deliver_exception_to_MAIN)
+
+def _wait_helper(ev, fd, evtype):
+    current, timeout_exc = ev.arg
+    if evtype & core.EV_TIMEOUT:
+        current.throw(timeout_exc)
+    else:
+        current.switch(ev)
+
+def wait_reader(fileno, timeout=-1, timeout_exc=TimeoutError):
+    evt = core.read(fileno, _wait_helper, timeout, (getcurrent(), timeout_exc))
+    try: 
+        returned_ev = get_hub().switch()
+        assert evt is returned_ev, (evt, returned_ev)
+    finally:
+        evt.cancel()
+
+def wait_writer(fileno, timeout=-1, timeout_exc=TimeoutError):
+    evt = core.write(fileno, _wait_helper, timeout, (getcurrent(), timeout_exc))
+    try:
+        returned_ev = get_hub().switch()
+        assert evt is returned_ev, (evt, returned_ev)
+    finally:
+        evt.cancel()
+
+
+class _SilentException:
+    pass
+
+
+class timeout(object):
+    """Schedule an exception to raise in the current greenlet (TimeoutError by default).
+    
+    Raise an exception in the block after timeout.
+
+    with timeout(seconds[, exc]):
+        ... code block ...
+
+    Assuming code block is yielding (i.e. gives up control to the hub),
+    an exception provided in `exc' argument will be raised
+    (TimeoutError if `exc' is omitted).
+
+    When exc is None, code block is interrupted silently.
+    """
+
+    def __init__(self, seconds, exception=TimeoutError):
+        if exception is None:
+            exception = _SilentException()
+        self.exception = exception
+        if seconds is None:
+            self.timeout = None
+        else:
+            self.timeout = timer(seconds, getcurrent().throw, exception)
+
+    def cancel(self):
+        if self.timeout is not None:
+            self.timeout.cancel()
+
+    def __repr__(self):
+        if self.timeout is not None:
+            return repr(self.timeout)
+        else:
+            return '<fake timeout>'
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, typ, value, tb):
+        self.cancel()
+        if typ is _SilentException and value is self.exception:
+            return True
+
+
+def with_timeout(seconds, func, *args, **kwds):
+    """Wrap a call to some (yielding) function with a timeout; if the called
+    function fails to return before the timeout, cancel it and return a flag
+    value.
+
+    seconds
+      (int or float) seconds before timeout occurs
+    func
+      the callable to execute with a timeout; must be one of the functions
+      that implicitly or explicitly yields
+    \*args, \*\*kwds
+      (positional, keyword) arguments to pass to *func*
+    timeout_value=
+      value to return if timeout occurs (default raise ``TimeoutError``)
+
+    **Returns**:
+
+    Value returned by *func* if *func* returns before *seconds*, else
+    *timeout_value* if provided, else raise ``TimeoutError``
+
+    **Raises**:
+
+    Any exception raised by *func*, and ``TimeoutError`` if *func* times out
+    and no ``timeout_value`` has been provided.
+
+    **Example**::
+
+      data = with_timeout(30, httpc.get, 'http://www.google.com/', timeout_value="")
+
+    Here *data* is either the result of the ``get()`` call, or the empty string if
+    it took too long to return. Any exception raised by the ``get()`` call is
+    passed through to the caller.
+    """
+    # Recognize a specific keyword argument, while also allowing pass-through
+    # of any other keyword arguments accepted by func. Use pop() so we don't
+    # pass timeout_value through to func().
+    has_timeout_value = "timeout_value" in kwds
+    timeout_value = kwds.pop("timeout_value", None)
+    error = TimeoutError()
+    timer = timeout(seconds, error)
+    try:
+        try:
+            return func(*args, **kwds)
+        except TimeoutError, ex:
+            if ex is error and has_timeout_value:
+                return timeout_value
+            raise
+    finally:
+        timer.cancel()
+
+_original_fork = os.fork
+
+def fork():
+    result = _original_fork()
+    core.reinit()
+    return result
+

gevent/backdoor.py

+# @author Bob Ippolito
+# 
+# Copyright (c) 2005-2006, Bob Ippolito
+# Copyright (c) 2007, Linden Research, Inc.
+# Copyright (c) 2008, Donovan Preston
+# 
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+# 
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+import socket
+import sys
+from code import InteractiveConsole
+
+import gevent
+
+try:
+    sys.ps1
+except AttributeError:
+    sys.ps1 = '>>> '
+try:
+    sys.ps2
+except AttributeError:
+    sys.ps2 = '... '
+
+
+class SocketConsole(gevent.Greenlet):
+    def __init__(self, desc, hostport, locals):
+        self.hostport = hostport
+        self.locals = locals
+        # mangle the socket
+        self.desc = desc
+        readline = desc.readline
+        self.old = {}
+        self.fixups = {
+            'softspace': 0,
+            'isatty': lambda: True,
+            'flush': lambda: None,
+            'readline': lambda *a: readline(*a).replace('\r\n', '\n'),
+        }
+        for key, value in self.fixups.iteritems():
+            if hasattr(desc, key):
+                self.old[key] = getattr(desc, key)
+            setattr(desc, key, value)
+
+        gevent.Greenlet.__init__(self)
+
+    def run(self):
+        try:
+            console = InteractiveConsole(self.locals)
+            console.interact()
+        finally:
+            self.switch_out()
+            self.finalize()
+
+    def switch(self, *args, **kw):
+        self.saved = sys.stdin, sys.stderr, sys.stdout
+        sys.stdin = sys.stdout = sys.stderr = self.desc
+        gevent.Greenlet.switch(self, *args, **kw)
+
+    def switch_out(self):
+        sys.stdin, sys.stderr, sys.stdout = self.saved
+
+    def finalize(self):
+        # restore the state of the socket
+        for key in self.fixups:
+            try:
+                value = self.old[key]
+            except KeyError:
+                delattr(self.desc, key)
+            else:
+                setattr(self.desc, key, value)
+        self.fixups.clear()
+        self.old.clear()
+        self.desc = None
+        print "backdoor closed to %s:%s" % self.hostport
+
+
+def backdoor_server(server, locals=None):
+    print "backdoor listening on %s:%s" % server.getsockname()
+    try:
+        try:
+            while True:
+                (conn, (host, port)) = server.accept()
+                print "backdoor connected to %s:%s" % (host, port)
+                fl = conn.makeGreenFile("rw")
+                fl.newlines = '\n'
+                greenlet = SocketConsole(fl, (host, port), locals)
+                gevent.timer(0, greenlet.switch)
+        except socket.error, e:
+            # Broken pipe means it was shutdown
+            if e[0] != 32:
+                raise
+    finally:
+        server.close()
+
+
+def backdoor((conn, addr), locals=None):
+    """ Use this with tcp_server like so:
+        tcp_server(tcp_listener(('127.0.0.1', 9000)), backdoor.backdoor, {})
+    """
+    host, port = addr
+    print "backdoor to %s:%s" % (host, port)
+    fl = conn.makeGreenFile("rw")
+    fl.newlines = '\n'
+    greenlet = SocketConsole(fl, (host, port), locals)
+    gevent.timer(0, greenlet.switch)
+
+
+if __name__ == '__main__':
+    from gevent.socket import tcp_server, tcp_listener
+    tcp_server(tcp_listener(('127.0.0.1', 9000)), backdoor, {})
+
+/* Generated by Cython 0.11.2 on Tue Jul  7 10:47:17 2009 */
+
+#define PY_SSIZE_T_CLEAN
+#include "Python.h"
+#include "structmember.h"
+#ifndef Py_PYTHON_H
+    #error Python headers needed to compile C extensions, please install development version of Python.
+#endif
+#ifndef PY_LONG_LONG
+  #define PY_LONG_LONG LONG_LONG
+#endif
+#ifndef DL_EXPORT
+  #define DL_EXPORT(t) t
+#endif
+#if PY_VERSION_HEX < 0x02040000
+  #define METH_COEXIST 0
+  #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
+#endif
+#if PY_VERSION_HEX < 0x02050000
+  typedef int Py_ssize_t;
+  #define PY_SSIZE_T_MAX INT_MAX
+  #define PY_SSIZE_T_MIN INT_MIN
+  #define PY_FORMAT_SIZE_T ""
+  #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
+  #define PyInt_AsSsize_t(o)   PyInt_AsLong(o)
+  #define PyNumber_Index(o)    PyNumber_Int(o)
+  #define PyIndex_Check(o)     PyNumber_Check(o)
+#endif
+#if PY_VERSION_HEX < 0x02060000
+  #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
+  #define Py_TYPE(ob)   (((PyObject*)(ob))->ob_type)
+  #define Py_SIZE(ob)   (((PyVarObject*)(ob))->ob_size)
+  #define PyVarObject_HEAD_INIT(type, size) \
+          PyObject_HEAD_INIT(type) size,
+  #define PyType_Modified(t)
+
+  typedef struct {
+       void *buf;
+       PyObject *obj;
+       Py_ssize_t len;
+       Py_ssize_t itemsize;
+       int readonly;
+       int ndim;
+       char *format;
+       Py_ssize_t *shape;
+       Py_ssize_t *strides;
+       Py_ssize_t *suboffsets;
+       void *internal;
+  } Py_buffer;
+
+  #define PyBUF_SIMPLE 0
+  #define PyBUF_WRITABLE 0x0001
+  #define PyBUF_FORMAT 0x0004
+  #define PyBUF_ND 0x0008
+  #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
+  #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
+  #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
+  #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
+  #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
+
+#endif
+#if PY_MAJOR_VERSION < 3
+  #define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
+#else
+  #define __Pyx_BUILTIN_MODULE_NAME "builtins"
+#endif
+#if PY_MAJOR_VERSION >= 3
+  #define Py_TPFLAGS_CHECKTYPES 0
+  #define Py_TPFLAGS_HAVE_INDEX 0
+#endif
+#if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)
+  #define Py_TPFLAGS_HAVE_NEWBUFFER 0
+#endif
+#if PY_MAJOR_VERSION >= 3
+  #define PyBaseString_Type            PyUnicode_Type
+  #define PyString_Type                PyBytes_Type
+  #define PyString_CheckExact          PyBytes_CheckExact
+  #define PyInt_Type                   PyLong_Type
+  #define PyInt_Check(op)              PyLong_Check(op)
+  #define PyInt_CheckExact(op)         PyLong_CheckExact(op)
+  #define PyInt_FromString             PyLong_FromString
+  #define PyInt_FromUnicode            PyLong_FromUnicode
+  #define PyInt_FromLong               PyLong_FromLong
+  #define PyInt_FromSize_t             PyLong_FromSize_t
+  #define PyInt_FromSsize_t            PyLong_FromSsize_t
+  #define PyInt_AsLong                 PyLong_AsLong
+  #define PyInt_AS_LONG                PyLong_AS_LONG
+  #define PyInt_AsSsize_t              PyLong_AsSsize_t
+  #define PyInt_AsUnsignedLongMask     PyLong_AsUnsignedLongMask
+  #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
+  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_TrueDivide(x,y)
+#else
+  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_Divide(x,y)
+  #define PyBytes_Type                 PyString_Type
+#endif
+#if PY_MAJOR_VERSION >= 3
+  #define PyMethod_New(func, self, klass) PyInstanceMethod_New(func)
+#endif
+#if !defined(WIN32) && !defined(MS_WINDOWS)
+  #ifndef __stdcall
+    #define __stdcall
+  #endif
+  #ifndef __cdecl
+    #define __cdecl
+  #endif
+  #ifndef __fastcall
+    #define __fastcall
+  #endif
+#else
+  #define _USE_MATH_DEFINES
+#endif
+#if PY_VERSION_HEX < 0x02050000
+  #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),((char *)(n)))
+  #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))
+  #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),((char *)(n)))
+#else
+  #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),(n))
+  #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))
+  #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),(n))
+#endif
+#if PY_VERSION_HEX < 0x02050000
+  #define __Pyx_NAMESTR(n) ((char *)(n))
+  #define __Pyx_DOCSTR(n)  ((char *)(n))
+#else
+  #define __Pyx_NAMESTR(n) (n)
+  #define __Pyx_DOCSTR(n)  (n)
+#endif
+#ifdef __cplusplus
+#define __PYX_EXTERN_C extern "C"
+#else
+#define __PYX_EXTERN_C extern
+#endif
+#include <math.h>
+#define __PYX_HAVE_API__gevent__core
+#include "libevent-internal.h"
+#include "sys/types.h"
+#include "stdio.h"
+#include "event.h"
+#include "libevent.h"
+#define __PYX_USE_C99_COMPLEX defined(_Complex_I)
+
+
+#ifdef __GNUC__
+#define INLINE __inline__
+#elif _WIN32
+#define INLINE __inline
+#else
+#define INLINE 
+#endif
+
+typedef struct {PyObject **p; char *s; long n; char is_unicode; char intern; char is_identifier;} __Pyx_StringTabEntry; /*proto*/
+
+
+
+static int __pyx_skip_dispatch = 0;
+
+
+/* Type Conversion Predeclarations */
+
+#if PY_MAJOR_VERSION < 3
+#define __Pyx_PyBytes_FromString          PyString_FromString
+#define __Pyx_PyBytes_FromStringAndSize   PyString_FromStringAndSize
+#define __Pyx_PyBytes_AsString            PyString_AsString
+#else
+#define __Pyx_PyBytes_FromString          PyBytes_FromString
+#define __Pyx_PyBytes_FromStringAndSize   PyBytes_FromStringAndSize
+#define __Pyx_PyBytes_AsString            PyBytes_AsString
+#endif
+
+#define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))
+static INLINE int __Pyx_PyObject_IsTrue(PyObject*);
+static INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x);
+
+#if !defined(T_PYSSIZET)
+#if PY_VERSION_HEX < 0x02050000
+#define T_PYSSIZET T_INT
+#elif !defined(T_LONGLONG)
+#define T_PYSSIZET \
+        ((sizeof(Py_ssize_t) == sizeof(int))  ? T_INT  : \
+        ((sizeof(Py_ssize_t) == sizeof(long)) ? T_LONG : -1))
+#else
+#define T_PYSSIZET \
+        ((sizeof(Py_ssize_t) == sizeof(int))          ? T_INT      : \
+        ((sizeof(Py_ssize_t) == sizeof(long))         ? T_LONG     : \
+        ((sizeof(Py_ssize_t) == sizeof(PY_LONG_LONG)) ? T_LONGLONG : -1)))
+#endif
+#endif
+
+#if !defined(T_SIZET)
+#if !defined(T_ULONGLONG)
+#define T_SIZET \
+        ((sizeof(size_t) == sizeof(unsigned int))  ? T_UINT  : \
+        ((sizeof(size_t) == sizeof(unsigned long)) ? T_ULONG : -1))
+#else
+#define T_SIZET \
+        ((sizeof(size_t) == sizeof(unsigned int))          ? T_UINT      : \
+        ((sizeof(size_t) == sizeof(unsigned long))         ? T_ULONG     : \
+        ((sizeof(size_t) == sizeof(unsigned PY_LONG_LONG)) ? T_ULONGLONG : -1)))
+#endif
+#endif
+
+static INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);
+static INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);
+static INLINE size_t __Pyx_PyInt_AsSize_t(PyObject*);
+
+#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))
+
+
+#ifdef __GNUC__
+/* Test for GCC > 2.95 */
+#if __GNUC__ > 2 ||               (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)) 
+#define likely(x)   __builtin_expect(!!(x), 1)
+#define unlikely(x) __builtin_expect(!!(x), 0)
+#else /* __GNUC__ > 2 ... */
+#define likely(x)   (x)
+#define unlikely(x) (x)
+#endif /* __GNUC__ > 2 ... */
+#else /* __GNUC__ */
+#define likely(x)   (x)
+#define unlikely(x) (x)
+#endif /* __GNUC__ */
+    
+static PyObject *__pyx_m;
+static PyObject *__pyx_b;
+static PyObject *__pyx_empty_tuple;
+static int __pyx_lineno;
+static int __pyx_clineno = 0;
+static const char * __pyx_cfilenm= __FILE__;
+static const char *__pyx_filename;
+static const char **__pyx_f;
+
+static char __pyx_mdoc[] = "event library\n\nThis module provides a mechanism to execute a function when a\nspecific event on a file handle, file descriptor, or signal occurs,\nor after a given time has passed.\n";
+
+
+#ifdef CYTHON_REFNANNY
+typedef struct {
+  void (*INCREF)(void*, PyObject*, int);
+  void (*DECREF)(void*, PyObject*, int);
+  void (*GOTREF)(void*, PyObject*, int);
+  void (*GIVEREF)(void*, PyObject*, int);
+  void* (*NewContext)(const char*, int, const char*);
+  void (*FinishContext)(void**);
+} __Pyx_RefnannyAPIStruct;
+static __Pyx_RefnannyAPIStruct *__Pyx_Refnanny = NULL;
+#define __Pyx_ImportRefcountAPI(name)   (__Pyx_RefnannyAPIStruct *) PyCObject_Import((char *)name, (char *)"RefnannyAPI")
+#define __Pyx_INCREF(r) __Pyx_Refnanny->INCREF(__pyx_refchk, (PyObject *)(r), __LINE__)
+#define __Pyx_DECREF(r) __Pyx_Refnanny->DECREF(__pyx_refchk, (PyObject *)(r), __LINE__)
+#define __Pyx_GOTREF(r) __Pyx_Refnanny->GOTREF(__pyx_refchk, (PyObject *)(r), __LINE__)
+#define __Pyx_GIVEREF(r) __Pyx_Refnanny->GIVEREF(__pyx_refchk, (PyObject *)(r), __LINE__)
+#define __Pyx_XDECREF(r) if((r) == NULL) ; else __Pyx_DECREF(r)
+#define __Pyx_SetupRefcountContext(name)   void* __pyx_refchk = __Pyx_Refnanny->NewContext((name), __LINE__, __FILE__)
+#define __Pyx_FinishRefcountContext()   __Pyx_Refnanny->FinishContext(&__pyx_refchk)
+#else
+#define __Pyx_INCREF(r) Py_INCREF(r)
+#define __Pyx_DECREF(r) Py_DECREF(r)
+#define __Pyx_GOTREF(r)
+#define __Pyx_GIVEREF(r)
+#define __Pyx_XDECREF(r) Py_XDECREF(r)
+#define __Pyx_SetupRefcountContext(name)
+#define __Pyx_FinishRefcountContext()
+#endif /* CYTHON_REFNANNY */
+#define __Pyx_XGIVEREF(r) if((r) == NULL) ; else __Pyx_GIVEREF(r)
+#define __Pyx_XGOTREF(r) if((r) == NULL) ; else __Pyx_GOTREF(r)
+
+static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
+
+static INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
+static INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
+
+static void __Pyx_RaiseDoubleKeywordsError(
+    const char* func_name, PyObject* kw_name); /*proto*/
+
+static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
+    Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/
+
+static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[],     PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args,     const char* function_name); /*proto*/
+
+static INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
+static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
+
+static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/
+
+static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/
+
+static INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);
+
+static INLINE void __Pyx_RaiseTooManyValuesError(void);
+
+static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/
+static int __Pyx_EndUnpack(PyObject *); /*proto*/
+
+#ifndef __PYX_FORCE_INIT_THREADS
+#if PY_VERSION_HEX < 0x02040200
+#define __PYX_FORCE_INIT_THREADS 1
+#else
+#define __PYX_FORCE_INIT_THREADS 0
+#endif
+#endif
+
+static INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/
+
+static INLINE unsigned char __Pyx_PyInt_AsUnsignedChar(PyObject *);
+
+static INLINE unsigned short __Pyx_PyInt_AsUnsignedShort(PyObject *);
+
+static INLINE unsigned int __Pyx_PyInt_AsUnsignedInt(PyObject *);
+
+static INLINE char __Pyx_PyInt_AsChar(PyObject *);
+
+static INLINE short __Pyx_PyInt_AsShort(PyObject *);
+
+static INLINE int __Pyx_PyInt_AsInt(PyObject *);
+
+static INLINE signed char __Pyx_PyInt_AsSignedChar(PyObject *);
+
+static INLINE signed short __Pyx_PyInt_AsSignedShort(PyObject *);
+
+static INLINE signed int __Pyx_PyInt_AsSignedInt(PyObject *);
+
+static INLINE unsigned long __Pyx_PyInt_AsUnsignedLong(PyObject *);
+
+static INLINE unsigned PY_LONG_LONG __Pyx_PyInt_AsUnsignedLongLong(PyObject *);
+
+static INLINE long __Pyx_PyInt_AsLong(PyObject *);
+
+static INLINE PY_LONG_LONG __Pyx_PyInt_AsLongLong(PyObject *);
+
+static INLINE signed long __Pyx_PyInt_AsSignedLong(PyObject *);
+
+static INLINE signed PY_LONG_LONG __Pyx_PyInt_AsSignedLongLong(PyObject *);
+
+static void __Pyx_WriteUnraisable(const char *name); /*proto*/
+
+static void __Pyx_AddTraceback(const char *funcname); /*proto*/
+
+static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
+
+/* Type declarations */
+
+typedef void (*__pyx_t_6gevent_4core_event_handler)(int, short, void *);
+
+/* "/home/denis/work/gevent/gevent/core.pyx":119
+ * 
+ * 
+ * cdef class event:             # <<<<<<<<<<<<<<
+ *     """event(callback, arg=None, evtype=0, handle=None) -> event object
+ * 
+ */
+
+struct __pyx_obj_6gevent_4core_event {
+  PyObject_HEAD
+  struct event ev;
+  PyObject *_callback;
+  PyObject *_arg;
+};
+
+/* "/home/denis/work/gevent/gevent/core.pyx":202
+ * 
+ * 
+ * cdef class read(event):             # <<<<<<<<<<<<<<
+ * 
+ *     def __init__(self, int handle, callback, timeout=-1, arg=None):
+ */
+
+struct __pyx_obj_6gevent_4core_read {
+  struct __pyx_obj_6gevent_4core_event __pyx_base;
+};
+
+/* "/home/denis/work/gevent/gevent/core.pyx":209
+ * 
+ * 
+ * cdef class write(event):             # <<<<<<<<<<<<<<
+ * 
+ *     def __init__(self, int handle, callback, timeout=-1, arg=None):
+ */
+
+struct __pyx_obj_6gevent_4core_write {
+  struct __pyx_obj_6gevent_4core_event __pyx_base;
+};
+
+/* "/home/denis/work/gevent/gevent/core.pyx":241
+ * 
+ * 
+ * cdef class signal(event):             # <<<<<<<<<<<<<<
+ * 
+ *     def __init__(self, int signalnum, callback, *args, **kwargs):
+ */
+
+struct __pyx_obj_6gevent_4core_signal {
+  struct __pyx_obj_6gevent_4core_event __pyx_base;
+};
+
+/* "/home/denis/work/gevent/gevent/core.pyx":232
+ * 
+ * 
+ * cdef class timer(event):             # <<<<<<<<<<<<<<
+ * 
+ *     def __init__(self, float seconds, callback, *args, **kwargs):
+ */
+
+struct __pyx_obj_6gevent_4core_timer {
+  struct __pyx_obj_6gevent_4core_event __pyx_base;
+};
+/* Module declarations from gevent.core */
+
+static PyTypeObject *__pyx_ptype_6gevent_4core_event = 0;
+static PyTypeObject *__pyx_ptype_6gevent_4core_read = 0;
+static PyTypeObject *__pyx_ptype_6gevent_4core_write = 0;
+static PyTypeObject *__pyx_ptype_6gevent_4core_timer = 0;
+static PyTypeObject *__pyx_ptype_6gevent_4core_signal = 0;
+static PyObject *__pyx_k_8 = 0;
+static void __pyx_f_6gevent_4core___event_handler(int, short, void *); /*proto*/
+static void __pyx_f_6gevent_4core___simple_handler(int, short, void *); /*proto*/
+#define __Pyx_MODULE_NAME "gevent.core"
+int __pyx_module_is_main_gevent__core = 0;
+
+/* Implementation of gevent.core */
+static char __pyx_k_1[] = "Dug Song <dugsong@monkey.org>";
+static char __pyx_k_2[] = "Martin Murray <mmurray@monkey.org>";
+static char __pyx_k_3[] = "Copyright (c) 2004 Dug Song";
+static char __pyx_k_4[] = "Copyright (c) 2003 Martin Murray";
+static char __pyx_k_6[] = "http://monkey.org/~dugsong/pyevent/";
+static char __pyx_k_7[] = "0.4+";
+static PyObject *__pyx_int_0x01;
+static PyObject *__pyx_int_0x02;
+static PyObject *__pyx_int_0x04;
+static PyObject *__pyx_int_0x08;
+static PyObject *__pyx_int_0x10;
+static PyObject *__pyx_int_neg_1;
+static char __pyx_k___main__[] = "__main__";
+static PyObject *__pyx_kp___main__;
+static char __pyx_k___init__[] = "__init__";
+static PyObject *__pyx_kp___init__;
+static char __pyx_k_add[] = "add";
+static PyObject *__pyx_kp_add;
+static char __pyx_k_cancel[] = "cancel";
+static PyObject *__pyx_kp_cancel;
+static char __pyx_k___dealloc__[] = "__dealloc__";
+static PyObject *__pyx_kp___dealloc__;
+static char __pyx_k___repr__[] = "__repr__";
+static PyObject *__pyx_kp___repr__;
+static char __pyx_k_evtype[] = "evtype";
+static PyObject *__pyx_kp_evtype;
+static char __pyx_k_handle[] = "handle";
+static PyObject *__pyx_kp_handle;
+static char __pyx_k_callback[] = "callback";
+static PyObject *__pyx_kp_callback;
+static char __pyx_k_arg[] = "arg";
+static PyObject *__pyx_kp_arg;
+static char __pyx_k_new[] = "new";
+static PyObject *__pyx_kp_new;
+static char __pyx_k_timeout[] = "timeout";
+static PyObject *__pyx_kp_timeout;
+static char __pyx_k_seconds[] = "seconds";
+static PyObject *__pyx_kp_seconds;
+static char __pyx_k_signalnum[] = "signalnum";
+static PyObject *__pyx_kp_signalnum;
+static char __pyx_k_nonblock[] = "nonblock";
+static PyObject *__pyx_kp_nonblock;
+static char __pyx_k___author__[] = "__author__";
+static PyObject *__pyx_kp___author__;
+static char __pyx_k___copyright__[] = "__copyright__";
+static PyObject *__pyx_kp___copyright__;
+static char __pyx_k___license__[] = "__license__";
+static PyObject *__pyx_kp___license__;
+static char __pyx_k_5[] = "BSD";
+static PyObject *__pyx_kp_5;
+static char __pyx_k___url__[] = "__url__";
+static PyObject *__pyx_kp___url__;
+static char __pyx_k___version__[] = "__version__";
+static PyObject *__pyx_kp___version__;
+static char __pyx_k_sys[] = "sys";
+static PyObject *__pyx_kp_sys;
+static char __pyx_k_traceback[] = "traceback";
+static PyObject *__pyx_kp_traceback;
+static char __pyx_k_EV_TIMEOUT[] = "EV_TIMEOUT";
+static PyObject *__pyx_kp_EV_TIMEOUT;
+static char __pyx_k_EV_READ[] = "EV_READ";
+static PyObject *__pyx_kp_EV_READ;
+static char __pyx_k_EV_WRITE[] = "EV_WRITE";
+static PyObject *__pyx_kp_EV_WRITE;
+static char __pyx_k_EV_SIGNAL[] = "EV_SIGNAL";
+static PyObject *__pyx_kp_EV_SIGNAL;
+static char __pyx_k_EV_PERSIST[] = "EV_PERSIST";
+static PyObject *__pyx_kp_EV_PERSIST;
+static char __pyx_k_init[] = "init";
+static PyObject *__pyx_kp_init;
+static char __pyx_k_print_exc[] = "print_exc";
+static PyObject *__pyx_kp_print_exc;
+static char __pyx_k_stderr[] = "stderr";
+static PyObject *__pyx_kp_stderr;
+static char __pyx_k_write[] = "write";
+static PyObject *__pyx_kp_write;
+static char __pyx_k_pending[] = "pending";
+static PyObject *__pyx_kp_pending;
+static char __pyx_k___name__[] = "__name__";
+static PyObject *__pyx_kp___name__;
+static PyObject *__pyx_kp_1;
+static PyObject *__pyx_kp_2;
+static PyObject *__pyx_kp_3;
+static PyObject *__pyx_kp_4;
+static PyObject *__pyx_kp_6;
+static PyObject *__pyx_kp_7;
+static PyObject *__pyx_kp_9;
+static char __pyx_k_9[] = "Failed to execute callback for %r\n";
+static PyObject *__pyx_kp_10;
+static PyObject *__pyx_kp_11;
+static PyObject *__pyx_kp_12;
+static char __pyx_k_10[] = " pending";
+static char __pyx_k_11[] = "";
+static char __pyx_k_12[] = "<%s flags=0x%x, handle=%s, callback=%s, arg=%s%s>";
+static PyObject *__pyx_kp_13;
+static char __pyx_k_13[] = "Failed to execute callback for %r\n";
+static char __pyx_k_14[] = "event_dispatch failed";
+static char __pyx_k_15[] = "event_loop failed";
+
+/* "/home/denis/work/gevent/gevent/core.pyx":104
+ * EV_PERSIST = 0x10
+ * 
+ * cdef void __event_handler(int fd, short evtype, void *arg) with gil:             # <<<<<<<<<<<<<<
+ *     cdef event ev = <event>arg
+ *     try:
+ */
+
+static  void __pyx_f_6gevent_4core___event_handler(int __pyx_v_fd, short __pyx_v_evtype, void *__pyx_v_arg) {
+  struct __pyx_obj_6gevent_4core_event *__pyx_v_ev = 0;
+  PyObject *__pyx_1 = 0;
+  PyObject *__pyx_2 = 0;
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  PyObject *__pyx_t_3 = NULL;
+  PyObject *__pyx_t_4 = NULL;
+  PyObject *__pyx_t_5 = NULL;
+  PyObject *__pyx_t_6 = NULL;
+  short __pyx_t_7;
+  int __pyx_t_8;
+  PyGILState_STATE _save = PyGILState_Ensure();
+  __Pyx_SetupRefcountContext("__event_handler");
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":105
+ * 
+ * cdef void __event_handler(int fd, short evtype, void *arg) with gil:
+ *     cdef event ev = <event>arg             # <<<<<<<<<<<<<<
+ *     try:
+ *         ev._callback(ev, fd, evtype)
+ */
+  __Pyx_INCREF(((PyObject *)((struct __pyx_obj_6gevent_4core_event *)__pyx_v_arg)));
+  __pyx_v_ev = ((struct __pyx_obj_6gevent_4core_event *)__pyx_v_arg);
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":106
+ * cdef void __event_handler(int fd, short evtype, void *arg) with gil:
+ *     cdef event ev = <event>arg
+ *     try:             # <<<<<<<<<<<<<<
+ *         ev._callback(ev, fd, evtype)
+ *     except:
+ */
+  /*try:*/ {
+    {
+      PyObject *__pyx_save_exc_type, *__pyx_save_exc_value, *__pyx_save_exc_tb;
+      __Pyx_ExceptionSave(&__pyx_save_exc_type, &__pyx_save_exc_value, &__pyx_save_exc_tb);
+      __Pyx_XGOTREF(__pyx_save_exc_type);
+      __Pyx_XGOTREF(__pyx_save_exc_value);
+      __Pyx_XGOTREF(__pyx_save_exc_tb);
+      /*try:*/ {
+
+        /* "/home/denis/work/gevent/gevent/core.pyx":107
+ *     cdef event ev = <event>arg
+ *     try:
+ *         ev._callback(ev, fd, evtype)             # <<<<<<<<<<<<<<
+ *     except:
+ *         traceback.print_exc()
+ */
+        __pyx_t_1 = PyInt_FromLong(__pyx_v_fd); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L6_error;}
+        __Pyx_GOTREF(__pyx_t_1);
+        __pyx_t_2 = PyInt_FromLong(__pyx_v_evtype); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L6_error;}
+        __Pyx_GOTREF(__pyx_t_2);
+        __pyx_t_3 = PyTuple_New(3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L6_error;}
+        __Pyx_GOTREF(((PyObject *)__pyx_t_3));
+        __Pyx_INCREF(((PyObject *)__pyx_v_ev));
+        PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_v_ev));
+        __Pyx_GIVEREF(((PyObject *)__pyx_v_ev));
+        PyTuple_SET_ITEM(__pyx_t_3, 1, __pyx_t_1);
+        __Pyx_GIVEREF(__pyx_t_1);
+        PyTuple_SET_ITEM(__pyx_t_3, 2, __pyx_t_2);
+        __Pyx_GIVEREF(__pyx_t_2);
+        __pyx_t_1 = 0;
+        __pyx_t_2 = 0;
+        __pyx_t_2 = PyObject_Call(__pyx_v_ev->_callback, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L6_error;}
+        __Pyx_GOTREF(__pyx_t_2);
+        __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0;
+        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+      }
+      __Pyx_XDECREF(__pyx_save_exc_type); __pyx_save_exc_type = 0;
+      __Pyx_XDECREF(__pyx_save_exc_value); __pyx_save_exc_value = 0;
+      __Pyx_XDECREF(__pyx_save_exc_tb); __pyx_save_exc_tb = 0;
+      goto __pyx_L13_try_end;
+      __pyx_L6_error:;
+      __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0;
+      __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0;
+
+      /* "/home/denis/work/gevent/gevent/core.pyx":108
+ *     try:
+ *         ev._callback(ev, fd, evtype)
+ *     except:             # <<<<<<<<<<<<<<
+ *         traceback.print_exc()
+ *         try:
+ */
+      /*except:*/ {
+        __Pyx_AddTraceback("gevent.core.__event_handler");
+        if (__Pyx_GetException(&__pyx_t_2, &__pyx_t_3, &__pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 108; __pyx_clineno = __LINE__; goto __pyx_L8_except_error;}
+        __Pyx_GOTREF(__pyx_t_2);
+        __Pyx_GOTREF(__pyx_t_3);
+        __Pyx_GOTREF(__pyx_t_1);
+
+        /* "/home/denis/work/gevent/gevent/core.pyx":109
+ *         ev._callback(ev, fd, evtype)
+ *     except:
+ *         traceback.print_exc()             # <<<<<<<<<<<<<<
+ *         try:
+ *             sys.stderr.write('Failed to execute callback for %r\n' % (ev, ))
+ */
+        __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_traceback); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L8_except_error;}
+        __Pyx_GOTREF(__pyx_1);
+        __pyx_t_4 = PyObject_GetAttr(__pyx_1, __pyx_kp_print_exc); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L8_except_error;}
+        __Pyx_GOTREF(__pyx_t_4);
+        __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+        __pyx_t_5 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 109; __pyx_clineno = __LINE__; goto __pyx_L8_except_error;}
+        __Pyx_GOTREF(__pyx_t_5);
+        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+        __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+
+        /* "/home/denis/work/gevent/gevent/core.pyx":110
+ *     except:
+ *         traceback.print_exc()
+ *         try:             # <<<<<<<<<<<<<<
+ *             sys.stderr.write('Failed to execute callback for %r\n' % (ev, ))
+ *         except:
+ */
+        {
+          PyObject *__pyx_save_exc_type, *__pyx_save_exc_value, *__pyx_save_exc_tb;
+          __Pyx_ExceptionSave(&__pyx_save_exc_type, &__pyx_save_exc_value, &__pyx_save_exc_tb);
+          __Pyx_XGOTREF(__pyx_save_exc_type);
+          __Pyx_XGOTREF(__pyx_save_exc_value);
+          __Pyx_XGOTREF(__pyx_save_exc_tb);
+          /*try:*/ {
+
+            /* "/home/denis/work/gevent/gevent/core.pyx":111
+ *         traceback.print_exc()
+ *         try:
+ *             sys.stderr.write('Failed to execute callback for %r\n' % (ev, ))             # <<<<<<<<<<<<<<
+ *         except:
+ *             pass
+ */
+            __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_sys); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L16_error;}
+            __Pyx_GOTREF(__pyx_1);
+            __pyx_t_5 = PyObject_GetAttr(__pyx_1, __pyx_kp_stderr); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L16_error;}
+            __Pyx_GOTREF(__pyx_t_5);
+            __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+            __pyx_t_4 = PyObject_GetAttr(__pyx_t_5, __pyx_kp_write); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L16_error;}
+            __Pyx_GOTREF(__pyx_t_4);
+            __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;
+            __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L16_error;}
+            __Pyx_GOTREF(((PyObject *)__pyx_t_5));
+            __Pyx_INCREF(((PyObject *)__pyx_v_ev));
+            PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_v_ev));
+            __Pyx_GIVEREF(((PyObject *)__pyx_v_ev));
+            __pyx_t_6 = PyNumber_Remainder(__pyx_kp_9, ((PyObject *)__pyx_t_5)); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L16_error;}
+            __Pyx_GOTREF(__pyx_t_6);
+            __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
+            __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L16_error;}
+            __Pyx_GOTREF(((PyObject *)__pyx_t_5));
+            PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_6);
+            __Pyx_GIVEREF(__pyx_t_6);
+            __pyx_t_6 = 0;
+            __pyx_t_6 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_t_5), NULL); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L16_error;}
+            __Pyx_GOTREF(__pyx_t_6);
+            __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;
+            __Pyx_DECREF(((PyObject *)__pyx_t_5)); __pyx_t_5 = 0;
+            __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;
+          }
+          __Pyx_XDECREF(__pyx_save_exc_type); __pyx_save_exc_type = 0;
+          __Pyx_XDECREF(__pyx_save_exc_value); __pyx_save_exc_value = 0;
+          __Pyx_XDECREF(__pyx_save_exc_tb); __pyx_save_exc_tb = 0;
+          goto __pyx_L23_try_end;
+          __pyx_L16_error:;
+          __Pyx_XDECREF(__pyx_1); __pyx_1 = 0;
+          __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
+          __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0;
+          __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0;
+
+          /* "/home/denis/work/gevent/gevent/core.pyx":112
+ *         try:
+ *             sys.stderr.write('Failed to execute callback for %r\n' % (ev, ))
+ *         except:             # <<<<<<<<<<<<<<
+ *             pass
+ *     finally:
+ */
+          /*except:*/ {
+            PyErr_Restore(0,0,0);
+            goto __pyx_L17_exception_handled;
+          }
+          __pyx_L17_exception_handled:;
+          __Pyx_XGIVEREF(__pyx_save_exc_type);
+          __Pyx_XGIVEREF(__pyx_save_exc_value);
+          __Pyx_XGIVEREF(__pyx_save_exc_tb);
+          __Pyx_ExceptionReset(__pyx_save_exc_type, __pyx_save_exc_value, __pyx_save_exc_tb);
+          __pyx_L23_try_end:;
+        }
+        __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+        __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+        goto __pyx_L7_exception_handled;
+      }
+      __pyx_L8_except_error:;
+      __Pyx_XDECREF(__pyx_save_exc_type);
+      __Pyx_XDECREF(__pyx_save_exc_value);
+      __Pyx_XDECREF(__pyx_save_exc_tb);
+      goto __pyx_L4;
+      __pyx_L7_exception_handled:;
+      __Pyx_XGIVEREF(__pyx_save_exc_type);
+      __Pyx_XGIVEREF(__pyx_save_exc_value);
+      __Pyx_XGIVEREF(__pyx_save_exc_tb);
+      __Pyx_ExceptionReset(__pyx_save_exc_type, __pyx_save_exc_value, __pyx_save_exc_tb);
+      __pyx_L13_try_end:;
+    }
+  }
+  /*finally:*/ {
+    int __pyx_why;
+    PyObject *__pyx_exc_type, *__pyx_exc_value, *__pyx_exc_tb;
+    int __pyx_exc_lineno;
+    __pyx_exc_type = 0; __pyx_exc_value = 0; __pyx_exc_tb = 0; __pyx_exc_lineno = 0;
+    __pyx_why = 0; goto __pyx_L5;
+    __pyx_L4: {
+      __pyx_why = 4;
+      __Pyx_XDECREF(__pyx_1); __pyx_1 = 0;
+      __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
+      __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0;
+      __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0;
+      __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0;
+      __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
+      __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0;
+      __Pyx_ErrFetch(&__pyx_exc_type, &__pyx_exc_value, &__pyx_exc_tb);
+      __pyx_exc_lineno = __pyx_lineno;
+      goto __pyx_L5;
+    }
+    __pyx_L5:;
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":115
+ *             pass
+ *     finally:
+ *         if not event_pending(&ev.ev, EV_READ|EV_WRITE|EV_SIGNAL|EV_TIMEOUT, NULL):             # <<<<<<<<<<<<<<
+ *             Py_DECREF(ev)
+ * 
+ */
+    __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_READ); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L24_error;}
+    __Pyx_GOTREF(__pyx_1);
+    __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_WRITE); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L24_error;}
+    __Pyx_GOTREF(__pyx_2);
+    __pyx_t_1 = PyNumber_Or(__pyx_1, __pyx_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L24_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+    __Pyx_DECREF(__pyx_2); __pyx_2 = 0;
+    __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_SIGNAL); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L24_error;}
+    __Pyx_GOTREF(__pyx_1);
+    __pyx_t_3 = PyNumber_Or(__pyx_t_1, __pyx_1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L24_error;}
+    __Pyx_GOTREF(__pyx_t_3);
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+    __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_TIMEOUT); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L24_error;}
+    __Pyx_GOTREF(__pyx_2);
+    __pyx_t_1 = PyNumber_Or(__pyx_t_3, __pyx_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L24_error;}
+    __Pyx_GOTREF(__pyx_t_1);
+    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
+    __Pyx_DECREF(__pyx_2); __pyx_2 = 0;
+    __pyx_t_7 = __Pyx_PyInt_AsShort(__pyx_t_1); if (unlikely((__pyx_t_7 == (short)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L24_error;}
+    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+    __pyx_t_8 = (!event_pending((&__pyx_v_ev->ev), __pyx_t_7, NULL));
+    if (__pyx_t_8) {
+
+      /* "/home/denis/work/gevent/gevent/core.pyx":116
+ *     finally:
+ *         if not event_pending(&ev.ev, EV_READ|EV_WRITE|EV_SIGNAL|EV_TIMEOUT, NULL):
+ *             Py_DECREF(ev)             # <<<<<<<<<<<<<<
+ * 
+ * 
+ */
+      Py_DECREF(((PyObject *)__pyx_v_ev));
+      goto __pyx_L25;
+    }
+    __pyx_L25:;
+    goto __pyx_L26;
+    __pyx_L24_error:;
+    if (__pyx_why == 4) {
+      Py_XDECREF(__pyx_exc_type);
+      Py_XDECREF(__pyx_exc_value);
+      Py_XDECREF(__pyx_exc_tb);
+    }
+    goto __pyx_L1_error;
+    __pyx_L26:;
+    switch (__pyx_why) {
+      case 4: {
+        __Pyx_ErrRestore(__pyx_exc_type, __pyx_exc_value, __pyx_exc_tb);
+        __pyx_lineno = __pyx_exc_lineno;
+        __pyx_exc_type = 0;
+        __pyx_exc_value = 0;
+        __pyx_exc_tb = 0;
+        goto __pyx_L1_error;
+      }
+    }
+  }
+
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_1);
+  __Pyx_XDECREF(__pyx_2);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_XDECREF(__pyx_t_3);
+  __Pyx_XDECREF(__pyx_t_4);
+  __Pyx_XDECREF(__pyx_t_5);
+  __Pyx_XDECREF(__pyx_t_6);
+  __Pyx_WriteUnraisable("gevent.core.__event_handler");
+  __pyx_L0:;
+  __Pyx_XDECREF((PyObject *)__pyx_v_ev);
+  __Pyx_FinishRefcountContext();
+  PyGILState_Release(_save);
+}
+
+/* "/home/denis/work/gevent/gevent/core.pyx":135
+ *     cdef object _callback, _arg
+ * 
+ *     def __init__(self, short evtype, int handle, callback, arg=None):             # <<<<<<<<<<<<<<
+ *         self._callback = callback
+ *         self._arg = arg
+ */
+
+static int __pyx_pf_6gevent_4core_5event___init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static int __pyx_pf_6gevent_4core_5event___init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  short __pyx_v_evtype;
+  int __pyx_v_handle;
+  PyObject *__pyx_v_callback = 0;
+  PyObject *__pyx_v_arg = 0;
+  void *__pyx_v_c_self;
+  int __pyx_r;
+  int __pyx_t_1;
+  static PyObject **__pyx_pyargnames[] = {&__pyx_kp_evtype,&__pyx_kp_handle,&__pyx_kp_callback,&__pyx_kp_arg,0};
+  __Pyx_SetupRefcountContext("__init__");
+  if (unlikely(__pyx_kwds)) {
+    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
+    PyObject* values[4] = {0,0,0,0};
+    values[3] = Py_None;
+    switch (PyTuple_GET_SIZE(__pyx_args)) {
+      case  4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3);
+      case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);
+      case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
+      case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+      case  0: break;
+      default: goto __pyx_L5_argtuple_error;
+    }
+    switch (PyTuple_GET_SIZE(__pyx_args)) {
+      case  0:
+      values[0] = PyDict_GetItem(__pyx_kwds, __pyx_kp_evtype);
+      if (likely(values[0])) kw_args--;
+      else goto __pyx_L5_argtuple_error;
+      case  1:
+      values[1] = PyDict_GetItem(__pyx_kwds, __pyx_kp_handle);
+      if (likely(values[1])) kw_args--;
+      else {
+        __Pyx_RaiseArgtupleInvalid("__init__", 0, 3, 4, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 135; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+      case  2:
+      values[2] = PyDict_GetItem(__pyx_kwds, __pyx_kp_callback);
+      if (likely(values[2])) kw_args--;
+      else {
+        __Pyx_RaiseArgtupleInvalid("__init__", 0, 3, 4, 2); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 135; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      }
+      case  3:
+      if (kw_args > 1) {
+        PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_kp_arg);
+        if (unlikely(value)) { values[3] = value; kw_args--; }
+      }
+    }
+    if (unlikely(kw_args > 0)) {
+      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 135; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    }
+    __pyx_v_evtype = __Pyx_PyInt_AsShort(values[0]); if (unlikely((__pyx_v_evtype == (short)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 135; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_handle = __Pyx_PyInt_AsInt(values[1]); if (unlikely((__pyx_v_handle == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 135; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    __pyx_v_callback = values[2];
+    __pyx_v_arg = values[3];
+  } else {
+    __pyx_v_arg = Py_None;
+    switch (PyTuple_GET_SIZE(__pyx_args)) {
+      case  4:
+      __pyx_v_arg = PyTuple_GET_ITEM(__pyx_args, 3);
+      case  3:
+      __pyx_v_callback = PyTuple_GET_ITEM(__pyx_args, 2);
+      __pyx_v_handle = __Pyx_PyInt_AsInt(PyTuple_GET_ITEM(__pyx_args, 1)); if (unlikely((__pyx_v_handle == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 135; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      __pyx_v_evtype = __Pyx_PyInt_AsShort(PyTuple_GET_ITEM(__pyx_args, 0)); if (unlikely((__pyx_v_evtype == (short)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 135; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+      break;
+      default: goto __pyx_L5_argtuple_error;
+    }
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("__init__", 0, 3, 4, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 135; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("gevent.core.event.__init__");
+  return -1;
+  __pyx_L4_argument_unpacking_done:;
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":136
+ * 
+ *     def __init__(self, short evtype, int handle, callback, arg=None):
+ *         self._callback = callback             # <<<<<<<<<<<<<<
+ *         self._arg = arg
+ *         cdef void* c_self = <void*>self
+ */
+  __Pyx_INCREF(__pyx_v_callback);
+  __Pyx_GIVEREF(__pyx_v_callback);
+  __Pyx_GOTREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_callback);
+  __Pyx_DECREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_callback);
+  ((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_callback = __pyx_v_callback;
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":137
+ *     def __init__(self, short evtype, int handle, callback, arg=None):
+ *         self._callback = callback
+ *         self._arg = arg             # <<<<<<<<<<<<<<
+ *         cdef void* c_self = <void*>self
+ *         if evtype == 0 and not handle:
+ */
+  __Pyx_INCREF(__pyx_v_arg);
+  __Pyx_GIVEREF(__pyx_v_arg);
+  __Pyx_GOTREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_arg);
+  __Pyx_DECREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_arg);
+  ((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_arg = __pyx_v_arg;
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":138
+ *         self._callback = callback
+ *         self._arg = arg
+ *         cdef void* c_self = <void*>self             # <<<<<<<<<<<<<<
+ *         if evtype == 0 and not handle:
+ *             evtimer_set(&self.ev, __event_handler, c_self)
+ */
+  __pyx_v_c_self = ((void *)__pyx_v_self);
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":139
+ *         self._arg = arg
+ *         cdef void* c_self = <void*>self
+ *         if evtype == 0 and not handle:             # <<<<<<<<<<<<<<
+ *             evtimer_set(&self.ev, __event_handler, c_self)
+ *         else:
+ */
+  if ((__pyx_v_evtype == 0)) {
+    __pyx_t_1 = (!__pyx_v_handle);
+  } else {
+    __pyx_t_1 = (__pyx_v_evtype == 0);
+  }
+  if (__pyx_t_1) {
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":140
+ *         cdef void* c_self = <void*>self
+ *         if evtype == 0 and not handle:
+ *             evtimer_set(&self.ev, __event_handler, c_self)             # <<<<<<<<<<<<<<
+ *         else:
+ *             event_set(&self.ev, handle, evtype, __event_handler, c_self)
+ */
+    evtimer_set((&((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->ev), __pyx_f_6gevent_4core___event_handler, __pyx_v_c_self);
+    goto __pyx_L6;
+  }
+  /*else*/ {
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":142
+ *             evtimer_set(&self.ev, __event_handler, c_self)
+ *         else:
+ *             event_set(&self.ev, handle, evtype, __event_handler, c_self)             # <<<<<<<<<<<<<<
+ * 
+ *     property callback:
+ */
+    event_set((&((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->ev), __pyx_v_handle, __pyx_v_evtype, __pyx_f_6gevent_4core___event_handler, __pyx_v_c_self);
+  }
+  __pyx_L6:;
+
+  __pyx_r = 0;
+  __Pyx_FinishRefcountContext();
+  return __pyx_r;
+}
+
+/* "/home/denis/work/gevent/gevent/core.pyx":146
+ *     property callback:
+ * 
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._callback
+ * 
+ */
+
+static PyObject *__pyx_pf_6gevent_4core_5event_8callback___get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pf_6gevent_4core_5event_8callback___get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_SetupRefcountContext("__get__");
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":147
+ * 
+ *         def __get__(self):
+ *             return self._callback             # <<<<<<<<<<<<<<
+ * 
+ *         def __set__(self, new):
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __Pyx_INCREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_callback);
+  __pyx_r = ((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_callback;
+  goto __pyx_L0;
+
+  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_FinishRefcountContext();
+  return __pyx_r;
+}
+
+/* "/home/denis/work/gevent/gevent/core.pyx":149
+ *             return self._callback
+ * 
+ *         def __set__(self, new):             # <<<<<<<<<<<<<<
+ *             self._callback = new
+ * 
+ */
+
+static int __pyx_pf_6gevent_4core_5event_8callback___set__(PyObject *__pyx_v_self, PyObject *__pyx_v_new); /*proto*/
+static int __pyx_pf_6gevent_4core_5event_8callback___set__(PyObject *__pyx_v_self, PyObject *__pyx_v_new) {
+  int __pyx_r;
+  __Pyx_SetupRefcountContext("__set__");
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":150
+ * 
+ *         def __set__(self, new):
+ *             self._callback = new             # <<<<<<<<<<<<<<
+ * 
+ *     property arg:
+ */
+  __Pyx_INCREF(__pyx_v_new);
+  __Pyx_GIVEREF(__pyx_v_new);
+  __Pyx_GOTREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_callback);
+  __Pyx_DECREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_callback);
+  ((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_callback = __pyx_v_new;
+
+  __pyx_r = 0;
+  __Pyx_FinishRefcountContext();
+  return __pyx_r;
+}
+
+/* "/home/denis/work/gevent/gevent/core.pyx":154
+ *     property arg:
+ * 
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return self._arg
+ * 
+ */
+
+static PyObject *__pyx_pf_6gevent_4core_5event_3arg___get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pf_6gevent_4core_5event_3arg___get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  __Pyx_SetupRefcountContext("__get__");
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":155
+ * 
+ *         def __get__(self):
+ *             return self._arg             # <<<<<<<<<<<<<<
+ * 
+ *         def __set__(self, new):
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __Pyx_INCREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_arg);
+  __pyx_r = ((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_arg;
+  goto __pyx_L0;
+
+  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_FinishRefcountContext();
+  return __pyx_r;
+}
+
+/* "/home/denis/work/gevent/gevent/core.pyx":157
+ *             return self._arg
+ * 
+ *         def __set__(self, new):             # <<<<<<<<<<<<<<
+ *             self._arg = new
+ * 
+ */
+
+static int __pyx_pf_6gevent_4core_5event_3arg___set__(PyObject *__pyx_v_self, PyObject *__pyx_v_new); /*proto*/
+static int __pyx_pf_6gevent_4core_5event_3arg___set__(PyObject *__pyx_v_self, PyObject *__pyx_v_new) {
+  int __pyx_r;
+  __Pyx_SetupRefcountContext("__set__");
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":158
+ * 
+ *         def __set__(self, new):
+ *             self._arg = new             # <<<<<<<<<<<<<<
+ * 
+ *     property pending:
+ */
+  __Pyx_INCREF(__pyx_v_new);
+  __Pyx_GIVEREF(__pyx_v_new);
+  __Pyx_GOTREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_arg);
+  __Pyx_DECREF(((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_arg);
+  ((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->_arg = __pyx_v_new;
+
+  __pyx_r = 0;
+  __Pyx_FinishRefcountContext();
+  return __pyx_r;
+}
+
+/* "/home/denis/work/gevent/gevent/core.pyx":162
+ *     property pending:
+ * 
+ *         def __get__(self):             # <<<<<<<<<<<<<<
+ *             return event_pending(&self.ev, EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE, NULL)
+ * 
+ */
+
+static PyObject *__pyx_pf_6gevent_4core_5event_7pending___get__(PyObject *__pyx_v_self); /*proto*/
+static PyObject *__pyx_pf_6gevent_4core_5event_7pending___get__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_r = NULL;
+  PyObject *__pyx_1 = 0;
+  PyObject *__pyx_2 = 0;
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  short __pyx_t_3;
+  __Pyx_SetupRefcountContext("__get__");
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":163
+ * 
+ *         def __get__(self):
+ *             return event_pending(&self.ev, EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE, NULL)             # <<<<<<<<<<<<<<
+ * 
+ *     def add(self, timeout=-1):
+ */
+  __Pyx_XDECREF(__pyx_r);
+  __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_TIMEOUT); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_1);
+  __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_SIGNAL); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_2);
+  __pyx_t_1 = PyNumber_Or(__pyx_1, __pyx_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+  __Pyx_DECREF(__pyx_2); __pyx_2 = 0;
+  __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_READ); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_1);
+  __pyx_t_2 = PyNumber_Or(__pyx_t_1, __pyx_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+  __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_WRITE); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_2);
+  __pyx_t_1 = PyNumber_Or(__pyx_t_2, __pyx_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __Pyx_DECREF(__pyx_2); __pyx_2 = 0;
+  __pyx_t_3 = __Pyx_PyInt_AsShort(__pyx_t_1); if (unlikely((__pyx_t_3 == (short)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_1 = PyInt_FromLong(event_pending((&((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->ev), __pyx_t_3, NULL)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 163; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_r = __pyx_t_1;
+  __pyx_t_1 = 0;
+  goto __pyx_L0;
+
+  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_1);
+  __Pyx_XDECREF(__pyx_2);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("gevent.core.event.pending.__get__");
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_FinishRefcountContext();
+  return __pyx_r;
+}
+
+/* "/home/denis/work/gevent/gevent/core.pyx":165
+ *             return event_pending(&self.ev, EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE, NULL)
+ * 
+ *     def add(self, timeout=-1):             # <<<<<<<<<<<<<<
+ *         """Add event to be executed after an optional timeout.
+ * 
+ */
+
+static PyObject *__pyx_pf_6gevent_4core_5event_add(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
+static char __pyx_doc_6gevent_4core_5event_add[] = "Add event to be executed after an optional timeout.\n\n        Arguments:\n\n        timeout -- seconds after which the event will be executed\n        ";
+static PyObject *__pyx_pf_6gevent_4core_5event_add(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
+  PyObject *__pyx_v_timeout = 0;
+  struct timeval __pyx_v_tv;
+  double __pyx_v_c_timeout;
+  PyObject *__pyx_r = NULL;
+  PyObject *__pyx_1 = 0;
+  PyObject *__pyx_2 = 0;
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  short __pyx_t_3;
+  int __pyx_t_4;
+  double __pyx_t_5;
+  static PyObject **__pyx_pyargnames[] = {&__pyx_kp_timeout,0};
+  __Pyx_SetupRefcountContext("add");
+  if (unlikely(__pyx_kwds)) {
+    Py_ssize_t kw_args = PyDict_Size(__pyx_kwds);
+    PyObject* values[1] = {0};
+    values[0] = __pyx_int_neg_1;
+    switch (PyTuple_GET_SIZE(__pyx_args)) {
+      case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
+      case  0: break;
+      default: goto __pyx_L5_argtuple_error;
+    }
+    switch (PyTuple_GET_SIZE(__pyx_args)) {
+      case  0:
+      if (kw_args > 1) {
+        PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_kp_timeout);
+        if (unlikely(value)) { values[0] = value; kw_args--; }
+      }
+    }
+    if (unlikely(kw_args > 0)) {
+      if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, PyTuple_GET_SIZE(__pyx_args), "add") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+    }
+    __pyx_v_timeout = values[0];
+  } else {
+    __pyx_v_timeout = __pyx_int_neg_1;
+    switch (PyTuple_GET_SIZE(__pyx_args)) {
+      case  1: __pyx_v_timeout = PyTuple_GET_ITEM(__pyx_args, 0);
+      case  0: break;
+      default: goto __pyx_L5_argtuple_error;
+    }
+  }
+  goto __pyx_L4_argument_unpacking_done;
+  __pyx_L5_argtuple_error:;
+  __Pyx_RaiseArgtupleInvalid("add", 0, 0, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
+  __pyx_L3_error:;
+  __Pyx_AddTraceback("gevent.core.event.add");
+  return NULL;
+  __pyx_L4_argument_unpacking_done:;
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":174
+ *         cdef timeval tv
+ *         cdef double c_timeout
+ *         if not event_pending(&self.ev, EV_READ|EV_WRITE|EV_SIGNAL|EV_TIMEOUT, NULL):             # <<<<<<<<<<<<<<
+ *             Py_INCREF(self)
+ *         if timeout >= 0.0:
+ */
+  __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_READ); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_1);
+  __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_WRITE); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_2);
+  __pyx_t_1 = PyNumber_Or(__pyx_1, __pyx_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+  __Pyx_DECREF(__pyx_2); __pyx_2 = 0;
+  __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_SIGNAL); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_1);
+  __pyx_t_2 = PyNumber_Or(__pyx_t_1, __pyx_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+  __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_TIMEOUT); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_2);
+  __pyx_t_1 = PyNumber_Or(__pyx_t_2, __pyx_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __Pyx_DECREF(__pyx_2); __pyx_2 = 0;
+  __pyx_t_3 = __Pyx_PyInt_AsShort(__pyx_t_1); if (unlikely((__pyx_t_3 == (short)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 174; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_4 = (!event_pending((&((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->ev), __pyx_t_3, NULL));
+  if (__pyx_t_4) {
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":175
+ *         cdef double c_timeout
+ *         if not event_pending(&self.ev, EV_READ|EV_WRITE|EV_SIGNAL|EV_TIMEOUT, NULL):
+ *             Py_INCREF(self)             # <<<<<<<<<<<<<<
+ *         if timeout >= 0.0:
+ *             c_timeout = <double>timeout
+ */
+    Py_INCREF(__pyx_v_self);
+    goto __pyx_L6;
+  }
+  __pyx_L6:;
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":176
+ *         if not event_pending(&self.ev, EV_READ|EV_WRITE|EV_SIGNAL|EV_TIMEOUT, NULL):
+ *             Py_INCREF(self)
+ *         if timeout >= 0.0:             # <<<<<<<<<<<<<<
+ *             c_timeout = <double>timeout
+ *             tv.tv_sec = <long>c_timeout
+ */
+  __pyx_t_1 = PyFloat_FromDouble(0.0); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = PyObject_RichCompare(__pyx_v_timeout, __pyx_t_1, Py_GE); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_4 = __Pyx_PyObject_IsTrue(__pyx_t_2); if (unlikely(__pyx_t_4 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 176; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  if (__pyx_t_4) {
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":177
+ *             Py_INCREF(self)
+ *         if timeout >= 0.0:
+ *             c_timeout = <double>timeout             # <<<<<<<<<<<<<<
+ *             tv.tv_sec = <long>c_timeout
+ *             tv.tv_usec = <unsigned int>((c_timeout - <double>tv.tv_sec) * 1000000.0)
+ */
+    __pyx_t_5 = __pyx_PyFloat_AsDouble(__pyx_v_timeout); if (unlikely(PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 177; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+    __pyx_v_c_timeout = ((double)__pyx_t_5);
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":178
+ *         if timeout >= 0.0:
+ *             c_timeout = <double>timeout
+ *             tv.tv_sec = <long>c_timeout             # <<<<<<<<<<<<<<
+ *             tv.tv_usec = <unsigned int>((c_timeout - <double>tv.tv_sec) * 1000000.0)
+ *             event_add(&self.ev, &tv)
+ */
+    __pyx_v_tv.tv_sec = ((long)__pyx_v_c_timeout);
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":179
+ *             c_timeout = <double>timeout
+ *             tv.tv_sec = <long>c_timeout
+ *             tv.tv_usec = <unsigned int>((c_timeout - <double>tv.tv_sec) * 1000000.0)             # <<<<<<<<<<<<<<
+ *             event_add(&self.ev, &tv)
+ *         else:
+ */
+    __pyx_v_tv.tv_usec = ((unsigned int)((__pyx_v_c_timeout - ((double)__pyx_v_tv.tv_sec)) * 1000000.0));
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":180
+ *             tv.tv_sec = <long>c_timeout
+ *             tv.tv_usec = <unsigned int>((c_timeout - <double>tv.tv_sec) * 1000000.0)
+ *             event_add(&self.ev, &tv)             # <<<<<<<<<<<<<<
+ *         else:
+ *             event_add(&self.ev, NULL)
+ */
+    event_add((&((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->ev), (&__pyx_v_tv));
+    goto __pyx_L7;
+  }
+  /*else*/ {
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":182
+ *             event_add(&self.ev, &tv)
+ *         else:
+ *             event_add(&self.ev, NULL)             # <<<<<<<<<<<<<<
+ * 
+ *     def cancel(self):
+ */
+    event_add((&((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->ev), NULL);
+  }
+  __pyx_L7:;
+
+  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_1);
+  __Pyx_XDECREF(__pyx_2);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("gevent.core.event.add");
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_FinishRefcountContext();
+  return __pyx_r;
+}
+
+/* "/home/denis/work/gevent/gevent/core.pyx":184
+ *             event_add(&self.ev, NULL)
+ * 
+ *     def cancel(self):             # <<<<<<<<<<<<<<
+ *         """Remove event from the event queue."""
+ *         if event_pending(&self.ev, EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE, NULL):
+ */
+
+static PyObject *__pyx_pf_6gevent_4core_5event_cancel(PyObject *__pyx_v_self, PyObject *unused); /*proto*/
+static char __pyx_doc_6gevent_4core_5event_cancel[] = "Remove event from the event queue.";
+static PyObject *__pyx_pf_6gevent_4core_5event_cancel(PyObject *__pyx_v_self, PyObject *unused) {
+  PyObject *__pyx_r = NULL;
+  PyObject *__pyx_1 = 0;
+  PyObject *__pyx_2 = 0;
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  short __pyx_t_3;
+  int __pyx_t_4;
+  __Pyx_SetupRefcountContext("cancel");
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":186
+ *     def cancel(self):
+ *         """Remove event from the event queue."""
+ *         if event_pending(&self.ev, EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE, NULL):             # <<<<<<<<<<<<<<
+ *             event_del(&self.ev)
+ *             Py_DECREF(self)
+ */
+  __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_TIMEOUT); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_1);
+  __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_SIGNAL); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_2);
+  __pyx_t_1 = PyNumber_Or(__pyx_1, __pyx_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+  __Pyx_DECREF(__pyx_2); __pyx_2 = 0;
+  __pyx_1 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_READ); if (unlikely(!__pyx_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_1);
+  __pyx_t_2 = PyNumber_Or(__pyx_t_1, __pyx_1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_1); __pyx_1 = 0;
+  __pyx_2 = __Pyx_GetName(__pyx_m, __pyx_kp_EV_WRITE); if (unlikely(!__pyx_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_2);
+  __pyx_t_1 = PyNumber_Or(__pyx_t_2, __pyx_2); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+  __Pyx_DECREF(__pyx_2); __pyx_2 = 0;
+  __pyx_t_3 = __Pyx_PyInt_AsShort(__pyx_t_1); if (unlikely((__pyx_t_3 == (short)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 186; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __pyx_t_4 = event_pending((&((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->ev), __pyx_t_3, NULL);
+  if (__pyx_t_4) {
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":187
+ *         """Remove event from the event queue."""
+ *         if event_pending(&self.ev, EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE, NULL):
+ *             event_del(&self.ev)             # <<<<<<<<<<<<<<
+ *             Py_DECREF(self)
+ * 
+ */
+    event_del((&((struct __pyx_obj_6gevent_4core_event *)__pyx_v_self)->ev));
+
+    /* "/home/denis/work/gevent/gevent/core.pyx":188
+ *         if event_pending(&self.ev, EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE, NULL):
+ *             event_del(&self.ev)
+ *             Py_DECREF(self)             # <<<<<<<<<<<<<<
+ * 
+ *     def __dealloc__(self):
+ */
+    Py_DECREF(__pyx_v_self);
+    goto __pyx_L5;
+  }
+  __pyx_L5:;
+
+  __pyx_r = Py_None; __Pyx_INCREF(Py_None);
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_1);
+  __Pyx_XDECREF(__pyx_2);
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("gevent.core.event.cancel");
+  __pyx_r = NULL;
+  __pyx_L0:;
+  __Pyx_XGIVEREF(__pyx_r);
+  __Pyx_FinishRefcountContext();
+  return __pyx_r;
+}
+
+/* "/home/denis/work/gevent/gevent/core.pyx":190
+ *             Py_DECREF(self)
+ * 
+ *     def __dealloc__(self):             # <<<<<<<<<<<<<<
+ *         self.cancel()
+ * 
+ */
+
+static void __pyx_pf_6gevent_4core_5event___dealloc__(PyObject *__pyx_v_self); /*proto*/
+static void __pyx_pf_6gevent_4core_5event___dealloc__(PyObject *__pyx_v_self) {
+  PyObject *__pyx_t_1 = NULL;
+  PyObject *__pyx_t_2 = NULL;
+  __Pyx_SetupRefcountContext("__dealloc__");
+
+  /* "/home/denis/work/gevent/gevent/core.pyx":191
+ * 
+ *     def __dealloc__(self):
+ *         self.cancel()             # <<<<<<<<<<<<<<
+ * 
+ *     def __repr__(self):
+ */
+  __pyx_t_1 = PyObject_GetAttr(__pyx_v_self, __pyx_kp_cancel); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_1);
+  __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 191; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
+  __Pyx_GOTREF(__pyx_t_2);
+  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
+  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
+
+  goto __pyx_L0;
+  __pyx_L1_error:;
+  __Pyx_XDECREF(__pyx_t_1);
+  __Pyx_XDECREF(__pyx_t_2);
+  __Pyx_AddTraceback("gevent.core.event.__dealloc__");
+  __pyx_L0:;
+  __Pyx_FinishRefcountContext();
+}