Commit 76aefde4 authored by Denis Bilenko's avatar Denis Bilenko

use cython_ifdef.py script - add preprocessor to core_.pyx; add Makefile to...

use cython_ifdef.py script - add preprocessor to core_.pyx; add Makefile to run cython-related things.
parent cc945708
all: gevent/gevent.core.c gevent/gevent.ares.c
gevent/core.pyx: gevent/core_.pyx
m4 -P gevent/core_.pyx > core.pyx && mv core.pyx gevent/
gevent/gevent.core.c: gevent/core.pyx gevent/libev.pxd
util/cython_ifdef.py -o gevent.core.c gevent/core.pyx
echo >> gevent.core.c
echo '#include "callbacks.c"' >> gevent.core.c
mv gevent.core.* gevent/
gevent/gevent.ares.c: gevent/*.pyx gevent/*.pxd
cython -o gevent.ares.c gevent/ares.pyx
mv gevent.ares.* gevent/
clean:
rm -f gevent.core.c gevent.core.h core.pyx gevent/gevent.core.c gevent/gevent.core.h gevent/core.pyx
rm -f gevent.ares.c gevent.ares.h gevent/gevent.ares.c gevent/gevent.ares.h
......@@ -28,9 +28,6 @@ cdef extern from "callbacks.h":
cdef extern from *:
int FD_SETSIZE
cdef void IFDEF_WINDOWS "#if defined(_WIN32) //" ()
cdef void IFDEF_EV_STANDALONE "#if defined(EV_STANDALONE) //" ()
cdef void ENDIF "#endif //" ()
int ioctlsocket(int, int, unsigned long*)
int FIONREAD
......@@ -221,14 +218,16 @@ cdef public class loop [object PyGeventLoopObject, type PyGeventLoop_Type]:
cdef libev.ev_loop* _ptr
cdef public object error_handler
cdef libev.ev_prepare _signal_checker
#ifdef _WIN32
cdef libev.ev_timer _periodic_signal_checker
#endif
def __init__(self, object flags=None, object default=True, size_t ptr=0):
cdef unsigned int c_flags
libev.ev_prepare_init(&self._signal_checker, <void*>gevent_signal_check)
IFDEF_WINDOWS()
#ifdef _WIN32
libev.ev_timer_init(&self._periodic_signal_checker, <void*>gevent_periodic_signal_check, 0.3, 0.3)
ENDIF()
#endif
if ptr:
self._ptr = <libev.ev_loop*>ptr
else:
......@@ -240,10 +239,10 @@ cdef public class loop [object PyGeventLoopObject, type PyGeventLoop_Type]:
raise SystemError("ev_default_loop(%s) failed" % (c_flags, ))
libev.ev_prepare_start(self._ptr, &self._signal_checker)
libev.ev_unref(self._ptr)
IFDEF_WINDOWS()
#ifdef _WIN32
libev.ev_timer_start(self._ptr, &self._periodic_signal_checker)
libev.ev_unref(self._ptr)
ENDIF()
#endif
else:
self._ptr = libev.ev_loop_new(c_flags)
if not self._ptr:
......@@ -253,11 +252,11 @@ cdef public class loop [object PyGeventLoopObject, type PyGeventLoop_Type]:
if libev.ev_is_active(&self._signal_checker):
libev.ev_ref(self._ptr)
libev.ev_prepare_stop(self._ptr, &self._signal_checker)
IFDEF_WINDOWS()
#ifdef _WIN32
if libev.ev_is_active(&self._periodic_signal_checker):
libev.ev_ref(self._ptr)
libev.ev_timer_stop(self._ptr, &self._periodic_signal_checker)
ENDIF()
#endif
def destroy(self):
if self._ptr:
......@@ -376,14 +375,12 @@ cdef public class loop [object PyGeventLoopObject, type PyGeventLoop_Type]:
return value
return backend
#ifdef EV_STANDALONE
property activecnt:
def __get__(self):
res = None
IFDEF_EV_STANDALONE()
res = self._ptr.activecnt
ENDIF()
return res
return self._ptr.activecnt
#endif
def io(self, int fd, int events):
return io(self, fd, events)
......
......@@ -45,6 +45,9 @@ ARES = Extension(name='gevent.ares',
ARES.optional = True
ext_modules = [CORE, ARES]
if os.path.exists('libev'):
CORE.define_macros += [('EV_STANDALONE', '1'),
('EV_COMMON', ''), # we don't use void* data
......@@ -110,70 +113,15 @@ if ares_embed:
ARES.define_macros += [('CARES_EMBED', '')]
def need_update(destination, *source):
if not os.path.exists(destination):
sys.stderr.write('Creating %s\n' % destination)
return True
dest_mtime = os.stat(destination).st_mtime
source = source + ('setup.py', )
for pattern in source:
for filename in glob(pattern):
if os.stat(filename).st_mtime - dest_mtime > 1:
sys.stderr.write('Updating %s (changed: %s)\n' % (destination, filename))
return True
def system(command):
sys.stderr.write(command + '\n')
return os.system(command)
def replace_in_file(filename, old, new, check=True):
olddata = open(filename).read()
newdata = olddata.replace(old, new)
if check:
assert olddata != newdata, 'replacement in %s failed' % filename
open(filename, 'w').write(newdata)
def run_cython_core(cython_command):
if need_update('gevent/core.pyx', 'gevent/core_.pyx'):
system('m4 -P gevent/core_.pyx > core.pyx && mv core.pyx gevent/')
if need_update('gevent/gevent.core.c', 'gevent/core.p*x*', 'gevent/libev.pxd'):
if 0 == system('%s gevent/core.pyx -o gevent.core.c && mv gevent.core.* gevent/' % (cython_command, )):
replace_in_file('gevent/gevent.core.c', '\n\n#endif /* Py_PYTHON_H */', '\n#include "callbacks.c"\n#endif /* Py_PYTHON_H */')
short_path = 'gevent/'
full_path = join(os.getcwd(), short_path)
replace_in_file('gevent/gevent.core.c', short_path, full_path, check=False)
replace_in_file('gevent/gevent.core.h', short_path, full_path, check=False)
if need_update('gevent/gevent.core.c', 'gevent/callbacks.*', 'gevent/libev*.h', 'libev/*.*'):
os.system('touch gevent/gevent.core.c')
def run_cython_ares(cython_command):
if need_update('gevent/gevent.ares.c', 'gevent/ares.pyx'):
system('%s gevent/ares.pyx -o gevent.ares.c && mv gevent.ares.* gevent/' % cython_command)
if need_update('gevent/gevent.ares.c', 'gevent/dnshelper.c', 'gevent/inet_ntop.c', 'c-ares/*.*'):
os.system('touch gevent/gevent.ares.c')
CORE.run_cython = run_cython_core
ARES.run_cython = run_cython_ares
class my_build_ext(build_ext):
user_options = (build_ext.user_options
+ [("cython=", None, "path to the cython executable")])
def initialize_options(self):
build_ext.initialize_options(self)
self.cython = "cython"
make_run = []
def gevent_prepare(self, ext):
if self.cython:
run_cython = getattr(ext, 'run_cython', None)
if run_cython:
run_cython(self.cython)
if not self.make_run:
if os.system('make'):
sys.exit(1)
self.make_run.append(1)
configure = getattr(ext, 'configure', None)
if configure:
configure()
......@@ -226,23 +174,6 @@ def read(name, *args):
return ''
ext_modules = [CORE, ARES]
warnings = []
def warn(message):
message += '\n'
sys.stderr.write(message)
warnings.append(message)
ARES.disabled_why = None
for filename in ARES.sources:
if not os.path.exists(filename):
ARES.disabled_why = '%s does not exist' % filename
ext_modules.remove(ARES)
break
def run_setup(ext_modules):
setup(
name='gevent',
......@@ -272,17 +203,12 @@ def run_setup(ext_modules):
if __name__ == '__main__':
if sys.argv[1:] == ['cython']:
CORE.run_cython('cython')
ARES.run_cython('cython')
else:
try:
run_setup(ext_modules)
except BuildFailed:
if ARES not in ext_modules:
raise
ext_modules.remove(ARES)
ARES.disabled_why = 'failed to build'
run_setup(ext_modules)
if ARES.disabled_why:
sys.stderr.write('\nWARNING: The gevent.ares extension has been disabled because %s.\n' % ARES.disabled_why)
try:
run_setup(ext_modules)
except BuildFailed:
if ARES not in ext_modules:
raise
ext_modules.remove(ARES)
run_setup(ext_modules)
if ARES not in ext_modules:
sys.stderr.write('\nWARNING: The gevent.ares extension has been disabled.\n')
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