1 | n/a | """Tests for distutils.sysconfig.""" |
---|
2 | n/a | import os |
---|
3 | n/a | import shutil |
---|
4 | n/a | import subprocess |
---|
5 | n/a | import sys |
---|
6 | n/a | import textwrap |
---|
7 | n/a | import unittest |
---|
8 | n/a | |
---|
9 | n/a | from distutils import sysconfig |
---|
10 | n/a | from distutils.ccompiler import get_default_compiler |
---|
11 | n/a | from distutils.tests import support |
---|
12 | n/a | from test.support import TESTFN, run_unittest, check_warnings |
---|
13 | n/a | |
---|
14 | n/a | class SysconfigTestCase(support.EnvironGuard, unittest.TestCase): |
---|
15 | n/a | def setUp(self): |
---|
16 | n/a | super(SysconfigTestCase, self).setUp() |
---|
17 | n/a | self.makefile = None |
---|
18 | n/a | |
---|
19 | n/a | def tearDown(self): |
---|
20 | n/a | if self.makefile is not None: |
---|
21 | n/a | os.unlink(self.makefile) |
---|
22 | n/a | self.cleanup_testfn() |
---|
23 | n/a | super(SysconfigTestCase, self).tearDown() |
---|
24 | n/a | |
---|
25 | n/a | def cleanup_testfn(self): |
---|
26 | n/a | if os.path.isfile(TESTFN): |
---|
27 | n/a | os.remove(TESTFN) |
---|
28 | n/a | elif os.path.isdir(TESTFN): |
---|
29 | n/a | shutil.rmtree(TESTFN) |
---|
30 | n/a | |
---|
31 | n/a | def test_get_config_h_filename(self): |
---|
32 | n/a | config_h = sysconfig.get_config_h_filename() |
---|
33 | n/a | self.assertTrue(os.path.isfile(config_h), config_h) |
---|
34 | n/a | |
---|
35 | n/a | def test_get_python_lib(self): |
---|
36 | n/a | # XXX doesn't work on Linux when Python was never installed before |
---|
37 | n/a | #self.assertTrue(os.path.isdir(lib_dir), lib_dir) |
---|
38 | n/a | # test for pythonxx.lib? |
---|
39 | n/a | self.assertNotEqual(sysconfig.get_python_lib(), |
---|
40 | n/a | sysconfig.get_python_lib(prefix=TESTFN)) |
---|
41 | n/a | |
---|
42 | n/a | def test_get_config_vars(self): |
---|
43 | n/a | cvars = sysconfig.get_config_vars() |
---|
44 | n/a | self.assertIsInstance(cvars, dict) |
---|
45 | n/a | self.assertTrue(cvars) |
---|
46 | n/a | |
---|
47 | n/a | def test_srcdir(self): |
---|
48 | n/a | # See Issues #15322, #15364. |
---|
49 | n/a | srcdir = sysconfig.get_config_var('srcdir') |
---|
50 | n/a | |
---|
51 | n/a | self.assertTrue(os.path.isabs(srcdir), srcdir) |
---|
52 | n/a | self.assertTrue(os.path.isdir(srcdir), srcdir) |
---|
53 | n/a | |
---|
54 | n/a | if sysconfig.python_build: |
---|
55 | n/a | # The python executable has not been installed so srcdir |
---|
56 | n/a | # should be a full source checkout. |
---|
57 | n/a | Python_h = os.path.join(srcdir, 'Include', 'Python.h') |
---|
58 | n/a | self.assertTrue(os.path.exists(Python_h), Python_h) |
---|
59 | n/a | self.assertTrue(sysconfig._is_python_source_dir(srcdir)) |
---|
60 | n/a | elif os.name == 'posix': |
---|
61 | n/a | self.assertEqual( |
---|
62 | n/a | os.path.dirname(sysconfig.get_makefile_filename()), |
---|
63 | n/a | srcdir) |
---|
64 | n/a | |
---|
65 | n/a | def test_srcdir_independent_of_cwd(self): |
---|
66 | n/a | # srcdir should be independent of the current working directory |
---|
67 | n/a | # See Issues #15322, #15364. |
---|
68 | n/a | srcdir = sysconfig.get_config_var('srcdir') |
---|
69 | n/a | cwd = os.getcwd() |
---|
70 | n/a | try: |
---|
71 | n/a | os.chdir('..') |
---|
72 | n/a | srcdir2 = sysconfig.get_config_var('srcdir') |
---|
73 | n/a | finally: |
---|
74 | n/a | os.chdir(cwd) |
---|
75 | n/a | self.assertEqual(srcdir, srcdir2) |
---|
76 | n/a | |
---|
77 | n/a | @unittest.skipUnless(get_default_compiler() == 'unix', |
---|
78 | n/a | 'not testing if default compiler is not unix') |
---|
79 | n/a | def test_customize_compiler(self): |
---|
80 | n/a | os.environ['AR'] = 'my_ar' |
---|
81 | n/a | os.environ['ARFLAGS'] = '-arflags' |
---|
82 | n/a | |
---|
83 | n/a | # make sure AR gets caught |
---|
84 | n/a | class compiler: |
---|
85 | n/a | compiler_type = 'unix' |
---|
86 | n/a | |
---|
87 | n/a | def set_executables(self, **kw): |
---|
88 | n/a | self.exes = kw |
---|
89 | n/a | |
---|
90 | n/a | comp = compiler() |
---|
91 | n/a | sysconfig.customize_compiler(comp) |
---|
92 | n/a | self.assertEqual(comp.exes['archiver'], 'my_ar -arflags') |
---|
93 | n/a | |
---|
94 | n/a | def test_parse_makefile_base(self): |
---|
95 | n/a | self.makefile = TESTFN |
---|
96 | n/a | fd = open(self.makefile, 'w') |
---|
97 | n/a | try: |
---|
98 | n/a | fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=LIB'" '\n') |
---|
99 | n/a | fd.write('VAR=$OTHER\nOTHER=foo') |
---|
100 | n/a | finally: |
---|
101 | n/a | fd.close() |
---|
102 | n/a | d = sysconfig.parse_makefile(self.makefile) |
---|
103 | n/a | self.assertEqual(d, {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'", |
---|
104 | n/a | 'OTHER': 'foo'}) |
---|
105 | n/a | |
---|
106 | n/a | def test_parse_makefile_literal_dollar(self): |
---|
107 | n/a | self.makefile = TESTFN |
---|
108 | n/a | fd = open(self.makefile, 'w') |
---|
109 | n/a | try: |
---|
110 | n/a | fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=\$$LIB'" '\n') |
---|
111 | n/a | fd.write('VAR=$OTHER\nOTHER=foo') |
---|
112 | n/a | finally: |
---|
113 | n/a | fd.close() |
---|
114 | n/a | d = sysconfig.parse_makefile(self.makefile) |
---|
115 | n/a | self.assertEqual(d, {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'", |
---|
116 | n/a | 'OTHER': 'foo'}) |
---|
117 | n/a | |
---|
118 | n/a | |
---|
119 | n/a | def test_sysconfig_module(self): |
---|
120 | n/a | import sysconfig as global_sysconfig |
---|
121 | n/a | self.assertEqual(global_sysconfig.get_config_var('CFLAGS'), |
---|
122 | n/a | sysconfig.get_config_var('CFLAGS')) |
---|
123 | n/a | self.assertEqual(global_sysconfig.get_config_var('LDFLAGS'), |
---|
124 | n/a | sysconfig.get_config_var('LDFLAGS')) |
---|
125 | n/a | |
---|
126 | n/a | @unittest.skipIf(sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'), |
---|
127 | n/a | 'compiler flags customized') |
---|
128 | n/a | def test_sysconfig_compiler_vars(self): |
---|
129 | n/a | # On OS X, binary installers support extension module building on |
---|
130 | n/a | # various levels of the operating system with differing Xcode |
---|
131 | n/a | # configurations. This requires customization of some of the |
---|
132 | n/a | # compiler configuration directives to suit the environment on |
---|
133 | n/a | # the installed machine. Some of these customizations may require |
---|
134 | n/a | # running external programs and, so, are deferred until needed by |
---|
135 | n/a | # the first extension module build. With Python 3.3, only |
---|
136 | n/a | # the Distutils version of sysconfig is used for extension module |
---|
137 | n/a | # builds, which happens earlier in the Distutils tests. This may |
---|
138 | n/a | # cause the following tests to fail since no tests have caused |
---|
139 | n/a | # the global version of sysconfig to call the customization yet. |
---|
140 | n/a | # The solution for now is to simply skip this test in this case. |
---|
141 | n/a | # The longer-term solution is to only have one version of sysconfig. |
---|
142 | n/a | |
---|
143 | n/a | import sysconfig as global_sysconfig |
---|
144 | n/a | if sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'): |
---|
145 | n/a | self.skipTest('compiler flags customized') |
---|
146 | n/a | self.assertEqual(global_sysconfig.get_config_var('LDSHARED'), |
---|
147 | n/a | sysconfig.get_config_var('LDSHARED')) |
---|
148 | n/a | self.assertEqual(global_sysconfig.get_config_var('CC'), |
---|
149 | n/a | sysconfig.get_config_var('CC')) |
---|
150 | n/a | |
---|
151 | n/a | @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, |
---|
152 | n/a | 'EXT_SUFFIX required for this test') |
---|
153 | n/a | def test_SO_deprecation(self): |
---|
154 | n/a | self.assertWarns(DeprecationWarning, |
---|
155 | n/a | sysconfig.get_config_var, 'SO') |
---|
156 | n/a | |
---|
157 | n/a | @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, |
---|
158 | n/a | 'EXT_SUFFIX required for this test') |
---|
159 | n/a | def test_SO_value(self): |
---|
160 | n/a | with check_warnings(('', DeprecationWarning)): |
---|
161 | n/a | self.assertEqual(sysconfig.get_config_var('SO'), |
---|
162 | n/a | sysconfig.get_config_var('EXT_SUFFIX')) |
---|
163 | n/a | |
---|
164 | n/a | @unittest.skipIf(sysconfig.get_config_var('EXT_SUFFIX') is None, |
---|
165 | n/a | 'EXT_SUFFIX required for this test') |
---|
166 | n/a | def test_SO_in_vars(self): |
---|
167 | n/a | vars = sysconfig.get_config_vars() |
---|
168 | n/a | self.assertIsNotNone(vars['SO']) |
---|
169 | n/a | self.assertEqual(vars['SO'], vars['EXT_SUFFIX']) |
---|
170 | n/a | |
---|
171 | n/a | def test_customize_compiler_before_get_config_vars(self): |
---|
172 | n/a | # Issue #21923: test that a Distribution compiler |
---|
173 | n/a | # instance can be called without an explicit call to |
---|
174 | n/a | # get_config_vars(). |
---|
175 | n/a | with open(TESTFN, 'w') as f: |
---|
176 | n/a | f.writelines(textwrap.dedent('''\ |
---|
177 | n/a | from distutils.core import Distribution |
---|
178 | n/a | config = Distribution().get_command_obj('config') |
---|
179 | n/a | # try_compile may pass or it may fail if no compiler |
---|
180 | n/a | # is found but it should not raise an exception. |
---|
181 | n/a | rc = config.try_compile('int x;') |
---|
182 | n/a | ''')) |
---|
183 | n/a | p = subprocess.Popen([str(sys.executable), TESTFN], |
---|
184 | n/a | stdout=subprocess.PIPE, |
---|
185 | n/a | stderr=subprocess.STDOUT, |
---|
186 | n/a | universal_newlines=True) |
---|
187 | n/a | outs, errs = p.communicate() |
---|
188 | n/a | self.assertEqual(0, p.returncode, "Subprocess failed: " + outs) |
---|
189 | n/a | |
---|
190 | n/a | |
---|
191 | n/a | def test_suite(): |
---|
192 | n/a | suite = unittest.TestSuite() |
---|
193 | n/a | suite.addTest(unittest.makeSuite(SysconfigTestCase)) |
---|
194 | n/a | return suite |
---|
195 | n/a | |
---|
196 | n/a | |
---|
197 | n/a | if __name__ == '__main__': |
---|
198 | n/a | run_unittest(test_suite()) |
---|