diff --git a/Lib/distutils/extension.py b/Lib/distutils/extension.py index a4054ff4548..16d2bef6f44 100644 --- a/Lib/distutils/extension.py +++ b/Lib/distutils/extension.py @@ -5,12 +5,9 @@ modules in setup scripts.""" __revision__ = "$Id$" -import os, sys - -try: - import warnings -except ImportError: - warnings = None +import os +import sys +import warnings # This class is really only used by the "build_ext" command, so it might # make sense to put it in distutils.command.build_ext. However, that @@ -129,14 +126,11 @@ class Extension: self.optional = optional # If there are unknown keyword options, warn about them - if len(kw): - L = map(repr, sorted(kw)) - msg = "Unknown Extension options: " + ', '.join(L) - if warnings is not None: - warnings.warn(msg) - else: - sys.stderr.write(msg + '\n') - + if len(kw) > 0: + options = [repr(option) for option in kw] + options = ', '.join(sorted(options)) + msg = "Unknown Extension options: %s" % options + warnings.warn(msg) def read_setup_file(filename): """Reads a Setup file and returns Extension instances.""" diff --git a/Lib/distutils/tests/test_extension.py b/Lib/distutils/tests/test_extension.py index 1fcf0f5ecf2..1ee30585fab 100755 --- a/Lib/distutils/tests/test_extension.py +++ b/Lib/distutils/tests/test_extension.py @@ -1,8 +1,10 @@ """Tests for distutils.extension.""" import unittest import os +import warnings -from distutils.extension import read_setup_file +from test.support import check_warnings +from distutils.extension import read_setup_file, Extension class ExtensionTestCase(unittest.TestCase): @@ -28,6 +30,37 @@ class ExtensionTestCase(unittest.TestCase): self.assertEquals(names, wanted) + def test_extension_init(self): + # the first argument, which is the name, must be a string + self.assertRaises(AssertionError, Extension, 1, []) + ext = Extension('name', []) + self.assertEquals(ext.name, 'name') + + # the second argument, which is the list of files, must + # be a list of strings + self.assertRaises(AssertionError, Extension, 'name', 'file') + self.assertRaises(AssertionError, Extension, 'name', ['file', 1]) + ext = Extension('name', ['file1', 'file2']) + self.assertEquals(ext.sources, ['file1', 'file2']) + + # others arguments have defaults + for attr in ('include_dirs', 'define_macros', 'undef_macros', + 'library_dirs', 'libraries', 'runtime_library_dirs', + 'extra_objects', 'extra_compile_args', 'extra_link_args', + 'export_symbols', 'swig_opts', 'depends'): + self.assertEquals(getattr(ext, attr), []) + + self.assertEquals(ext.language, None) + self.assertEquals(ext.optional, None) + + # if there are unknown keyword options, warn about them + with check_warnings() as w: + warnings.simplefilter('always') + ext = Extension('name', ['file1', 'file2'], chic=True) + + self.assertEquals(len(w.warnings), 1) + self.assertEquals(str(w.warnings[0].message), + "Unknown Extension options: 'chic'") def test_suite(): return unittest.makeSuite(ExtensionTestCase)