blob: a97b38848ec38c0e6167c8f23b8248967a660255 [file] [log] [blame]
Tarek Ziadéedacea32010-01-29 11:41:03 +00001"""Tests for 'site'.
2
3Tests assume the initial paths in sys.path once the interpreter has begun
4executing have not been removed.
5
6"""
7import unittest
8import sys
Tarek Ziadéedacea32010-01-29 11:41:03 +00009import os
Florent Xiclunaa4707382010-03-11 00:05:17 +000010import subprocess
Georg Brandl89fad142010-03-14 10:23:39 +000011import shutil
Tarek Ziadéedacea32010-01-29 11:41:03 +000012from copy import copy, deepcopy
13
Tarek Ziadéa7514992010-05-25 09:44:36 +000014from test.support import (run_unittest, TESTFN, unlink, get_attribute,
Brian Curtin3b4499c2010-12-28 14:31:47 +000015 captured_stdout, skip_unless_symlink)
Tarek Ziadéedacea32010-01-29 11:41:03 +000016
17import sysconfig
18from sysconfig import (get_paths, get_platform, get_config_vars,
19 get_path, get_path_names, _INSTALL_SCHEMES,
Tarek Ziadébd797682010-02-02 23:16:13 +000020 _get_default_scheme, _expand_vars,
Tarek Ziadéa7514992010-05-25 09:44:36 +000021 get_scheme_names, get_config_var, _main)
Tarek Ziadéedacea32010-01-29 11:41:03 +000022
23class TestSysConfig(unittest.TestCase):
24
25 def setUp(self):
26 """Make a copy of sys.path"""
27 super(TestSysConfig, self).setUp()
28 self.sys_path = sys.path[:]
29 self.makefile = None
30 # patching os.uname
31 if hasattr(os, 'uname'):
32 self.uname = os.uname
33 self._uname = os.uname()
34 else:
35 self.uname = None
36 self._uname = None
37 os.uname = self._get_uname
38 # saving the environment
39 self.name = os.name
40 self.platform = sys.platform
41 self.version = sys.version
42 self.sep = os.sep
43 self.join = os.path.join
44 self.isabs = os.path.isabs
45 self.splitdrive = os.path.splitdrive
46 self._config_vars = copy(sysconfig._CONFIG_VARS)
47 self.old_environ = deepcopy(os.environ)
48
49 def tearDown(self):
50 """Restore sys.path"""
51 sys.path[:] = self.sys_path
52 if self.makefile is not None:
53 os.unlink(self.makefile)
54 self._cleanup_testfn()
55 if self.uname is not None:
56 os.uname = self.uname
57 else:
58 del os.uname
59 os.name = self.name
60 sys.platform = self.platform
61 sys.version = self.version
62 os.sep = self.sep
63 os.path.join = self.join
64 os.path.isabs = self.isabs
65 os.path.splitdrive = self.splitdrive
66 sysconfig._CONFIG_VARS = copy(self._config_vars)
67 for key, value in self.old_environ.items():
68 if os.environ.get(key) != value:
69 os.environ[key] = value
70
71 for key in list(os.environ.keys()):
72 if key not in self.old_environ:
73 del os.environ[key]
74
75 super(TestSysConfig, self).tearDown()
76
77 def _set_uname(self, uname):
78 self._uname = uname
79
80 def _get_uname(self):
81 return self._uname
82
83 def _cleanup_testfn(self):
84 path = TESTFN
85 if os.path.isfile(path):
86 os.remove(path)
87 elif os.path.isdir(path):
88 shutil.rmtree(path)
89
90 def test_get_path_names(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000091 self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
Tarek Ziadéedacea32010-01-29 11:41:03 +000092
93 def test_get_paths(self):
94 scheme = get_paths()
95 default_scheme = _get_default_scheme()
96 wanted = _expand_vars(default_scheme, None)
97 wanted = list(wanted.items())
98 wanted.sort()
99 scheme = list(scheme.items())
100 scheme.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000101 self.assertEqual(scheme, wanted)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000102
103 def test_get_path(self):
104 # xxx make real tests here
105 for scheme in _INSTALL_SCHEMES:
106 for name in _INSTALL_SCHEMES[scheme]:
107 res = get_path(name, scheme)
108
109 def test_get_config_vars(self):
110 cvars = get_config_vars()
111 self.assertTrue(isinstance(cvars, dict))
112 self.assertTrue(cvars)
113
114 def test_get_platform(self):
115 # windows XP, 32bits
116 os.name = 'nt'
117 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
118 '[MSC v.1310 32 bit (Intel)]')
119 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000120 self.assertEqual(get_platform(), 'win32')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000121
122 # windows XP, amd64
123 os.name = 'nt'
124 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
125 '[MSC v.1310 32 bit (Amd64)]')
126 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000127 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000128
129 # windows XP, itanium
130 os.name = 'nt'
131 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
132 '[MSC v.1310 32 bit (Itanium)]')
133 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000134 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000135
136 # macbook
137 os.name = 'posix'
138 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
139 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
140 sys.platform = 'darwin'
141 self._set_uname(('Darwin', 'macziade', '8.11.1',
142 ('Darwin Kernel Version 8.11.1: '
143 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000144 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200145
146
147 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000148
149 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
150 '-fwrapv -O3 -Wall -Wstrict-prototypes')
151
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000152 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000153 try:
154 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000155 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000156 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000157 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000158 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000159 sys.maxsize = maxint
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000160
161
162 self._set_uname(('Darwin', 'macziade', '8.11.1',
163 ('Darwin Kernel Version 8.11.1: '
164 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéedacea32010-01-29 11:41:03 +0000165 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
166 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200167 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000168
169 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
170 '-fwrapv -O3 -Wall -Wstrict-prototypes')
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000171 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000172 try:
173 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000174 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000175 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000176 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000177 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000178 sys.maxsize = maxint
Tarek Ziadéedacea32010-01-29 11:41:03 +0000179
180 # macbook with fat binaries (fat, universal or fat64)
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200181 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000182 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
183 '/Developer/SDKs/MacOSX10.4u.sdk '
184 '-fno-strict-aliasing -fno-common '
185 '-dynamic -DNDEBUG -g -O3')
186
Ezio Melottib3aedd42010-11-20 19:04:17 +0000187 self.assertEqual(get_platform(), 'macosx-10.4-fat')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000188
189 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
190 '/Developer/SDKs/MacOSX10.4u.sdk '
191 '-fno-strict-aliasing -fno-common '
192 '-dynamic -DNDEBUG -g -O3')
193
Ezio Melottib3aedd42010-11-20 19:04:17 +0000194 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000195
196 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
197 '/Developer/SDKs/MacOSX10.4u.sdk '
198 '-fno-strict-aliasing -fno-common '
199 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000200 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000201
202 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 Melottib3aedd42010-11-20 19:04:17 +0000206 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000207
208 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
209 '/Developer/SDKs/MacOSX10.4u.sdk '
210 '-fno-strict-aliasing -fno-common '
211 '-dynamic -DNDEBUG -g -O3')
212
Ezio Melottib3aedd42010-11-20 19:04:17 +0000213 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000214
215 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
216 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
217 '/Developer/SDKs/MacOSX10.4u.sdk '
218 '-fno-strict-aliasing -fno-common '
219 '-dynamic -DNDEBUG -g -O3'%(arch,))
220
Ezio Melottib3aedd42010-11-20 19:04:17 +0000221 self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000222
223 # linux debian sarge
224 os.name = 'posix'
225 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
226 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
227 sys.platform = 'linux2'
228 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
229 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
230
Ezio Melottib3aedd42010-11-20 19:04:17 +0000231 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000232
233 # XXX more platforms to tests here
234
235 def test_get_config_h_filename(self):
236 config_h = sysconfig.get_config_h_filename()
237 self.assertTrue(os.path.isfile(config_h), config_h)
238
Barry Warsaw0646b4b2010-09-20 19:12:07 +0000239 @unittest.skipIf(sys.platform.startswith('win'),
240 'Test is not Windows compatible')
Barry Warsawebbef6f2010-09-20 15:29:53 +0000241 def test_get_makefile_filename(self):
242 makefile = sysconfig.get_makefile_filename()
243 self.assertTrue(os.path.isfile(makefile), makefile)
244
Tarek Ziadébd797682010-02-02 23:16:13 +0000245 def test_get_scheme_names(self):
Benjamin Peterson3c451e62010-05-08 15:51:23 +0000246 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
247 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000248 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadébd797682010-02-02 23:16:13 +0000249
Brian Curtin3b4499c2010-12-28 14:31:47 +0000250 @skip_unless_symlink
Florent Xiclunaa4707382010-03-11 00:05:17 +0000251 def test_symlink(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000252 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
253 # to add the directory to the path.
254 if sys.platform == "win32":
Brian Curtin74e45612010-07-09 15:58:59 +0000255 os.environ["Path"] = "{};{}".format(
256 os.path.dirname(sys.executable), os.environ["Path"])
Brian Curtind40e6f72010-07-08 21:39:08 +0000257
Florent Xiclunaa4707382010-03-11 00:05:17 +0000258 # Issue 7880
Florent Xiclunaa4707382010-03-11 00:05:17 +0000259 def get(python):
260 cmd = [python, '-c',
Florent Xicluna01fe6102010-03-13 15:35:12 +0000261 'import sysconfig; print(sysconfig.get_platform())']
Brian Curtind40e6f72010-07-08 21:39:08 +0000262 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000263 return p.communicate()
264 real = os.path.realpath(sys.executable)
265 link = os.path.abspath(TESTFN)
Brian Curtind40e6f72010-07-08 21:39:08 +0000266 os.symlink(real, link)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000267 try:
268 self.assertEqual(get(real), get(link))
269 finally:
270 unlink(link)
271
Tarek Ziadé06710a82010-05-19 22:25:00 +0000272 def test_user_similar(self):
273 # Issue 8759 : make sure the posix scheme for the users
274 # is similar to the global posix_prefix one
275 base = get_config_var('base')
276 user = get_config_var('userbase')
277 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
278 global_path = get_path(name, 'posix_prefix')
279 user_path = get_path(name, 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000280 self.assertEqual(user_path, global_path.replace(base, user))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000281
Tarek Ziadéa7514992010-05-25 09:44:36 +0000282 def test_main(self):
283 # just making sure _main() runs and returns things in the stdout
284 with captured_stdout() as output:
285 _main()
286 self.assertTrue(len(output.getvalue().split('\n')) > 0)
287
Brian Curtindb902ac2010-07-22 15:38:28 +0000288 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows")
Ronald Oussorenf4ebe2e2010-07-19 13:00:36 +0000289 def test_ldshared_value(self):
290 ldflags = sysconfig.get_config_var('LDFLAGS')
291 ldshared = sysconfig.get_config_var('LDSHARED')
292
293 self.assertIn(ldflags, ldshared)
294
Tarek Ziadéa7514992010-05-25 09:44:36 +0000295
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200296 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
297 def test_platform_in_subprocess(self):
298 my_platform = sysconfig.get_platform()
299
300 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
301
302 env = os.environ.copy()
303 if 'MACOSX_DEPLOYMENT_TARGET' in env:
304 del env['MACOSX_DEPLOYMENT_TARGET']
305
306 with open('/dev/null', 'w') as devnull_fp:
307 p = subprocess.Popen([
308 sys.executable, '-c',
309 'import sysconfig; print(sysconfig.get_platform())',
310 ],
311 stdout=subprocess.PIPE,
312 stderr=devnull_fp,
313 env=env)
314 test_platform = p.communicate()[0].strip()
315 test_platform = test_platform.decode('utf-8')
316 status = p.wait()
317
318 self.assertEqual(status, 0)
319 self.assertEqual(my_platform, test_platform)
320
321
322 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
323 # using a value that is unlikely to be the default one.
324 env = os.environ.copy()
325 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
326
327 p = subprocess.Popen([
328 sys.executable, '-c',
329 'import sysconfig; print(sysconfig.get_platform())',
330 ],
331 stdout=subprocess.PIPE,
332 stderr=open('/dev/null'),
333 env=env)
334 test_platform = p.communicate()[0].strip()
335 test_platform = test_platform.decode('utf-8')
336 status = p.wait()
337
338 self.assertEqual(status, 0)
339 self.assertEqual(my_platform, test_platform)
340
341
342
343
Tarek Ziadéedacea32010-01-29 11:41:03 +0000344def test_main():
345 run_unittest(TestSysConfig)
346
347if __name__ == "__main__":
348 test_main()