|
5 | 5 | # the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
|
6 | 6 |
|
7 | 7 |
|
8 |
| -import setuptools |
| 8 | +import os |
| 9 | +import os.path |
| 10 | +import re |
9 | 11 | import sys
|
10 | 12 |
|
| 13 | +import setuptools |
| 14 | +from setuptools.command import build_ext as _build_ext |
| 15 | + |
11 | 16 |
|
12 | 17 | if sys.version_info < (3, 5):
|
13 | 18 | raise RuntimeError('asyncpg requires Python 3.5 or greater')
|
14 | 19 |
|
15 | 20 |
|
| 21 | +CFLAGS = ['-O2'] |
| 22 | +LDFLAGS = [] |
| 23 | + |
| 24 | + |
| 25 | +class build_ext(_build_ext.build_ext): |
| 26 | + user_options = _build_ext.build_ext.user_options + [ |
| 27 | + ('cython-always', None, |
| 28 | + 'run cythonize() even if .c files are present'), |
| 29 | + ('cython-annotate', None, |
| 30 | + 'Produce a colorized HTML version of the Cython source.'), |
| 31 | + ] |
| 32 | + |
| 33 | + def initialize_options(self): |
| 34 | + super(build_ext, self).initialize_options() |
| 35 | + self.cython_always = False |
| 36 | + self.cython_annotate = None |
| 37 | + |
| 38 | + def finalize_options(self): |
| 39 | + need_cythonize = self.cython_always |
| 40 | + cfiles = {} |
| 41 | + |
| 42 | + for extension in self.distribution.ext_modules: |
| 43 | + for i, sfile in enumerate(extension.sources): |
| 44 | + if sfile.endswith('.pyx'): |
| 45 | + prefix, ext = os.path.splitext(sfile) |
| 46 | + cfile = prefix + '.c' |
| 47 | + |
| 48 | + if os.path.exists(cfile) and not self.cython_always: |
| 49 | + extension.sources[i] = cfile |
| 50 | + else: |
| 51 | + if os.path.exists(cfile): |
| 52 | + cfiles[cfile] = os.path.getmtime(cfile) |
| 53 | + else: |
| 54 | + cfiles[cfile] = 0 |
| 55 | + need_cythonize = True |
| 56 | + |
| 57 | + if need_cythonize: |
| 58 | + try: |
| 59 | + import Cython |
| 60 | + except ImportError: |
| 61 | + raise RuntimeError( |
| 62 | + 'please install Cython to compile asyncpg from source') |
| 63 | + |
| 64 | + if Cython.__version__ < '0.24': |
| 65 | + raise RuntimeError( |
| 66 | + 'asyncpg requires Cython version 0.24 or greater') |
| 67 | + |
| 68 | + from Cython.Build import cythonize |
| 69 | + |
| 70 | + directives = {} |
| 71 | + if self.cython_directives: |
| 72 | + for directive in self.cython_directives.split(','): |
| 73 | + k, _, v = directive.partition('=') |
| 74 | + if v.lower() == 'false': |
| 75 | + v = False |
| 76 | + if v.lower() == 'true': |
| 77 | + v = True |
| 78 | + |
| 79 | + directives[k] = v |
| 80 | + |
| 81 | + self.distribution.ext_modules[:] = cythonize( |
| 82 | + self.distribution.ext_modules, |
| 83 | + compiler_directives=directives, |
| 84 | + annotate=self.cython_annotate) |
| 85 | + |
| 86 | + for cfile, timestamp in cfiles.items(): |
| 87 | + if os.path.getmtime(cfile) != timestamp: |
| 88 | + # The file was recompiled, patch |
| 89 | + self._patch_cfile(cfile) |
| 90 | + |
| 91 | + super(build_ext, self).finalize_options() |
| 92 | + |
| 93 | + def _patch_cfile(self, cfile): |
| 94 | + # Script to patch Cython 'async def' coroutines to have a 'tp_iter' |
| 95 | + # slot, which makes them compatible with 'yield from' without the |
| 96 | + # `asyncio.coroutine` decorator. |
| 97 | + |
| 98 | + with open(cfile, 'rt') as f: |
| 99 | + src = f.read() |
| 100 | + |
| 101 | + src = re.sub( |
| 102 | + r''' |
| 103 | + \s* offsetof\(__pyx_CoroutineObject,\s*gi_weakreflist\), |
| 104 | + \s* 0, |
| 105 | + \s* 0, |
| 106 | + \s* __pyx_Coroutine_methods, |
| 107 | + \s* __pyx_Coroutine_memberlist, |
| 108 | + \s* __pyx_Coroutine_getsets, |
| 109 | + ''', |
| 110 | + |
| 111 | + r''' |
| 112 | + offsetof(__pyx_CoroutineObject, gi_weakreflist), |
| 113 | + __Pyx_Coroutine_await, /* tp_iter */ |
| 114 | + 0, |
| 115 | + __pyx_Coroutine_methods, |
| 116 | + __pyx_Coroutine_memberlist, |
| 117 | + __pyx_Coroutine_getsets, |
| 118 | + ''', |
| 119 | + |
| 120 | + src, flags=re.X) |
| 121 | + |
| 122 | + # Fix a segfault in Cython. |
| 123 | + src = re.sub( |
| 124 | + r''' |
| 125 | + \s* __Pyx_Coroutine_get_qualname\(__pyx_CoroutineObject\s+\*self\) |
| 126 | + \s* { |
| 127 | + \s* Py_INCREF\(self->gi_qualname\); |
| 128 | + ''', |
| 129 | + |
| 130 | + r''' |
| 131 | + __Pyx_Coroutine_get_qualname(__pyx_CoroutineObject *self) |
| 132 | + { |
| 133 | + if (self->gi_qualname == NULL) { return __pyx_empty_unicode; } |
| 134 | + Py_INCREF(self->gi_qualname); |
| 135 | + ''', |
| 136 | + |
| 137 | + src, flags=re.X) |
| 138 | + |
| 139 | + src = re.sub( |
| 140 | + r''' |
| 141 | + \s* __Pyx_Coroutine_get_name\(__pyx_CoroutineObject\s+\*self\) |
| 142 | + \s* { |
| 143 | + \s* Py_INCREF\(self->gi_name\); |
| 144 | + ''', |
| 145 | + |
| 146 | + r''' |
| 147 | + __Pyx_Coroutine_get_name(__pyx_CoroutineObject *self) |
| 148 | + { |
| 149 | + if (self->gi_name == NULL) { return __pyx_empty_unicode; } |
| 150 | + Py_INCREF(self->gi_name); |
| 151 | + ''', |
| 152 | + |
| 153 | + src, flags=re.X) |
| 154 | + |
| 155 | + with open(cfile, 'wt') as f: |
| 156 | + f.write(src) |
| 157 | + |
| 158 | + |
16 | 159 | setuptools.setup(
|
17 | 160 | name='asyncpg',
|
18 | 161 | version='0.5.4',
|
|
38 | 181 | setuptools.Extension(
|
39 | 182 | "asyncpg.protocol.protocol",
|
40 | 183 | ["asyncpg/protocol/record/recordobj.c",
|
41 |
| - "asyncpg/protocol/protocol.c"], |
42 |
| - extra_compile_args=['-O2']) |
43 |
| - ] |
| 184 | + "asyncpg/protocol/protocol.pyx"], |
| 185 | + extra_compile_args=CFLAGS, |
| 186 | + extra_link_args=LDFLAGS) |
| 187 | + ], |
| 188 | + cmdclass={'build_ext': build_ext}, |
44 | 189 | )
|
0 commit comments