test_sysconfig.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. """Tests for sysconfig."""
  2. import unittest
  3. import sys
  4. import os
  5. import shutil
  6. import subprocess
  7. from copy import copy, deepcopy
  8. from test.test_support import run_unittest, TESTFN, unlink, get_attribute
  9. import sysconfig
  10. from sysconfig import (get_paths, get_platform, get_config_vars,
  11. get_path, get_path_names, _INSTALL_SCHEMES,
  12. _get_default_scheme, _expand_vars,
  13. get_scheme_names, get_config_var)
  14. import _osx_support
  15. class TestSysConfig(unittest.TestCase):
  16. def setUp(self):
  17. """Make a copy of sys.path"""
  18. super(TestSysConfig, self).setUp()
  19. self.sys_path = sys.path[:]
  20. self.makefile = None
  21. # patching os.uname
  22. if hasattr(os, 'uname'):
  23. self.uname = os.uname
  24. self._uname = os.uname()
  25. else:
  26. self.uname = None
  27. self._uname = None
  28. os.uname = self._get_uname
  29. # saving the environment
  30. self.name = os.name
  31. self.platform = sys.platform
  32. self.version = sys.version
  33. self.sep = os.sep
  34. self.join = os.path.join
  35. self.isabs = os.path.isabs
  36. self.splitdrive = os.path.splitdrive
  37. self._config_vars = copy(sysconfig._CONFIG_VARS)
  38. self.old_environ = deepcopy(os.environ)
  39. def tearDown(self):
  40. """Restore sys.path"""
  41. sys.path[:] = self.sys_path
  42. if self.makefile is not None:
  43. os.unlink(self.makefile)
  44. self._cleanup_testfn()
  45. if self.uname is not None:
  46. os.uname = self.uname
  47. else:
  48. del os.uname
  49. os.name = self.name
  50. sys.platform = self.platform
  51. sys.version = self.version
  52. os.sep = self.sep
  53. os.path.join = self.join
  54. os.path.isabs = self.isabs
  55. os.path.splitdrive = self.splitdrive
  56. sysconfig._CONFIG_VARS = copy(self._config_vars)
  57. for key, value in self.old_environ.items():
  58. if os.environ.get(key) != value:
  59. os.environ[key] = value
  60. for key in os.environ.keys():
  61. if key not in self.old_environ:
  62. del os.environ[key]
  63. super(TestSysConfig, self).tearDown()
  64. def _set_uname(self, uname):
  65. self._uname = uname
  66. def _get_uname(self):
  67. return self._uname
  68. def _cleanup_testfn(self):
  69. path = TESTFN
  70. if os.path.isfile(path):
  71. os.remove(path)
  72. elif os.path.isdir(path):
  73. shutil.rmtree(path)
  74. def test_get_path_names(self):
  75. self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
  76. def test_get_paths(self):
  77. scheme = get_paths()
  78. default_scheme = _get_default_scheme()
  79. wanted = _expand_vars(default_scheme, None)
  80. wanted = wanted.items()
  81. wanted.sort()
  82. scheme = scheme.items()
  83. scheme.sort()
  84. self.assertEqual(scheme, wanted)
  85. def test_get_path(self):
  86. # xxx make real tests here
  87. for scheme in _INSTALL_SCHEMES:
  88. for name in _INSTALL_SCHEMES[scheme]:
  89. res = get_path(name, scheme)
  90. def test_get_config_vars(self):
  91. cvars = get_config_vars()
  92. self.assertIsInstance(cvars, dict)
  93. self.assertTrue(cvars)
  94. def test_get_platform(self):
  95. # windows XP, 32bits
  96. os.name = 'nt'
  97. sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
  98. '[MSC v.1310 32 bit (Intel)]')
  99. sys.platform = 'win32'
  100. self.assertEqual(get_platform(), 'win32')
  101. # windows XP, amd64
  102. os.name = 'nt'
  103. sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
  104. '[MSC v.1310 32 bit (Amd64)]')
  105. sys.platform = 'win32'
  106. self.assertEqual(get_platform(), 'win-amd64')
  107. # windows XP, itanium
  108. os.name = 'nt'
  109. sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
  110. '[MSC v.1310 32 bit (Itanium)]')
  111. sys.platform = 'win32'
  112. self.assertEqual(get_platform(), 'win-ia64')
  113. # macbook
  114. os.name = 'posix'
  115. sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
  116. '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
  117. sys.platform = 'darwin'
  118. self._set_uname(('Darwin', 'macziade', '8.11.1',
  119. ('Darwin Kernel Version 8.11.1: '
  120. 'Wed Oct 10 18:23:28 PDT 2007; '
  121. 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
  122. _osx_support._remove_original_values(get_config_vars())
  123. get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
  124. get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
  125. '-fwrapv -O3 -Wall -Wstrict-prototypes')
  126. maxint = sys.maxint
  127. try:
  128. sys.maxint = 2147483647
  129. self.assertEqual(get_platform(), 'macosx-10.3-ppc')
  130. sys.maxint = 9223372036854775807
  131. self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
  132. finally:
  133. sys.maxint = maxint
  134. self._set_uname(('Darwin', 'macziade', '8.11.1',
  135. ('Darwin Kernel Version 8.11.1: '
  136. 'Wed Oct 10 18:23:28 PDT 2007; '
  137. 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
  138. _osx_support._remove_original_values(get_config_vars())
  139. get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
  140. get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
  141. '-fwrapv -O3 -Wall -Wstrict-prototypes')
  142. maxint = sys.maxint
  143. try:
  144. sys.maxint = 2147483647
  145. self.assertEqual(get_platform(), 'macosx-10.3-i386')
  146. sys.maxint = 9223372036854775807
  147. self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
  148. finally:
  149. sys.maxint = maxint
  150. # macbook with fat binaries (fat, universal or fat64)
  151. _osx_support._remove_original_values(get_config_vars())
  152. get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
  153. get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
  154. '/Developer/SDKs/MacOSX10.4u.sdk '
  155. '-fno-strict-aliasing -fno-common '
  156. '-dynamic -DNDEBUG -g -O3')
  157. self.assertEqual(get_platform(), 'macosx-10.4-fat')
  158. _osx_support._remove_original_values(get_config_vars())
  159. get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
  160. '/Developer/SDKs/MacOSX10.4u.sdk '
  161. '-fno-strict-aliasing -fno-common '
  162. '-dynamic -DNDEBUG -g -O3')
  163. self.assertEqual(get_platform(), 'macosx-10.4-intel')
  164. _osx_support._remove_original_values(get_config_vars())
  165. get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
  166. '/Developer/SDKs/MacOSX10.4u.sdk '
  167. '-fno-strict-aliasing -fno-common '
  168. '-dynamic -DNDEBUG -g -O3')
  169. self.assertEqual(get_platform(), 'macosx-10.4-fat3')
  170. _osx_support._remove_original_values(get_config_vars())
  171. get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
  172. '/Developer/SDKs/MacOSX10.4u.sdk '
  173. '-fno-strict-aliasing -fno-common '
  174. '-dynamic -DNDEBUG -g -O3')
  175. self.assertEqual(get_platform(), 'macosx-10.4-universal')
  176. _osx_support._remove_original_values(get_config_vars())
  177. get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
  178. '/Developer/SDKs/MacOSX10.4u.sdk '
  179. '-fno-strict-aliasing -fno-common '
  180. '-dynamic -DNDEBUG -g -O3')
  181. self.assertEqual(get_platform(), 'macosx-10.4-fat64')
  182. for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
  183. _osx_support._remove_original_values(get_config_vars())
  184. get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
  185. '/Developer/SDKs/MacOSX10.4u.sdk '
  186. '-fno-strict-aliasing -fno-common '
  187. '-dynamic -DNDEBUG -g -O3'%(arch,))
  188. self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
  189. # linux debian sarge
  190. os.name = 'posix'
  191. sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
  192. '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
  193. sys.platform = 'linux2'
  194. self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
  195. '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
  196. self.assertEqual(get_platform(), 'linux-i686')
  197. # XXX more platforms to tests here
  198. def test_get_config_h_filename(self):
  199. config_h = sysconfig.get_config_h_filename()
  200. self.assertTrue(os.path.isfile(config_h), config_h)
  201. def test_get_scheme_names(self):
  202. wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
  203. 'posix_home', 'posix_prefix', 'posix_user')
  204. self.assertEqual(get_scheme_names(), wanted)
  205. @unittest.skipIf(sys.platform.startswith('win'),
  206. 'Test is not Windows compatible')
  207. def test_get_makefile_filename(self):
  208. makefile = sysconfig.get_makefile_filename()
  209. self.assertTrue(os.path.isfile(makefile), makefile)
  210. # Issue 22199
  211. self.assertEqual(sysconfig._get_makefile_filename(), makefile)
  212. def test_symlink(self):
  213. # Issue 7880
  214. symlink = get_attribute(os, "symlink")
  215. def get(python):
  216. cmd = [python, '-c',
  217. 'import sysconfig; print sysconfig.get_platform()']
  218. p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
  219. return p.communicate()
  220. real = os.path.realpath(sys.executable)
  221. link = os.path.abspath(TESTFN)
  222. symlink(real, link)
  223. try:
  224. self.assertEqual(get(real), get(link))
  225. finally:
  226. unlink(link)
  227. def test_user_similar(self):
  228. # Issue #8759: make sure the posix scheme for the users
  229. # is similar to the global posix_prefix one
  230. base = get_config_var('base')
  231. user = get_config_var('userbase')
  232. # the global scheme mirrors the distinction between prefix and
  233. # exec-prefix but not the user scheme, so we have to adapt the paths
  234. # before comparing (issue #9100)
  235. adapt = sys.prefix != sys.exec_prefix
  236. for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
  237. global_path = get_path(name, 'posix_prefix')
  238. if adapt:
  239. global_path = global_path.replace(sys.exec_prefix, sys.prefix)
  240. base = base.replace(sys.exec_prefix, sys.prefix)
  241. user_path = get_path(name, 'posix_user')
  242. self.assertEqual(user_path, global_path.replace(base, user, 1))
  243. @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
  244. def test_platform_in_subprocess(self):
  245. my_platform = sysconfig.get_platform()
  246. # Test without MACOSX_DEPLOYMENT_TARGET in the environment
  247. env = os.environ.copy()
  248. if 'MACOSX_DEPLOYMENT_TARGET' in env:
  249. del env['MACOSX_DEPLOYMENT_TARGET']
  250. with open('/dev/null', 'w') as devnull_fp:
  251. p = subprocess.Popen([
  252. sys.executable, '-c',
  253. 'import sysconfig; print(sysconfig.get_platform())',
  254. ],
  255. stdout=subprocess.PIPE,
  256. stderr=devnull_fp,
  257. env=env)
  258. test_platform = p.communicate()[0].strip()
  259. test_platform = test_platform.decode('utf-8')
  260. status = p.wait()
  261. self.assertEqual(status, 0)
  262. self.assertEqual(my_platform, test_platform)
  263. # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
  264. # using a value that is unlikely to be the default one.
  265. env = os.environ.copy()
  266. env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
  267. p = subprocess.Popen([
  268. sys.executable, '-c',
  269. 'import sysconfig; print(sysconfig.get_platform())',
  270. ],
  271. stdout=subprocess.PIPE,
  272. stderr=open('/dev/null'),
  273. env=env)
  274. test_platform = p.communicate()[0].strip()
  275. test_platform = test_platform.decode('utf-8')
  276. status = p.wait()
  277. self.assertEqual(status, 0)
  278. self.assertEqual(my_platform, test_platform)
  279. def test_main():
  280. run_unittest(TestSysConfig)
  281. if __name__ == "__main__":
  282. test_main()