Skip to content

Commit 6121c0f

Browse files
committed
Merge pull request #51 from akatrevorjay/feature/fix-pypi-build-setuptools
Work around bug in setuptools with setup_requires, also include pyx/pyd files to cythonize
2 parents 913608a + 98276af commit 6121c0f

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
include tools/*
22
include include/*
33
include pyrox/VERSION
4+
include pyrox/http/*.pyx
5+
include pyrox/http/*.pxd

pyrox/http/parser.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ cdef extern from "http_el.h":
2424
http_data_cb on_body
2525
http_cb on_message_complete
2626

27+
28+
cdef extern from "http_el.c":
2729
void http_parser_init(http_parser *parser, http_parser_type ptype)
2830
void free_http_parser(http_parser *parser)
2931

pyrox/http/parser.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ from libc.stdlib cimport malloc, free
33

44
from cpython cimport bool, PyBytes_FromStringAndSize, PyBytes_FromString
55

6+
from parser cimport http_parser_type, http_parser, http_parser_settings, http_parser_init, free_http_parser, http_parser_exec, http_should_keep_alive, http_transfer_encoding_chunked
7+
68
import traceback
79

810
_REQUEST_PARSER = 0

setup.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,36 @@
33
import sys
44
import pyrox.about
55

6-
from setuptools import setup, find_packages
7-
from distutils.extension import Extension
6+
from setuptools import setup, find_packages, Extension
87

9-
try:
10-
from Cython.Build import cythonize
11-
has_cython = True
12-
except ImportError:
13-
has_cython = False
8+
9+
# force setuptools not to convert .pyx to .c in the Extension
10+
import setuptools.extension
11+
assert callable(setuptools.extension.have_pyrex), "Requires setuptools 0.6.26 or later"
12+
setuptools.extension.have_pyrex = lambda: True
13+
14+
# https://bitbucket.org/pypa/setuptools/issues/288/cannot-specify-cython-under-setup_requires
15+
class LateResolvedCommandLookup(dict):
16+
"""
17+
A dictionary of distutils commands with overrides to be resolved late.
18+
Late-resolved commands should be implemented as callable methods on
19+
the class.
20+
21+
This class allows 'build_ext' to be resolve after setup_requires resolves
22+
dependencies such as Cython.
23+
"""
24+
def __getitem__(self, name):
25+
if getattr(self, name, None):
26+
return getattr(self, name)()
27+
return super(LateResolvedCommandLookup, self).__getitem__(name)
28+
29+
def __contains__(self, name):
30+
return hasattr(self, name) or (
31+
super(LateResolvedCommandLookup, self).__contains__(name))
32+
33+
def build_ext(self):
34+
Cython = __import__('Cython.Distutils.build_ext')
35+
return Cython.Distutils.build_ext
1436

1537

1638
def read(relative):
@@ -21,17 +43,21 @@ def read(relative):
2143
def compile_pyx():
2244
ext_modules = list()
2345

24-
cparser = cythonize('pyrox/http/parser.pyx')[0]
25-
cparser.sources.insert(0, 'include/http_el.c')
26-
ext_modules.append(cparser)
46+
e = Extension('pyrox.http.parser',
47+
sources=['pyrox/http/parser.pyx'],
48+
include_dirs = ['include/'])
49+
ext_modules.append(e)
2750

28-
ext_modules.extend(cythonize('pyrox/http/model_util.pyx'))
51+
e = Extension('pyrox.http.model_util',
52+
sources=['pyrox/http/model_util.pyx'],
53+
include_dirs = ['include/'])
54+
ext_modules.append(e)
2955

3056
return ext_modules
3157

3258

3359
# compiler flags
34-
CFLAGS = ['-I', './include']
60+
CFLAGS = []
3561
DEBUG = os.getenv('DEBUG')
3662

3763
if DEBUG and DEBUG.lower() == 'true':
@@ -63,6 +89,7 @@ def compile_pyx():
6389
'Topic :: Utilities'
6490
],
6591
scripts=['scripts/pyrox'],
92+
setup_requires=read('tools/setup_requires.txt'),
6693
tests_require=read('tools/tests_require.txt'),
6794
install_requires=read('tools/install_requires.txt'),
6895
test_suite='nose.collector',
@@ -72,4 +99,6 @@ def compile_pyx():
7299
},
73100
include_package_data=True,
74101
packages=find_packages(exclude=['*.tests']),
75-
ext_modules=compile_pyx())
102+
ext_modules=compile_pyx(),
103+
cmdclass=LateResolvedCommandLookup(),
104+
)

tools/setup_requires.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cython

0 commit comments

Comments
 (0)