Debian Codegraph @

lib-python/3/distutils/tests/test_sysconfig.py

raw symbols SCIP
1 """Tests for distutils.sysconfig."""
2 import contextlib
3 import os
4 import shutil
5 import subprocess
6 import sys
7 import textwrap
8 import unittest
9
10 from distutils import sysconfig
11 from distutils.ccompiler import get_default_compiler
12 from distutils.tests import support
13 from test.support import swap_item, requires_subprocess, is_wasi, impl_detail
14 from test.support.os_helper import TESTFN
15 from test.support.warnings_helper import check_warnings
16
17
18 class SysconfigTestCase(support.EnvironGuard, unittest.TestCase):
19 def setUp(self):
20 super(SysconfigTestCase, self).setUp()
21 self.makefile = None
22
23 def tearDown(self):
24 if self.makefile is not None:
25 os.unlink(self.makefile)
26 self.cleanup_testfn()
27 super(SysconfigTestCase, self).tearDown()
28
29 def cleanup_testfn(self):
30 if os.path.isfile(TESTFN):
31 os.remove(TESTFN)
32 elif os.path.isdir(TESTFN):
33 shutil.rmtree(TESTFN)
34
35 @unittest.skipIf(is_wasi, "Incompatible with WASI mapdir and OOT builds")
36 def test_get_config_h_filename(self):
37 config_h = sysconfig.get_config_h_filename()
38 self.assertTrue(os.path.isfile(config_h), config_h)
39
40 def test_get_python_lib(self):
41 # XXX doesn't work on Linux when Python was never installed before
42 #self.assertTrue(os.path.isdir(lib_dir), lib_dir)
43 # test for pythonxx.lib?
44 self.assertNotEqual(sysconfig.get_python_lib(),
45 sysconfig.get_python_lib(prefix=TESTFN))
46
47 def test_get_config_vars(self):
48 cvars = sysconfig.get_config_vars()
49 self.assertIsInstance(cvars, dict)
50 self.assertTrue(cvars)
51
52 @unittest.skipIf(is_wasi, "Incompatible with WASI mapdir and OOT builds")
53 @impl_detail("pypy fakes makefile_filename", pypy=False)
54 def test_srcdir(self):
55 # See Issues #15322, #15364.
56 srcdir = sysconfig.get_config_var('srcdir')
57
58 self.assertTrue(os.path.isabs(srcdir), srcdir)
59 self.assertTrue(os.path.isdir(srcdir), srcdir)
60
61 if sysconfig.python_build:
62 # The python executable has not been installed so srcdir
63 # should be a full source checkout.
64 Python_h = os.path.join(srcdir, 'Include', 'Python.h')
65 self.assertTrue(os.path.exists(Python_h), Python_h)
66 # <srcdir>/PC/pyconfig.h always exists even if unused on POSIX.
67 pyconfig_h = os.path.join(srcdir, 'PC', 'pyconfig.h')
68 self.assertTrue(os.path.exists(pyconfig_h), pyconfig_h)
69 pyconfig_h_in = os.path.join(srcdir, 'pyconfig.h.in')
70 self.assertTrue(os.path.exists(pyconfig_h_in), pyconfig_h_in)
71 elif os.name == 'posix':
72 self.assertEqual(
73 os.path.dirname(sysconfig.get_makefile_filename()),
74 srcdir)
75
76 def test_srcdir_independent_of_cwd(self):
77 # srcdir should be independent of the current working directory
78 # See Issues #15322, #15364.
79 srcdir = sysconfig.get_config_var('srcdir')
80 cwd = os.getcwd()
81 try:
82 os.chdir('..')
83 srcdir2 = sysconfig.get_config_var('srcdir')
84 finally:
85 os.chdir(cwd)
86 self.assertEqual(srcdir, srcdir2)
87
88 def customize_compiler(self):
89 # make sure AR gets caught
90 class compiler:
91 compiler_type = 'unix'
92
93 def set_executables(self, **kw):
94 self.exes = kw
95
96 sysconfig_vars = {
97 'AR': 'sc_ar',
98 'CC': 'sc_cc',
99 'CXX': 'sc_cxx',
100 'ARFLAGS': '--sc-arflags',
101 'CFLAGS': '--sc-cflags',
102 'CCSHARED': '--sc-ccshared',
103 'LDSHARED': 'sc_ldshared',
104 'SHLIB_SUFFIX': 'sc_shutil_suffix',
105
106 # On macOS, disable _osx_support.customize_compiler()
107 'CUSTOMIZED_OSX_COMPILER': 'True',
108 }
109
110 comp = compiler()
111 with contextlib.ExitStack() as cm:
112 for key, value in sysconfig_vars.items():
113 cm.enter_context(swap_item(sysconfig._config_vars, key, value))
114 sysconfig.customize_compiler(comp)
115
116 return comp
117
118 @unittest.skipUnless(get_default_compiler() == 'unix',
119 'not testing if default compiler is not unix')
120 def test_customize_compiler(self):
121 # Make sure that sysconfig._config_vars is initialized
122 sysconfig.get_config_vars()
123
124 os.environ['AR'] = 'env_ar'
125 os.environ['CC'] = 'env_cc'
126 os.environ['CPP'] = 'env_cpp'
127 os.environ['CXX'] = 'env_cxx --env-cxx-flags'
128 os.environ['LDSHARED'] = 'env_ldshared'
129 os.environ['LDFLAGS'] = '--env-ldflags'
130 os.environ['ARFLAGS'] = '--env-arflags'
131 os.environ['CFLAGS'] = '--env-cflags'
132 os.environ['CPPFLAGS'] = '--env-cppflags'
133
134 comp = self.customize_compiler()
135 self.assertEqual(comp.exes['archiver'],
136 'env_ar --env-arflags')
137 self.assertEqual(comp.exes['preprocessor'],
138 'env_cpp --env-cppflags')
139 self.assertEqual(comp.exes['compiler'],
140 'env_cc --sc-cflags --env-cflags --env-cppflags')
141 self.assertEqual(comp.exes['compiler_so'],
142 ('env_cc --sc-cflags '
143 '--env-cflags ''--env-cppflags --sc-ccshared'))
144 self.assertEqual(comp.exes['compiler_cxx'],
145 'env_cxx --env-cxx-flags')
146 self.assertEqual(comp.exes['linker_exe'],
147 'env_cc')
148 self.assertEqual(comp.exes['linker_so'],
149 ('env_ldshared --env-ldflags --env-cflags'
150 ' --env-cppflags'))
151 self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
152
153 del os.environ['AR']
154 del os.environ['CC']
155 del os.environ['CPP']
156 del os.environ['CXX']
157 del os.environ['LDSHARED']
158 del os.environ['LDFLAGS']
159 del os.environ['ARFLAGS']
160 del os.environ['CFLAGS']
161 del os.environ['CPPFLAGS']
162
163 comp = self.customize_compiler()
164 self.assertEqual(comp.exes['archiver'],
165 'sc_ar --sc-arflags')
166 self.assertEqual(comp.exes['preprocessor'],
167 'sc_cc -E')
168 self.assertEqual(comp.exes['compiler'],
169 'sc_cc --sc-cflags')
170 self.assertEqual(comp.exes['compiler_so'],
171 'sc_cc --sc-cflags --sc-ccshared')
172 self.assertEqual(comp.exes['compiler_cxx'],
173 'sc_cxx')
174 self.assertEqual(comp.exes['linker_exe'],
175 'sc_cc')
176 self.assertEqual(comp.exes['linker_so'],
177 'sc_ldshared')
178 self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
179
180 def test_parse_makefile_base(self):
181 self.makefile = TESTFN
182 fd = open(self.makefile, 'w')
183 try:
184 fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=LIB'" '\n')
185 fd.write('VAR=$OTHER\nOTHER=foo')
186 finally:
187 fd.close()
188 d = sysconfig.parse_makefile(self.makefile)
189 self.assertEqual(d, {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'",
190 'OTHER': 'foo'})
191
192 def test_parse_makefile_literal_dollar(self):
193 self.makefile = TESTFN
194 fd = open(self.makefile, 'w')
195 try:
196 fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=\$$LIB'" '\n')
197 fd.write('VAR=$OTHER\nOTHER=foo')
198 finally:
199 fd.close()
200 d = sysconfig.parse_makefile(self.makefile)
201 self.assertEqual(d, {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'",
202 'OTHER': 'foo'})
203
204
205 def test_sysconfig_module(self):
206 import sysconfig as global_sysconfig
207 self.assertEqual(global_sysconfig.get_config_var('CFLAGS'),
208 sysconfig.get_config_var('CFLAGS'))
209 self.assertEqual(global_sysconfig.get_config_var('LDFLAGS'),
210 sysconfig.get_config_var('LDFLAGS'))
211
212 @unittest.skipIf(sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'),
213 'compiler flags customized')
214 def test_sysconfig_compiler_vars(self):
215 # On OS X, binary installers support extension module building on
216 # various levels of the operating system with differing Xcode
217 # configurations. This requires customization of some of the
218 # compiler configuration directives to suit the environment on
219 # the installed machine. Some of these customizations may require
220 # running external programs and, so, are deferred until needed by
221 # the first extension module build. With Python 3.3, only
222 # the Distutils version of sysconfig is used for extension module
223 # builds, which happens earlier in the Distutils tests. This may
224 # cause the following tests to fail since no tests have caused
225 # the global version of sysconfig to call the customization yet.
226 # The solution for now is to simply skip this test in this case.
227 # The longer-term solution is to only have one version of sysconfig.
228
229 import sysconfig as global_sysconfig
230 if sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'):
231 self.skipTest('compiler flags customized')
232 self.assertEqual(global_sysconfig.get_config_var('LDSHARED'),
233 sysconfig.get_config_var('LDSHARED'))
234 self.assertEqual(global_sysconfig.get_config_var('CC'),
235 sysconfig.get_config_var('CC'))
236
237 @requires_subprocess()
238 def test_customize_compiler_before_get_config_vars(self):
239 # Issue #21923: test that a Distribution compiler
240 # instance can be called without an explicit call to
241 # get_config_vars().
242 with open(TESTFN, 'w') as f:
243 f.writelines(textwrap.dedent('''\
244 from distutils.core import Distribution
245 config = Distribution().get_command_obj('config')
246 # try_compile may pass or it may fail if no compiler
247 # is found but it should not raise an exception.
248 rc = config.try_compile('int x;')
249 '''))
250 p = subprocess.Popen([str(sys.executable), TESTFN],
251 stdout=subprocess.PIPE,
252 stderr=subprocess.STDOUT,
253 universal_newlines=True)
254 outs, errs = p.communicate()
255 self.assertEqual(0, p.returncode, "Subprocess failed: " + outs)
256
257
258 if __name__ == '__main__':
259 unittest.main()