test_sysconfig.py 8.2 KB

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