Skip to content

Commit

Permalink
Merge pull request #14 from eriknw/cython_optional
Browse files Browse the repository at this point in the history
Make Cython optional.  Use *.c files when deployed.
  • Loading branch information
eriknw committed Apr 24, 2014
2 parents 79e01b8 + cc98bea commit ce7fd63
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 24 deletions.
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ python:
- "3.2"
- "3.3"

env:
- WITH_CYTHON=true
- WITH_CYTHON=false

before_install:
- pip install git+https://github.com/pytoolz/toolz.git
- pip install cython

install:
- python setup.py build_ext --inplace
# *.c files aren't in the repo, so we must always create them
- python setup.py build_ext --inplace --with-cython
- if [[ $WITH_CYTHON == 'false' ]]; then
- rm cytoolz/*.so
- python setup.py build_ext --inplace --without-cython
- fi

# commands to run tests
script:
Expand Down
2 changes: 1 addition & 1 deletion cytoolz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# Aliases
comp = compose

__version__ = '0.0.1' # In alpha. Should eventually match `toolz.__version__`
from ._version import __version__
1 change: 1 addition & 0 deletions cytoolz/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.5.3dev'
3 changes: 3 additions & 0 deletions requirements_devel.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cython
nose
-e git+https://github.com/pytoolz/toolz.git
91 changes: 69 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,71 @@
""" Build `cytoolz` with or without Cython.
Deployed versions of CyToolz do not rely on Cython by default even if the
user has Cython installed. A C compiler is used to compile the distributed
*.c files instead.
Pass "--cython" or "--with-cython" as a command line argument to setup.py
to build the project using Cython.
Pass "--no-cython" or "--without-cython" to disable usage of Cython.
For convenience, developmental versions (with 'dev' in the version number)
automatically use Cython unless disabled via a command line argument.
"""
import os.path
import sys
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [
Extension("cytoolz.itertoolz",
["cytoolz/itertoolz.pyx"]),
Extension("cytoolz.functoolz",
["cytoolz/functoolz.pyx"]),
Extension("cytoolz.dicttoolz",
["cytoolz/dicttoolz.pyx"]),
Extension("cytoolz.recipes",
["cytoolz/recipes.pyx"]),
Extension("cytoolz.curried_exceptions",
["cytoolz/curried_exceptions.pyx"]),
]

setup(
name="cytoolz",
cmdclass={"build_ext": build_ext},
ext_modules=ext_modules,
packages=['cytoolz'],
package_data={'cytoolz': ['*.pxd']},
)

info = {}
filename = os.path.join('cytoolz', '_version.py')
exec(compile(open(filename, "rb").read(), filename, 'exec'), info)
VERSION = info['__version__']

try:
from Cython.Distutils import build_ext
has_cython = True
except ImportError:
has_cython = False

is_dev = 'dev' in VERSION
use_cython = is_dev or '--cython' in sys.argv or '--with-cython' in sys.argv
if '--no-cython' in sys.argv:
use_cython = False
sys.argv.remove('--no-cython')
if '--without-cython' in sys.argv:
use_cython = False
sys.argv.remove('--without-cython')
if '--cython' in sys.argv:
sys.argv.remove('--cython')
if '--with-cython' in sys.argv:
sys.argv.remove('--with-cython')

if use_cython and not has_cython:
if is_dev:
raise RuntimeError('Cython required to build dev version of cytoolz.')
print('WARNING: Cython not installed. Building without Cython.')
use_cython = False

if use_cython:
suffix = '.pyx'
cmdclass = {'build_ext': build_ext}
else:
suffix = '.c'
cmdclass = {}

ext_modules = []
for modname in ['dicttoolz', 'functoolz', 'itertoolz',
'curried_exceptions', 'recipes']:
ext_modules.append(Extension('cytoolz.' + modname,
['cytoolz/' + modname + suffix]))

if __name__ == '__main__':
setup(
name='cytoolz',
cmdclass=cmdclass,
ext_modules=ext_modules,
packages=['cytoolz'],
package_data={'cytoolz': ['*.pxd']},
)

0 comments on commit ce7fd63

Please sign in to comment.