8000 Declare Numpy as a setup dependency by mdboom · Pull Request #2445 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Declare Numpy as a setup dependency #2445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
else:
del sdist.sdist.make_release_tree

from distutils.dist import Distribution

import setupext
from setupext import print_line, print_raw, print_message, print_status

Expand Down Expand Up @@ -126,6 +128,7 @@
package_data = {}
package_dir = {'': 'lib'}
install_requires = []
setup_requires = []
default_backend = None


Expand Down Expand Up @@ -188,6 +191,7 @@
package_data.setdefault(key, [])
package_data[key] = list(set(val + package_data[key]))
install_requires.extend(package.get_install_requires())
setup_requires.extend(package.get_setup_requires())

# Write the default matplotlibrc file
if default_backend is None:
Expand All @@ -213,6 +217,19 @@
# versions of distribute don't support it.
extra_args['use_2to3'] = True

# Finalize the extension modules so they can get the Numpy include
# dirs
for mod in ext_modules:
mod.finalize()


# Avoid installing setup_requires dependencies if the user just
# queries for information
if (any('--' + opt in sys.argv for opt in
Distribution.display_option_names + ['help']) or
'clean' in sys.argv):
setup_requires = []


# Finally, pass this all along to distutils to do the heavy lifting.
distrib = setup(name="matplotlib",
Expand Down Expand Up @@ -241,6 +258,7 @@

# List third-party Python packages that we require
install_requires=install_requires,
setup_requires=setup_requires,

# matplotlib has C/C++ extensions, so it's not zip safe.
# Telling setuptools this prevents it from doing an automatic
Expand Down
109 changes: 94 additions & 15 deletions setupext.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def make_extension(name, files, *args, **kwargs):
Any additional arguments are passed to the
`distutils.core.Extension` constructor.
"""
ext = Extension(name, files, *args, **kwargs)
ext = DelayedExtension(name, files, *args, **kwargs)
for dir in get_base_dirs():
include_dir = os.path.join(dir, 'include')
if os.path.exists(include_dir):
Expand Down Expand Up @@ -390,6 +390,14 @@ def get_install_requires(self):
"""
return []

def get_setup_requires(self):
"""
Get a list of Python packages that we require at build time.
pip/easy_install will attempt to download and install this
package if it is not installed.
"""
return []

def _check_for_pkg_config(self, package, include_file, min_version=None,
version=None):
"""
Expand Down Expand Up @@ -647,42 +655,113 @@ def get_install_requires(self):
return ['nose']


class DelayedExtension(Extension, object):
"""
A distutils Extension subclass where some of its members
may have delayed computation until reaching the build phase.

This is so we can, for example, get the Numpy include dirs
after pip has installed Numpy for us if it wasn't already
on the system.
"""
def __init__(self, *args, **kwargs):
super(DelayedExtension, self).__init__(*args, **kwargs)
self._finalized = False
self._hooks = {}

def add_hook(self, member, func):
"""
Add a hook to dynamically compute a member.

Parameters
----------
member : string
The name of the member

func : callable
The function to call to get dynamically-computed values
for the member.
"""
self._hooks[member] = func

def finalize(self):
self._finalized = True

class DelayedMember(property):
def __init__(self, name):
self._name = name

def __get__(self, obj, objtype=None):
result = getattr(obj, '_' + self._name, [])

if obj._finalized:
if self._name in obj._hooks:
result = obj._hooks[self._name]() + result

return result

def __set__(self, obj, value):
setattr(obj, '_' + self._name, value)

include_dirs = DelayedMember('include_dirs')


class Numpy(SetupPackage):
name = "numpy"

@staticmethod
def include_dirs_hook():
if sys.version_info[0] >= 3:
import builtins
if hasattr(builtins, '__NUMPY_SETUP__'):
del builtins.__NUMPY_SETUP__
import imp
import numpy
imp.reload(numpy)
else:
import __builtin__
if hasattr(__builtin__, '__NUMPY_SETUP__'):
del __builtin__.__NUMPY_SETUP__
import numpy
reload(numpy)

ext = Extension('test', [])
ext.include_dirs.append(numpy.get_include())
if not has_include_file(
ext.include_dirs, os.path.join("numpy", "arrayobject.h")):
warnings.warn(
"The C headers for numpy could not be found. "
"You may need to install the development package")

return [numpy.get_include()]

def check(self):
min_version = extract_versions()['__version__numpy__']
try:
import numpy
except ImportError:
raise SystemExit(
"Requires numpy %s or later to build. (Numpy not found)" %
min_version)
return 'not found. pip may install it below.'

if not is_min_version(numpy.__version__, min_version):
raise SystemExit(
"Requires numpy %s or later to build. (Found %s)" %
(min_version, numpy.__version__))

ext = make_extension('test', [])
ext.include_dirs.append(numpy.get_include())
if not has_include_file(
ext.include_dirs, os.path.join("numpy", "arrayobject.h")):
raise CheckFailed(
"The C headers for numpy could not be found. You"
"may need to install the development package.")

return 'version %s' % numpy.__version__

def add_flags(self, ext):
import numpy

# Ensure that PY_ARRAY_UNIQUE_SYMBOL is uniquely defined for
# each extension
array_api_name = 'MPL_' + ext.name.replace('.', '_') + '_ARRAY_API'

ext.include_dirs.append(numpy.get_include())
ext.define_macros.append(('PY_ARRAY_UNIQUE_SYMBOL', array_api_name))
ext.add_hook('include_dirs', self.include_dirs_hook)

def get_setup_requires(self):
return ['numpy>=1.5']

def get_install_requires(self):
return ['numpy>=1.5']


class CXX(SetupPackage):
Expand Down
24 changes: 24 additions & 0 deletions unit/test_clean_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Tests that matplotlib can install into a completely clean virtual
# environment.

set -e
cd ..
rm -rf build
rm -rf numpy*
rm -rf python.tmp
python unit/virtualenv.py python.tmp
python.tmp/bin/python setup.py install
python.tmp/bin/python -c "import matplotlib"
rm -rf python.tmp

set -e
cd ..
rm -rf build
rm -rf numpy*
rm -rf python.tmp
python3 unit/virtualenv.py python.tmp
python.tmp/bin/python3 setup.py install
python.tmp/bin/python3 -c "import matplotlib"
rm -rf python.tmp
Loading
0