]> granicus.if.org Git - python/commitdiff
#5583 Added optional Extensions in Distutils
authorTarek Ziadé <ziade.tarek@gmail.com>
Tue, 31 Mar 2009 22:27:23 +0000 (22:27 +0000)
committerTarek Ziadé <ziade.tarek@gmail.com>
Tue, 31 Mar 2009 22:27:23 +0000 (22:27 +0000)
Doc/distutils/setupscript.rst
Lib/distutils/command/build_ext.py
Lib/distutils/extension.py
Lib/distutils/tests/test_build_ext.py
Misc/NEWS

index efdc51abe391b148e82100cf7896ca312791f546..61f57aa37b81c024b6e786bd585928d28aec1e8e 100644 (file)
@@ -334,6 +334,10 @@ Other options
 
 There are still some other options which can be used to handle special cases.
 
+The :option:`optional` option is a boolean; if it is true, that specifies that
+a build failure in the extension should not abort the build process, but simply
+not install the failing extension.
+
 The :option:`extra_objects` option is a list of object files to be passed to the
 linker. These files must not have extensions, as the default extension for the
 compiler is used.
index 125fa7f52df20b6c7a700dd7667135171b11f6af..905fa1ff7d7f31a0ec5be1a46f7839b7a21e694f 100644 (file)
@@ -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
index 440d128cdc6e658221c7ae3b2d944ea0693cb85d..c80c61e35beef394fdd42d5b2ad28d55918958f3 100644 (file)
@@ -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):
index ff60c12b1a8459cc1b556e0e95d2bd22a1ef02a6..a27696d8cb6ab120701ba4ab115f6dc43445440e 100644 (file)
@@ -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):
index aa99ac005cb907e2fea919c5c9fb671e9b8fa615..3d15073ef8e6bd3b50a417bdf2aa8c05e696c176 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -199,6 +199,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5583: Added optional Extensions in Distutils. Initial patch by Georg
+  Brandl.
+
 - Issue #5619: Multiprocessing children disobey the debug flag and causes
   popups on windows buildbots. Patch applied to work around this issue.