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