blob: a0b65dfc10b910b09171cc0b60b22dca81275386 [file] [log] [blame]
Éric Araujo1a1a8a02011-10-08 02:49:12 +02001"""Tests for sysconfig."""
Tarek Ziadé5633a802010-01-23 09:23:15 +00002
Tarek Ziadé5633a802010-01-23 09:23:15 +00003import unittest
4import sys
Tarek Ziadé5633a802010-01-23 09:23:15 +00005import os
Georg Brandld1fa76e2010-02-06 23:12:12 +00006import shutil
Florent Xicluna85677612010-03-10 23:58:42 +00007import subprocess
Tarek Ziadé5633a802010-01-23 09:23:15 +00008from copy import copy, deepcopy
9
Florent Xicluna85677612010-03-10 23:58:42 +000010from test.test_support import run_unittest, TESTFN, unlink, get_attribute
Tarek Ziadé5633a802010-01-23 09:23:15 +000011
12import sysconfig
13from sysconfig import (get_paths, get_platform, get_config_vars,
14 get_path, get_path_names, _INSTALL_SCHEMES,
Tarek Ziadée81b0282010-02-02 22:54:28 +000015 _get_default_scheme, _expand_vars,
Tarek Ziadé8f692272010-05-19 22:20:14 +000016 get_scheme_names, get_config_var)
Ned Deily18fae3f2013-01-31 01:24:55 -080017import _osx_support
Tarek Ziadé5633a802010-01-23 09:23:15 +000018
19class TestSysConfig(unittest.TestCase):
20
21 def setUp(self):
22 """Make a copy of sys.path"""
23 super(TestSysConfig, self).setUp()
24 self.sys_path = sys.path[:]
25 self.makefile = None
26 # patching os.uname
27 if hasattr(os, 'uname'):
28 self.uname = os.uname
29 self._uname = os.uname()
30 else:
31 self.uname = None
32 self._uname = None
33 os.uname = self._get_uname
34 # saving the environment
35 self.name = os.name
36 self.platform = sys.platform
37 self.version = sys.version
38 self.sep = os.sep
39 self.join = os.path.join
40 self.isabs = os.path.isabs
41 self.splitdrive = os.path.splitdrive
42 self._config_vars = copy(sysconfig._CONFIG_VARS)
43 self.old_environ = deepcopy(os.environ)
44
45 def tearDown(self):
46 """Restore sys.path"""
47 sys.path[:] = self.sys_path
48 if self.makefile is not None:
49 os.unlink(self.makefile)
50 self._cleanup_testfn()
51 if self.uname is not None:
52 os.uname = self.uname
53 else:
54 del os.uname
55 os.name = self.name
56 sys.platform = self.platform
57 sys.version = self.version
58 os.sep = self.sep
59 os.path.join = self.join
60 os.path.isabs = self.isabs
61 os.path.splitdrive = self.splitdrive
62 sysconfig._CONFIG_VARS = copy(self._config_vars)
63 for key, value in self.old_environ.items():
64 if os.environ.get(key) != value:
65 os.environ[key] = value
66
67 for key in os.environ.keys():
68 if key not in self.old_environ:
69 del os.environ[key]
70
71 super(TestSysConfig, self).tearDown()
72
73 def _set_uname(self, uname):
74 self._uname = uname
75
76 def _get_uname(self):
77 return self._uname
78
79 def _cleanup_testfn(self):
Georg Brandla4f46e12010-02-07 17:03:15 +000080 path = TESTFN
Tarek Ziadé5633a802010-01-23 09:23:15 +000081 if os.path.isfile(path):
82 os.remove(path)
83 elif os.path.isdir(path):
84 shutil.rmtree(path)
85
86 def test_get_path_names(self):
Ezio Melotti2623a372010-11-21 13:34:58 +000087 self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
Tarek Ziadé5633a802010-01-23 09:23:15 +000088
89 def test_get_paths(self):
90 scheme = get_paths()
91 default_scheme = _get_default_scheme()
92 wanted = _expand_vars(default_scheme, None)
93 wanted = wanted.items()
94 wanted.sort()
95 scheme = scheme.items()
96 scheme.sort()
Ezio Melotti2623a372010-11-21 13:34:58 +000097 self.assertEqual(scheme, wanted)
Tarek Ziadé5633a802010-01-23 09:23:15 +000098
99 def test_get_path(self):
100 # xxx make real tests here
101 for scheme in _INSTALL_SCHEMES:
102 for name in _INSTALL_SCHEMES[scheme]:
103 res = get_path(name, scheme)
104
105 def test_get_config_vars(self):
106 cvars = get_config_vars()
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000107 self.assertIsInstance(cvars, dict)
Tarek Ziadé5633a802010-01-23 09:23:15 +0000108 self.assertTrue(cvars)
109
110 def test_get_platform(self):
111 # windows XP, 32bits
112 os.name = 'nt'
113 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
114 '[MSC v.1310 32 bit (Intel)]')
115 sys.platform = 'win32'
Ezio Melotti2623a372010-11-21 13:34:58 +0000116 self.assertEqual(get_platform(), 'win32')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000117
118 # windows XP, amd64
119 os.name = 'nt'
120 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
121 '[MSC v.1310 32 bit (Amd64)]')
122 sys.platform = 'win32'
Ezio Melotti2623a372010-11-21 13:34:58 +0000123 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000124
125 # windows XP, itanium
126 os.name = 'nt'
127 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
128 '[MSC v.1310 32 bit (Itanium)]')
129 sys.platform = 'win32'
Ezio Melotti2623a372010-11-21 13:34:58 +0000130 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000131
132 # macbook
133 os.name = 'posix'
134 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
135 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
136 sys.platform = 'darwin'
137 self._set_uname(('Darwin', 'macziade', '8.11.1',
138 ('Darwin Kernel Version 8.11.1: '
139 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000140 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ned Deily18fae3f2013-01-31 01:24:55 -0800141 _osx_support._remove_original_values(get_config_vars())
Ronald Oussorena70286b2011-05-15 16:44:27 +0200142 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000143
144 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
145 '-fwrapv -O3 -Wall -Wstrict-prototypes')
146
147 maxint = sys.maxint
148 try:
149 sys.maxint = 2147483647
Ezio Melotti2623a372010-11-21 13:34:58 +0000150 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000151 sys.maxint = 9223372036854775807
Ezio Melotti2623a372010-11-21 13:34:58 +0000152 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000153 finally:
154 sys.maxint = maxint
155
156
157 self._set_uname(('Darwin', 'macziade', '8.11.1',
158 ('Darwin Kernel Version 8.11.1: '
159 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé5633a802010-01-23 09:23:15 +0000160 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
Ned Deily18fae3f2013-01-31 01:24:55 -0800161 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadé5633a802010-01-23 09:23:15 +0000162 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé5633a802010-01-23 09:23:15 +0000163
164 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
165 '-fwrapv -O3 -Wall -Wstrict-prototypes')
166
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000167 maxint = sys.maxint
168 try:
169 sys.maxint = 2147483647
Ezio Melotti2623a372010-11-21 13:34:58 +0000170 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000171 sys.maxint = 9223372036854775807
Ezio Melotti2623a372010-11-21 13:34:58 +0000172 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000173 finally:
174 sys.maxint = maxint
Tarek Ziadé5633a802010-01-23 09:23:15 +0000175
176 # macbook with fat binaries (fat, universal or fat64)
Ned Deily18fae3f2013-01-31 01:24:55 -0800177 _osx_support._remove_original_values(get_config_vars())
Ronald Oussorena70286b2011-05-15 16:44:27 +0200178 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
Tarek Ziadé5633a802010-01-23 09:23:15 +0000179 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
180 '/Developer/SDKs/MacOSX10.4u.sdk '
181 '-fno-strict-aliasing -fno-common '
182 '-dynamic -DNDEBUG -g -O3')
183
Ezio Melotti2623a372010-11-21 13:34:58 +0000184 self.assertEqual(get_platform(), 'macosx-10.4-fat')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000185
Ned Deily18fae3f2013-01-31 01:24:55 -0800186 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadé5633a802010-01-23 09:23:15 +0000187 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
188 '/Developer/SDKs/MacOSX10.4u.sdk '
189 '-fno-strict-aliasing -fno-common '
190 '-dynamic -DNDEBUG -g -O3')
191
Ezio Melotti2623a372010-11-21 13:34:58 +0000192 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000193
Ned Deily18fae3f2013-01-31 01:24:55 -0800194 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadé5633a802010-01-23 09:23:15 +0000195 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
196 '/Developer/SDKs/MacOSX10.4u.sdk '
197 '-fno-strict-aliasing -fno-common '
198 '-dynamic -DNDEBUG -g -O3')
Ezio Melotti2623a372010-11-21 13:34:58 +0000199 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000200
Ned Deily18fae3f2013-01-31 01:24:55 -0800201 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadé5633a802010-01-23 09:23:15 +0000202 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
203 '/Developer/SDKs/MacOSX10.4u.sdk '
204 '-fno-strict-aliasing -fno-common '
205 '-dynamic -DNDEBUG -g -O3')
Ezio Melotti2623a372010-11-21 13:34:58 +0000206 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000207
Ned Deily18fae3f2013-01-31 01:24:55 -0800208 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadé5633a802010-01-23 09:23:15 +0000209 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
210 '/Developer/SDKs/MacOSX10.4u.sdk '
211 '-fno-strict-aliasing -fno-common '
212 '-dynamic -DNDEBUG -g -O3')
213
Ezio Melotti2623a372010-11-21 13:34:58 +0000214 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000215
216 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
Ned Deily18fae3f2013-01-31 01:24:55 -0800217 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadé5633a802010-01-23 09:23:15 +0000218 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
219 '/Developer/SDKs/MacOSX10.4u.sdk '
220 '-fno-strict-aliasing -fno-common '
221 '-dynamic -DNDEBUG -g -O3'%(arch,))
222
Ezio Melotti2623a372010-11-21 13:34:58 +0000223 self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
Tarek Ziadé5633a802010-01-23 09:23:15 +0000224
225 # linux debian sarge
226 os.name = 'posix'
227 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
228 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
229 sys.platform = 'linux2'
230 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
231 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
232
Ezio Melotti2623a372010-11-21 13:34:58 +0000233 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000234
235 # XXX more platforms to tests here
236
237 def test_get_config_h_filename(self):
238 config_h = sysconfig.get_config_h_filename()
239 self.assertTrue(os.path.isfile(config_h), config_h)
240
Tarek Ziadée81b0282010-02-02 22:54:28 +0000241 def test_get_scheme_names(self):
Benjamin Peterson7baf8622010-05-08 15:42:29 +0000242 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
Benjamin Petersonac896ed2010-05-08 15:41:44 +0000243 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melotti2623a372010-11-21 13:34:58 +0000244 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadée81b0282010-02-02 22:54:28 +0000245
Ned Deily9d6488a2014-08-22 13:48:06 -0700246 @unittest.skipIf(sys.platform.startswith('win'),
247 'Test is not Windows compatible')
248 def test_get_makefile_filename(self):
249 makefile = sysconfig.get_makefile_filename()
250 self.assertTrue(os.path.isfile(makefile), makefile)
251 # Issue 22199
252 self.assertEqual(sysconfig._get_makefile_filename(), makefile)
253
Florent Xicluna85677612010-03-10 23:58:42 +0000254 def test_symlink(self):
255 # Issue 7880
256 symlink = get_attribute(os, "symlink")
257 def get(python):
258 cmd = [python, '-c',
259 'import sysconfig; print sysconfig.get_platform()']
Florent Xicluna6602ec62010-03-11 01:39:55 +0000260 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
Florent Xicluna85677612010-03-10 23:58:42 +0000261 return p.communicate()
262 real = os.path.realpath(sys.executable)
263 link = os.path.abspath(TESTFN)
264 symlink(real, link)
265 try:
266 self.assertEqual(get(real), get(link))
267 finally:
268 unlink(link)
269
Tarek Ziadé8f692272010-05-19 22:20:14 +0000270 def test_user_similar(self):
Éric Araujoa971df32011-08-31 16:48:17 +0200271 # Issue #8759: make sure the posix scheme for the users
Tarek Ziadé8f692272010-05-19 22:20:14 +0000272 # is similar to the global posix_prefix one
273 base = get_config_var('base')
274 user = get_config_var('userbase')
Éric Araujo1a1a8a02011-10-08 02:49:12 +0200275 # the global scheme mirrors the distinction between prefix and
276 # exec-prefix but not the user scheme, so we have to adapt the paths
277 # before comparing (issue #9100)
278 adapt = sys.prefix != sys.exec_prefix
Tarek Ziadé8f692272010-05-19 22:20:14 +0000279 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
280 global_path = get_path(name, 'posix_prefix')
Éric Araujo1a1a8a02011-10-08 02:49:12 +0200281 if adapt:
282 global_path = global_path.replace(sys.exec_prefix, sys.prefix)
283 base = base.replace(sys.exec_prefix, sys.prefix)
Tarek Ziadé8f692272010-05-19 22:20:14 +0000284 user_path = get_path(name, 'posix_user')
Éric Araujoa971df32011-08-31 16:48:17 +0200285 self.assertEqual(user_path, global_path.replace(base, user, 1))
Tarek Ziadé5633a802010-01-23 09:23:15 +0000286
Ronald Oussorena70286b2011-05-15 16:44:27 +0200287 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
288 def test_platform_in_subprocess(self):
289 my_platform = sysconfig.get_platform()
290
291 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
292
293 env = os.environ.copy()
294 if 'MACOSX_DEPLOYMENT_TARGET' in env:
295 del env['MACOSX_DEPLOYMENT_TARGET']
296
Serhiy Storchakae0622432015-02-16 01:49:22 +0200297 with open('/dev/null', 'w') as devnull_fp:
Ronald Oussorena70286b2011-05-15 16:44:27 +0200298 p = subprocess.Popen([
299 sys.executable, '-c',
300 'import sysconfig; print(sysconfig.get_platform())',
301 ],
302 stdout=subprocess.PIPE,
303 stderr=devnull_fp,
304 env=env)
305 test_platform = p.communicate()[0].strip()
306 test_platform = test_platform.decode('utf-8')
307 status = p.wait()
308
309 self.assertEqual(status, 0)
310 self.assertEqual(my_platform, test_platform)
311
312
313 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
314 # using a value that is unlikely to be the default one.
315 env = os.environ.copy()
316 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
317
318 p = subprocess.Popen([
319 sys.executable, '-c',
320 'import sysconfig; print(sysconfig.get_platform())',
321 ],
322 stdout=subprocess.PIPE,
Serhiy Storchakae0622432015-02-16 01:49:22 +0200323 stderr=open('/dev/null'),
Ronald Oussorena70286b2011-05-15 16:44:27 +0200324 env=env)
325 test_platform = p.communicate()[0].strip()
326 test_platform = test_platform.decode('utf-8')
327 status = p.wait()
328
329 self.assertEqual(status, 0)
330 self.assertEqual(my_platform, test_platform)
331
Tarek Ziadé5633a802010-01-23 09:23:15 +0000332def test_main():
333 run_unittest(TestSysConfig)
334
335if __name__ == "__main__":
336 test_main()