Debian Codegraph @

cpython27/Lib/distutils/tests/test_sysconfig.py

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