diff options
author | Tarek Ziadé <ziade.tarek@gmail.com> | 2009-03-31 22:27:23 (GMT) |
---|---|---|
committer | Tarek Ziadé <ziade.tarek@gmail.com> | 2009-03-31 22:27:23 (GMT) |
commit | 9e47ce49a06075adbc391e04b4457707fa3892a7 (patch) | |
tree | 3f45fb5a324e9dae28b8429510fca186c0716247 /Lib/distutils | |
parent | 42f9b4e5b4fff45ef4a071112787de6b29a5099a (diff) | |
download | cpython-9e47ce49a06075adbc391e04b4457707fa3892a7.zip cpython-9e47ce49a06075adbc391e04b4457707fa3892a7.tar.gz cpython-9e47ce49a06075adbc391e04b4457707fa3892a7.tar.bz2 |
#5583 Added optional Extensions in Distutils
Diffstat (limited to 'Lib/distutils')
-rw-r--r-- | Lib/distutils/command/build_ext.py | 8 | ||||
-rw-r--r-- | Lib/distutils/extension.py | 5 | ||||
-rw-r--r-- | Lib/distutils/tests/test_build_ext.py | 22 |
3 files changed, 33 insertions, 2 deletions
diff --git a/Lib/distutils/command/build_ext.py b/Lib/distutils/command/build_ext.py index 125fa7f..905fa1f 100644 --- a/Lib/distutils/command/build_ext.py +++ b/Lib/distutils/command/build_ext.py @@ -476,7 +476,13 @@ class build_ext (Command): self.check_extensions_list(self.extensions) for ext in self.extensions: - self.build_extension(ext) + try: + self.build_extension(ext) + except (CCompilerError, DistutilsError), e: + if not ext.optional: + raise + self.warn('building extension "%s" failed: %s' % + (ext.name, e)) def build_extension(self, ext): sources = ext.sources diff --git a/Lib/distutils/extension.py b/Lib/distutils/extension.py index 440d128..c80c61e 100644 --- a/Lib/distutils/extension.py +++ b/Lib/distutils/extension.py @@ -83,6 +83,9 @@ class Extension: language : string extension language (i.e. "c", "c++", "objc"). Will be detected from the source extensions if not provided. + optional : boolean + specifies that a build failure in the extension should not abort the + build process, but simply not install the failing extension. """ # When adding arguments to this constructor, be sure to update @@ -101,6 +104,7 @@ class Extension: swig_opts = None, depends=None, language=None, + optional=None, **kw # To catch unknown keywords ): assert type(name) is StringType, "'name' must be a string" @@ -123,6 +127,7 @@ class Extension: self.swig_opts = swig_opts or [] self.depends = depends or [] self.language = language + self.optional = optional # If there are unknown keyword options, warn about them if len(kw): diff --git a/Lib/distutils/tests/test_build_ext.py b/Lib/distutils/tests/test_build_ext.py index ff60c12..a27696d 100644 --- a/Lib/distutils/tests/test_build_ext.py +++ b/Lib/distutils/tests/test_build_ext.py @@ -8,6 +8,8 @@ from distutils.core import Extension, Distribution from distutils.command.build_ext import build_ext from distutils import sysconfig from distutils.tests import support +from distutils.extension import Extension +from distutils.errors import UnknownFileError import unittest from test import test_support @@ -20,7 +22,9 @@ def _get_source_filename(): srcdir = sysconfig.get_config_var('srcdir') return os.path.join(srcdir, 'Modules', 'xxmodule.c') -class BuildExtTestCase(support.TempdirManager, unittest.TestCase): +class BuildExtTestCase(support.TempdirManager, + support.LoggingSilencer, + unittest.TestCase): def setUp(self): # Create a simple test environment # Note that we're making changes to sys.path @@ -142,6 +146,22 @@ class BuildExtTestCase(support.TempdirManager, unittest.TestCase): self.assert_(lib in cmd.library_dirs) self.assert_(incl in cmd.include_dirs) + def test_optional_extension(self): + + # this extension will fail, but let's ignore this failure + # with the optional argument. + modules = [Extension('foo', ['xxx'], optional=False)] + dist = Distribution({'name': 'xx', 'ext_modules': modules}) + cmd = build_ext(dist) + cmd.ensure_finalized() + self.assertRaises(UnknownFileError, cmd.run) # should raise an error + + modules = [Extension('foo', ['xxx'], optional=True)] + dist = Distribution({'name': 'xx', 'ext_modules': modules}) + cmd = build_ext(dist) + cmd.ensure_finalized() + cmd.run() # should pass + def test_suite(): src = _get_source_filename() if not os.path.exists(src): |