Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
BLD: Defer numpy/Cython operations in setup.py.
Browse files Browse the repository at this point in the history
Use a lazy cmdclass object to defer accesses to `numpy` and `Cython`
until after those modules have been installed by `setuptools` via
`setup_requires`.

Without this, the fact that we're supplying numpy and Cython in
`setup_requires` is meaningless, because we access numpy at module
scope, well before we're able to tell `setuptools` that we require
numpy.
  • Loading branch information
ssanderson committed Nov 23, 2015
1 parent c056c31 commit 3aab4e2
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions setup.py
Expand Up @@ -22,8 +22,55 @@
import re

from setuptools import setup, Extension, find_packages
from pkg_resources import resource_filename


class LazyCommandClass(dict):
"""
Lazy command class that defers operations requiring Cython and numpy until
they've actually been downloaded and installed by setup_requires.
"""
def __contains__(self, key):
return (
key == 'build_ext'
or super(LazyCommandClass, self).__contains__(key)
)

def __setitem__(self, key, value):
if key == 'build_ext':
raise AssertionError("build_ext overridden!")
super(LazyCommandClass, self).__setitem__(key, value)

def __getitem__(self, key):
if key != 'build_ext':
return super(LazyCommandClass, self).__getitem__(key)

from Cython.Distutils import build_ext as cython_build_ext

class build_ext(cython_build_ext):
"""
Custom build_ext command that lazily adds numpy's include_dir to
extensions.
"""
def build_extensions(self):
"""
Lazily append numpy's include directory to Extension includes.
This is done here rather than at module scope because setup.py
may be run before numpy has been installed, in which case
importing numpy and calling `numpy.get_include()` will fail.
"""
numpy_incl = resource_filename('numpy', 'core/include')
for ext in self.extensions:
ext.include_dirs.append(numpy_incl)

# This explicitly calls the superclass method rather than the
# usual super() invocation because distutils' build_class, of
# which Cython's build_ext is a subclass, is an old-style class
# in Python 2, which doesn't support `super`.
cython_build_ext.build_extensions(self)
return build_ext

import numpy

# Global variables
CFLAGS = os.environ.get('CFLAGS', '').split()
Expand All @@ -32,7 +79,7 @@
BLOSC_DIR = os.environ.get('BLOSC_DIR', '')

# Sources & libraries
inc_dirs = ['bcolz', numpy.get_include()]
inc_dirs = ['bcolz']
lib_dirs = []
libs = []
def_macros = []
Expand Down Expand Up @@ -160,5 +207,6 @@
test=tests_require
),
packages=find_packages(),
package_data={'bcolz': ['carray_ext.pxd']}
package_data={'bcolz': ['carray_ext.pxd']},
cmdclass=LazyCommandClass(),
)

0 comments on commit 3aab4e2

Please sign in to comment.