Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2e36f1

Browse files
author
Tarek Ziadé
committed
Merged revisions 70910 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r70910 | tarek.ziade | 2009-03-31 17:27:23 -0500 (Tue, 31 Mar 2009) | 1 line #5583 Added optional Extensions in Distutils ........
1 parent a931404 commit b2e36f1

5 files changed

Lines changed: 41 additions & 2 deletions

File tree

Doc/distutils/setupscript.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ Other options
334334

335335
There are still some other options which can be used to handle special cases.
336336

337+
The :option:`optional` option is a boolean; if it is true, that specifies that
338+
a build failure in the extension should not abort the build process, but simply
339+
not install the failing extension.
340+
337341
The :option:`extra_objects` option is a list of object files to be passed to the
338342
linker. These files must not have extensions, as the default extension for the
339343
compiler is used.

Lib/distutils/command/build_ext.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,13 @@ def build_extensions(self):
455455
self.check_extensions_list(self.extensions)
456456

457457
for ext in self.extensions:
458-
self.build_extension(ext)
458+
try:
459+
self.build_extension(ext)
460+
except (CCompilerError, DistutilsError) as e:
461+
if not ext.optional:
462+
raise
463+
self.warn('building extension "%s" failed: %s' %
464+
(ext.name, e))
459465

460466
def build_extension(self, ext):
461467
sources = ext.sources

Lib/distutils/extension.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class Extension:
8282
language : string
8383
extension language (i.e. "c", "c++", "objc"). Will be detected
8484
from the source extensions if not provided.
85+
optional : boolean
86+
specifies that a build failure in the extension should not abort the
87+
build process, but simply not install the failing extension.
8588
"""
8689

8790
# When adding arguments to this constructor, be sure to update
@@ -100,6 +103,7 @@ def __init__(self, name, sources,
100103
swig_opts = None,
101104
depends=None,
102105
language=None,
106+
optional=None,
103107
**kw # To catch unknown keywords
104108
):
105109
assert isinstance(name, str), "'name' must be a string"
@@ -122,6 +126,7 @@ def __init__(self, name, sources,
122126
self.swig_opts = swig_opts or []
123127
self.depends = depends or []
124128
self.language = language
129+
self.optional = optional
125130

126131
# If there are unknown keyword options, warn about them
127132
if len(kw):

Lib/distutils/tests/test_build_ext.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from distutils.command.build_ext import build_ext
99
from distutils import sysconfig
1010
from distutils.tests.support import TempdirManager
11+
from distutils.tests.support import LoggingSilencer
12+
from distutils.extension import Extension
13+
from distutils.errors import UnknownFileError
1114

1215
import unittest
1316
from test import support
@@ -20,7 +23,9 @@ def _get_source_filename():
2023
srcdir = sysconfig.get_config_var('srcdir')
2124
return os.path.join(srcdir, 'Modules', 'xxmodule.c')
2225

23-
class BuildExtTestCase(TempdirManager, unittest.TestCase):
26+
class BuildExtTestCase(TempdirManager,
27+
LoggingSilencer,
28+
unittest.TestCase):
2429
def setUp(self):
2530
# Create a simple test environment
2631
# Note that we're making changes to sys.path
@@ -141,6 +146,22 @@ def test_user_site(self):
141146
self.assert_(lib in cmd.library_dirs)
142147
self.assert_(incl in cmd.include_dirs)
143148

149+
def test_optional_extension(self):
150+
151+
# this extension will fail, but let's ignore this failure
152+
# with the optional argument.
153+
modules = [Extension('foo', ['xxx'], optional=False)]
154+
dist = Distribution({'name': 'xx', 'ext_modules': modules})
155+
cmd = build_ext(dist)
156+
cmd.ensure_finalized()
157+
self.assertRaises(UnknownFileError, cmd.run) # should raise an error
158+
159+
modules = [Extension('foo', ['xxx'], optional=True)]
160+
dist = Distribution({'name': 'xx', 'ext_modules': modules})
161+
cmd = build_ext(dist)
162+
cmd.ensure_finalized()
163+
cmd.run() # should pass
164+
144165
def test_suite():
145166
src = _get_source_filename()
146167
if not os.path.exists(src):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ Core and Builtins
282282
Library
283283
-------
284284

285+
- Issue #5583: Added optional Extensions in Distutils. Initial patch by Georg
286+
Brandl.
287+
285288
- Issue #1222: locale.format() bug when the thousands separator is a space
286289
character.
287290

0 commit comments

Comments
 (0)