test_sysconfig.py 11 KB

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