test_sysconfig.py 11 KB

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