diff --git a/extension_helpers/__init__.py b/extension_helpers/__init__.py index 7736220..5ccfe0c 100644 --- a/extension_helpers/__init__.py +++ b/extension_helpers/__init__.py @@ -1,5 +1,4 @@ -from ._distutils_helpers import get_compiler from ._openmp_helpers import add_openmp_flags_if_available -from ._setup_helpers import get_extensions, pkg_config +from ._setup_helpers import get_compiler, get_extensions, pkg_config from ._utils import import_file, write_if_different from .version import version as __version__ diff --git a/extension_helpers/_distutils_helpers.py b/extension_helpers/_distutils_helpers.py deleted file mode 100644 index f6250c2..0000000 --- a/extension_helpers/_distutils_helpers.py +++ /dev/null @@ -1,59 +0,0 @@ -""" -This module contains various utilities for introspecting the distutils -module and the setup process. - -Some of these utilities require the -`extension_helpers.setup_helpers.register_commands` function to be called first, -as it will affect introspection of setuptools command-line arguments. Other -utilities in this module do not have that restriction. -""" - -import os -import sys - -from setuptools.command.build_ext import new_compiler -from setuptools.dist import Distribution -from setuptools.errors import DistutilsError - -from ._utils import silence - -__all__ = ['get_compiler'] - - -def get_dummy_distribution(): - """ - Returns a distutils Distribution object used to instrument the setup - environment before calling the actual setup() function. - """ - - # Pre-parse the Distutils command-line options and config files to if - # the option is set. - dist = Distribution({'script_name': os.path.basename(sys.argv[0]), - 'script_args': sys.argv[1:]}) - - with silence(): - try: - dist.parse_config_files() - dist.parse_command_line() - except (DistutilsError, AttributeError, SystemExit): - # Let distutils handle DistutilsErrors itself AttributeErrors can - # get raise for ./setup.py --help SystemExit can be raised if a - # display option was used, for example - pass - - return dist - - -def get_compiler(): - """ - Determines the compiler that will be used to build extension modules. - - Returns - ------- - compiler : str - The compiler option specified for the build, build_ext, or build_clib - command; or the default compiler for the platform if none was - specified. - - """ - return new_compiler().compiler_type diff --git a/extension_helpers/_openmp_helpers.py b/extension_helpers/_openmp_helpers.py index d01d87c..e7376d6 100644 --- a/extension_helpers/_openmp_helpers.py +++ b/extension_helpers/_openmp_helpers.py @@ -22,7 +22,7 @@ from setuptools.command.build_ext import customize_compiler, get_config_var, new_compiler -from ._distutils_helpers import get_compiler +from ._setup_helpers import get_compiler __all__ = ['add_openmp_flags_if_available'] diff --git a/extension_helpers/_setup_helpers.py b/extension_helpers/_setup_helpers.py index f2f831c..7e766da 100644 --- a/extension_helpers/_setup_helpers.py +++ b/extension_helpers/_setup_helpers.py @@ -12,15 +12,30 @@ from collections import defaultdict from setuptools import Extension, find_packages +from setuptools.command.build_ext import new_compiler -from ._distutils_helpers import get_compiler from ._utils import import_file, walk_skip_hidden -__all__ = ['get_extensions', 'pkg_config'] +__all__ = ['get_compiler', 'get_extensions', 'pkg_config'] log = logging.getLogger(__name__) +def get_compiler(): + """ + Determines the compiler that will be used to build extension modules. + + Returns + ------- + compiler : str + The compiler option specified for the build, build_ext, or build_clib + command; or the default compiler for the platform if none was + specified. + + """ + return new_compiler().compiler_type + + def get_extensions(srcdir='.'): """ Collect all extensions from Cython files and ``setup_package.py`` files. diff --git a/extension_helpers/_utils.py b/extension_helpers/_utils.py index c32a11b..75dc3cb 100644 --- a/extension_helpers/_utils.py +++ b/extension_helpers/_utils.py @@ -9,41 +9,6 @@ __all__ = ['write_if_different', 'import_file'] -class _DummyFile(object): - """A noop writeable object.""" - - errors = '' - - def write(self, s): - pass - - def flush(self): - pass - - -@contextlib.contextmanager -def silence(): - """A context manager that silences sys.stdout and sys.stderr.""" - - old_stdout = sys.stdout - old_stderr = sys.stderr - sys.stdout = _DummyFile() - sys.stderr = _DummyFile() - exception_occurred = False - try: - yield - except: # noqa - exception_occurred = True - # Go ahead and clean up so that exception handling can work normally - sys.stdout = old_stdout - sys.stderr = old_stderr - raise - - if not exception_occurred: - sys.stdout = old_stdout - sys.stderr = old_stderr - - if sys.platform == 'win32': import ctypes diff --git a/extension_helpers/tests/test_distutils_helpers.py b/extension_helpers/tests/test_distutils_helpers.py deleted file mode 100644 index af8a732..0000000 --- a/extension_helpers/tests/test_distutils_helpers.py +++ /dev/null @@ -1,7 +0,0 @@ -from .._distutils_helpers import get_compiler - -POSSIBLE_COMPILERS = ['unix', 'msvc', 'bcpp', 'cygwin', 'mingw32'] - - -def test_get_compiler(): - assert get_compiler() in POSSIBLE_COMPILERS diff --git a/extension_helpers/tests/test_setup_helpers.py b/extension_helpers/tests/test_setup_helpers.py index abbf8dc..9dc572d 100644 --- a/extension_helpers/tests/test_setup_helpers.py +++ b/extension_helpers/tests/test_setup_helpers.py @@ -5,7 +5,7 @@ import pytest -from .._setup_helpers import get_extensions +from .._setup_helpers import get_compiler, get_extensions from . import cleanup_import, run_setup extension_helpers_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) # noqa @@ -19,6 +19,13 @@ def teardown_module(module): os.remove(tmpfile) +POSSIBLE_COMPILERS = ['unix', 'msvc', 'bcpp', 'cygwin', 'mingw32'] + + +def test_get_compiler(): + assert get_compiler() in POSSIBLE_COMPILERS + + def _extension_test_package(tmpdir, request, extension_type='c', include_numpy=False): """Creates a simple test package with an extension module.""" diff --git a/extension_helpers/tests/test_utils.py b/extension_helpers/tests/test_utils.py new file mode 100644 index 0000000..dc01488 --- /dev/null +++ b/extension_helpers/tests/test_utils.py @@ -0,0 +1,26 @@ +import os +import time + +from .._utils import import_file, write_if_different + + +def test_import_file(tmpdir): + filename = str(tmpdir / 'spam.py') + with open(filename, 'w') as f: + f.write('magic = 12345') + module = import_file(filename) + assert module.magic == 12345 + + +def test_write_if_different(tmpdir): + filename = str(tmpdir / 'test.txt') + write_if_different(filename, b'abc') + time1 = os.path.getmtime(filename) + time.sleep(0.01) + write_if_different(filename, b'abc') + time2 = os.path.getmtime(filename) + assert time2 == time1 + time.sleep(0.01) + write_if_different(filename, b'abcd') + time3 = os.path.getmtime(filename) + assert time3 > time1