blob: 57d1f55fc4b5a87ea3ca7a0e7d4c3bf3ea525d4f [file] [log] [blame]
Tarek Ziadé5633a802010-01-23 09:23:15 +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é5633a802010-01-23 09:23:15 +00009import os
Georg Brandld1fa76e2010-02-06 23:12:12 +000010import shutil
Florent Xicluna85677612010-03-10 23:58:42 +000011import subprocess
Tarek Ziadé5633a802010-01-23 09:23:15 +000012from copy import copy, deepcopy
13
Florent Xicluna85677612010-03-10 23:58:42 +000014from test.test_support import run_unittest, TESTFN, unlink, get_attribute
Tarek Ziadé5633a802010-01-23 09:23:15 +000015
16import sysconfig
17from sysconfig import (get_paths, get_platform, get_config_vars,
18 get_path, get_path_names, _INSTALL_SCHEMES,
Tarek Ziadée81b0282010-02-02 22:54:28 +000019 _get_default_scheme, _expand_vars,
Tarek Ziadé8f692272010-05-19 22:20:14 +000020 get_scheme_names, get_config_var)
Tarek Ziadé5633a802010-01-23 09:23:15 +000021
22class TestSysConfig(unittest.TestCase):
23
24 def setUp(self):
25 """Make a copy of sys.path"""
26 super(TestSysConfig, self).setUp()
27 self.sys_path = sys.path[:]
28 self.makefile = None
29 # patching os.uname
30 if hasattr(os, 'uname'):
31 self.uname = os.uname
32 self._uname = os.uname()
33 else:
34 self.uname = None
35 self._uname = None
36 os.uname = self._get_uname
37 # saving the environment
38 self.name = os.name
39 self.platform = sys.platform
40 self.version = sys.version
41 self.sep = os.sep
42 self.join = os.path.join
43 self.isabs = os.path.isabs
44 self.splitdrive = os.path.splitdrive
45 self._config_vars = copy(sysconfig._CONFIG_VARS)
46 self.old_environ = deepcopy(os.environ)
47
48 def tearDown(self):
49 """Restore sys.path"""
50 sys.path[:] = self.sys_path
51 if self.makefile is not None:
52 os.unlink(self.makefile)
53 self._cleanup_testfn()
54 if self.uname is not None:
55 os.uname = self.uname
56 else:
57 del os.uname
58 os.name = self.name
59 sys.platform = self.platform
60 sys.version = self.version
61 os.sep = self.sep
62 os.path.join = self.join
63 os.path.isabs = self.isabs
64 os.path.splitdrive = self.splitdrive
65 sysconfig._CONFIG_VARS = copy(self._config_vars)
66 for key, value in self.old_environ.items():
67 if os.environ.get(key) != value:
68 os.environ[key] = value
69
70 for key in os.environ.keys():
71 if key not in self.old_environ:
72 del os.environ[key]
73
74 super(TestSysConfig, self).tearDown()
75
76 def _set_uname(self, uname):
77 self._uname = uname
78
79 def _get_uname(self):
80 return self._uname
81
82 def _cleanup_testfn(self):
Georg Brandla4f46e12010-02-07 17:03:15 +000083 path = TESTFN
Tarek Ziadé5633a802010-01-23 09:23:15 +000084 if os.path.isfile(path):
85 os.remove(path)
86 elif os.path.isdir(path):
87 shutil.rmtree(path)
88
89 def test_get_path_names(self):
Ezio Melotti2623a372010-11-21 13:34:58 +000090 self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
Tarek Ziadé5633a802010-01-23 09:23:15 +000091
92 def test_get_paths(self):
93 scheme = get_paths()
94 default_scheme = _get_default_scheme()
95 wanted = _expand_vars(default_scheme, None)
96 wanted = wanted.items()
97 wanted.sort()
98 scheme = scheme.items()
99 scheme.sort()
Ezio Melotti2623a372010-11-21 13:34:58 +0000100 self.assertEqual(scheme, wanted)
Tarek Ziadé5633a802010-01-23 09:23:15 +0000101
102 def test_get_path(self):
103 # xxx make real tests here
104 for scheme in _INSTALL_SCHEMES:
105 for name in _INSTALL_SCHEMES[scheme]:
106 res = get_path(name, scheme)
107
108 def test_get_config_vars(self):
109 cvars = get_config_vars()
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000110 self.assertIsInstance(cvars, dict)
Tarek Ziadé5633a802010-01-23 09:23:15 +0000111 self.assertTrue(cvars)
112
113 def test_get_platform(self):
114 # windows XP, 32bits
115 os.name = 'nt'
116 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
117 '[MSC v.1310 32 bit (Intel)]')
118 sys.platform = 'win32'
Ezio Melotti2623a372010-11-21 13:34:58 +0000119 self.assertEqual(get_platform(), 'win32')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000120
121 # windows XP, amd64
122 os.name = 'nt'
123 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
124 '[MSC v.1310 32 bit (Amd64)]')
125 sys.platform = 'win32'
Ezio Melotti2623a372010-11-21 13:34:58 +0000126 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000127
128 # windows XP, itanium
129 os.name = 'nt'
130 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
131 '[MSC v.1310 32 bit (Itanium)]')
132 sys.platform = 'win32'
Ezio Melotti2623a372010-11-21 13:34:58 +0000133 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000134
135 # macbook
136 os.name = 'posix'
137 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
138 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
139 sys.platform = 'darwin'
140 self._set_uname(('Darwin', 'macziade', '8.11.1',
141 ('Darwin Kernel Version 8.11.1: '
142 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000143 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ronald Oussorena70286b2011-05-15 16:44:27 +0200144 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000145
146 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
147 '-fwrapv -O3 -Wall -Wstrict-prototypes')
148
149 maxint = sys.maxint
150 try:
151 sys.maxint = 2147483647
Ezio Melotti2623a372010-11-21 13:34:58 +0000152 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000153 sys.maxint = 9223372036854775807
Ezio Melotti2623a372010-11-21 13:34:58 +0000154 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000155 finally:
156 sys.maxint = maxint
157
158
159 self._set_uname(('Darwin', 'macziade', '8.11.1',
160 ('Darwin Kernel Version 8.11.1: '
161 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé5633a802010-01-23 09:23:15 +0000162 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
163 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé5633a802010-01-23 09:23:15 +0000164
165 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
166 '-fwrapv -O3 -Wall -Wstrict-prototypes')
167
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000168 maxint = sys.maxint
169 try:
170 sys.maxint = 2147483647
Ezio Melotti2623a372010-11-21 13:34:58 +0000171 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000172 sys.maxint = 9223372036854775807
Ezio Melotti2623a372010-11-21 13:34:58 +0000173 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000174 finally:
175 sys.maxint = maxint
Tarek Ziadé5633a802010-01-23 09:23:15 +0000176
177 # macbook with fat binaries (fat, universal or fat64)
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
186 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
187 '/Developer/SDKs/MacOSX10.4u.sdk '
188 '-fno-strict-aliasing -fno-common '
189 '-dynamic -DNDEBUG -g -O3')
190
Ezio Melotti2623a372010-11-21 13:34:58 +0000191 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000192
193 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
194 '/Developer/SDKs/MacOSX10.4u.sdk '
195 '-fno-strict-aliasing -fno-common '
196 '-dynamic -DNDEBUG -g -O3')
Ezio Melotti2623a372010-11-21 13:34:58 +0000197 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000198
199 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
200 '/Developer/SDKs/MacOSX10.4u.sdk '
201 '-fno-strict-aliasing -fno-common '
202 '-dynamic -DNDEBUG -g -O3')
Ezio Melotti2623a372010-11-21 13:34:58 +0000203 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000204
205 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
206 '/Developer/SDKs/MacOSX10.4u.sdk '
207 '-fno-strict-aliasing -fno-common '
208 '-dynamic -DNDEBUG -g -O3')
209
Ezio Melotti2623a372010-11-21 13:34:58 +0000210 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000211
212 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
213 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
214 '/Developer/SDKs/MacOSX10.4u.sdk '
215 '-fno-strict-aliasing -fno-common '
216 '-dynamic -DNDEBUG -g -O3'%(arch,))
217
Ezio Melotti2623a372010-11-21 13:34:58 +0000218 self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
Tarek Ziadé5633a802010-01-23 09:23:15 +0000219
220 # linux debian sarge
221 os.name = 'posix'
222 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
223 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
224 sys.platform = 'linux2'
225 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
226 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
227
Ezio Melotti2623a372010-11-21 13:34:58 +0000228 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000229
230 # XXX more platforms to tests here
231
232 def test_get_config_h_filename(self):
233 config_h = sysconfig.get_config_h_filename()
234 self.assertTrue(os.path.isfile(config_h), config_h)
235
Tarek Ziadée81b0282010-02-02 22:54:28 +0000236 def test_get_scheme_names(self):
Benjamin Peterson7baf8622010-05-08 15:42:29 +0000237 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
Benjamin Petersonac896ed2010-05-08 15:41:44 +0000238 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melotti2623a372010-11-21 13:34:58 +0000239 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadée81b0282010-02-02 22:54:28 +0000240
Florent Xicluna85677612010-03-10 23:58:42 +0000241 def test_symlink(self):
242 # Issue 7880
243 symlink = get_attribute(os, "symlink")
244 def get(python):
245 cmd = [python, '-c',
246 'import sysconfig; print sysconfig.get_platform()']
Florent Xicluna6602ec62010-03-11 01:39:55 +0000247 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
Florent Xicluna85677612010-03-10 23:58:42 +0000248 return p.communicate()
249 real = os.path.realpath(sys.executable)
250 link = os.path.abspath(TESTFN)
251 symlink(real, link)
252 try:
253 self.assertEqual(get(real), get(link))
254 finally:
255 unlink(link)
256
Tarek Ziadé8f692272010-05-19 22:20:14 +0000257 def test_user_similar(self):
Éric Araujoa971df32011-08-31 16:48:17 +0200258 # Issue #8759: make sure the posix scheme for the users
Tarek Ziadé8f692272010-05-19 22:20:14 +0000259 # is similar to the global posix_prefix one
260 base = get_config_var('base')
261 user = get_config_var('userbase')
262 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
263 global_path = get_path(name, 'posix_prefix')
264 user_path = get_path(name, 'posix_user')
Éric Araujoa971df32011-08-31 16:48:17 +0200265 self.assertEqual(user_path, global_path.replace(base, user, 1))
Tarek Ziadé5633a802010-01-23 09:23:15 +0000266
Ronald Oussorena70286b2011-05-15 16:44:27 +0200267 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
268 def test_platform_in_subprocess(self):
269 my_platform = sysconfig.get_platform()
270
271 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
272
273 env = os.environ.copy()
274 if 'MACOSX_DEPLOYMENT_TARGET' in env:
275 del env['MACOSX_DEPLOYMENT_TARGET']
276
277 with open('/dev/null', 'w') as devnull_fp:
278 p = subprocess.Popen([
279 sys.executable, '-c',
280 'import sysconfig; print(sysconfig.get_platform())',
281 ],
282 stdout=subprocess.PIPE,
283 stderr=devnull_fp,
284 env=env)
285 test_platform = p.communicate()[0].strip()
286 test_platform = test_platform.decode('utf-8')
287 status = p.wait()
288
289 self.assertEqual(status, 0)
290 self.assertEqual(my_platform, test_platform)
291
292
293 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
294 # using a value that is unlikely to be the default one.
295 env = os.environ.copy()
296 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
297
298 p = subprocess.Popen([
299 sys.executable, '-c',
300 'import sysconfig; print(sysconfig.get_platform())',
301 ],
302 stdout=subprocess.PIPE,
303 stderr=open('/dev/null'),
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
Tarek Ziadé5633a802010-01-23 09:23:15 +0000312def test_main():
313 run_unittest(TestSysConfig)
314
315if __name__ == "__main__":
316 test_main()