blob: 214fc704f03ae9c65184e379f57bef90035d0c1d [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,
Tarek Ziade1231a4e2011-05-19 13:07:25 +020019 get_path, get_path_names, _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)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020047 self._added_envvars = []
48 self._changed_envvars = []
49 for var in ('MACOSX_DEPLOYMENT_TARGET', 'Path'):
50 if var in os.environ:
51 self._changed_envvars.append((var, os.environ[var]))
52 else:
53 self._added_envvars.append(var)
Tarek Ziadéedacea32010-01-29 11:41:03 +000054
55 def tearDown(self):
56 """Restore sys.path"""
57 sys.path[:] = self.sys_path
58 if self.makefile is not None:
59 os.unlink(self.makefile)
60 self._cleanup_testfn()
61 if self.uname is not None:
62 os.uname = self.uname
63 else:
64 del os.uname
65 os.name = self.name
66 sys.platform = self.platform
67 sys.version = self.version
68 os.sep = self.sep
69 os.path.join = self.join
70 os.path.isabs = self.isabs
71 os.path.splitdrive = self.splitdrive
72 sysconfig._CONFIG_VARS = copy(self._config_vars)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020073 for var, value in self._changed_envvars:
74 os.environ[var] = value
75 for var in self._added_envvars:
76 os.environ.pop(var, None)
Tarek Ziadéedacea32010-01-29 11:41:03 +000077
78 super(TestSysConfig, self).tearDown()
79
80 def _set_uname(self, uname):
81 self._uname = uname
82
83 def _get_uname(self):
84 return self._uname
85
86 def _cleanup_testfn(self):
87 path = TESTFN
88 if os.path.isfile(path):
89 os.remove(path)
90 elif os.path.isdir(path):
91 shutil.rmtree(path)
92
93 def test_get_path_names(self):
Tarek Ziade1231a4e2011-05-19 13:07:25 +020094 self.assertEqual(get_path_names(), _SCHEMES.options('posix_prefix'))
Tarek Ziadéedacea32010-01-29 11:41:03 +000095
96 def test_get_paths(self):
97 scheme = get_paths()
98 default_scheme = _get_default_scheme()
99 wanted = _expand_vars(default_scheme, None)
100 wanted = list(wanted.items())
101 wanted.sort()
102 scheme = list(scheme.items())
103 scheme.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000104 self.assertEqual(scheme, wanted)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000105
106 def test_get_path(self):
107 # xxx make real tests here
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200108 for scheme in _SCHEMES:
109 for name in _SCHEMES[scheme]:
Tarek Ziadéedacea32010-01-29 11:41:03 +0000110 res = get_path(name, scheme)
111
112 def test_get_config_vars(self):
113 cvars = get_config_vars()
114 self.assertTrue(isinstance(cvars, dict))
115 self.assertTrue(cvars)
116
117 def test_get_platform(self):
118 # windows XP, 32bits
119 os.name = 'nt'
120 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
121 '[MSC v.1310 32 bit (Intel)]')
122 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000123 self.assertEqual(get_platform(), 'win32')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000124
125 # windows XP, amd64
126 os.name = 'nt'
127 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
128 '[MSC v.1310 32 bit (Amd64)]')
129 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000130 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000131
132 # windows XP, itanium
133 os.name = 'nt'
134 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
135 '[MSC v.1310 32 bit (Itanium)]')
136 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000137 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000138
139 # macbook
140 os.name = 'posix'
141 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
142 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
143 sys.platform = 'darwin'
144 self._set_uname(('Darwin', 'macziade', '8.11.1',
145 ('Darwin Kernel Version 8.11.1: '
146 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000147 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200148
149
150 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000151
152 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
153 '-fwrapv -O3 -Wall -Wstrict-prototypes')
154
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000155 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000156 try:
157 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000158 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000159 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000160 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000161 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000162 sys.maxsize = maxint
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000163
164
165 self._set_uname(('Darwin', 'macziade', '8.11.1',
166 ('Darwin Kernel Version 8.11.1: '
167 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéedacea32010-01-29 11:41:03 +0000168 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
169 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200170 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000171
172 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
173 '-fwrapv -O3 -Wall -Wstrict-prototypes')
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000174 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000175 try:
176 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000177 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000178 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000179 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000180 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000181 sys.maxsize = maxint
Tarek Ziadéedacea32010-01-29 11:41:03 +0000182
183 # macbook with fat binaries (fat, universal or fat64)
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200184 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000185 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
186 '/Developer/SDKs/MacOSX10.4u.sdk '
187 '-fno-strict-aliasing -fno-common '
188 '-dynamic -DNDEBUG -g -O3')
189
Ezio Melottib3aedd42010-11-20 19:04:17 +0000190 self.assertEqual(get_platform(), 'macosx-10.4-fat')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000191
192 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
193 '/Developer/SDKs/MacOSX10.4u.sdk '
194 '-fno-strict-aliasing -fno-common '
195 '-dynamic -DNDEBUG -g -O3')
196
Ezio Melottib3aedd42010-11-20 19:04:17 +0000197 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000198
199 get_config_vars()['CFLAGS'] = ('-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 Melottib3aedd42010-11-20 19:04:17 +0000203 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000204
205 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
206 '/Developer/SDKs/MacOSX10.4u.sdk '
207 '-fno-strict-aliasing -fno-common '
208 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000209 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000210
211 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
212 '/Developer/SDKs/MacOSX10.4u.sdk '
213 '-fno-strict-aliasing -fno-common '
214 '-dynamic -DNDEBUG -g -O3')
215
Ezio Melottib3aedd42010-11-20 19:04:17 +0000216 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000217
218 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
219 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
220 '/Developer/SDKs/MacOSX10.4u.sdk '
221 '-fno-strict-aliasing -fno-common '
222 '-dynamic -DNDEBUG -g -O3'%(arch,))
223
Ezio Melottib3aedd42010-11-20 19:04:17 +0000224 self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000225
226 # linux debian sarge
227 os.name = 'posix'
228 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
229 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
230 sys.platform = 'linux2'
231 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
232 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
233
Ezio Melottib3aedd42010-11-20 19:04:17 +0000234 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000235
236 # XXX more platforms to tests here
237
238 def test_get_config_h_filename(self):
239 config_h = sysconfig.get_config_h_filename()
240 self.assertTrue(os.path.isfile(config_h), config_h)
241
Barry Warsaw0646b4b2010-09-20 19:12:07 +0000242 @unittest.skipIf(sys.platform.startswith('win'),
243 'Test is not Windows compatible')
Barry Warsawebbef6f2010-09-20 15:29:53 +0000244 def test_get_makefile_filename(self):
245 makefile = sysconfig.get_makefile_filename()
246 self.assertTrue(os.path.isfile(makefile), makefile)
247
Tarek Ziadébd797682010-02-02 23:16:13 +0000248 def test_get_scheme_names(self):
Benjamin Peterson3c451e62010-05-08 15:51:23 +0000249 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
250 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000251 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadébd797682010-02-02 23:16:13 +0000252
Brian Curtin3b4499c2010-12-28 14:31:47 +0000253 @skip_unless_symlink
Florent Xiclunaa4707382010-03-11 00:05:17 +0000254 def test_symlink(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000255 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
256 # to add the directory to the path.
257 if sys.platform == "win32":
Brian Curtin74e45612010-07-09 15:58:59 +0000258 os.environ["Path"] = "{};{}".format(
259 os.path.dirname(sys.executable), os.environ["Path"])
Brian Curtind40e6f72010-07-08 21:39:08 +0000260
Florent Xiclunaa4707382010-03-11 00:05:17 +0000261 # Issue 7880
Florent Xiclunaa4707382010-03-11 00:05:17 +0000262 def get(python):
263 cmd = [python, '-c',
Florent Xicluna01fe6102010-03-13 15:35:12 +0000264 'import sysconfig; print(sysconfig.get_platform())']
Brian Curtind40e6f72010-07-08 21:39:08 +0000265 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000266 return p.communicate()
267 real = os.path.realpath(sys.executable)
268 link = os.path.abspath(TESTFN)
Brian Curtind40e6f72010-07-08 21:39:08 +0000269 os.symlink(real, link)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000270 try:
271 self.assertEqual(get(real), get(link))
272 finally:
273 unlink(link)
274
Tarek Ziadé06710a82010-05-19 22:25:00 +0000275 def test_user_similar(self):
276 # Issue 8759 : make sure the posix scheme for the users
277 # is similar to the global posix_prefix one
278 base = get_config_var('base')
279 user = get_config_var('userbase')
280 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
281 global_path = get_path(name, 'posix_prefix')
282 user_path = get_path(name, 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000283 self.assertEqual(user_path, global_path.replace(base, user))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000284
Tarek Ziadéa7514992010-05-25 09:44:36 +0000285 def test_main(self):
286 # just making sure _main() runs and returns things in the stdout
287 with captured_stdout() as output:
288 _main()
289 self.assertTrue(len(output.getvalue().split('\n')) > 0)
290
Brian Curtindb902ac2010-07-22 15:38:28 +0000291 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows")
Ronald Oussorenf4ebe2e2010-07-19 13:00:36 +0000292 def test_ldshared_value(self):
293 ldflags = sysconfig.get_config_var('LDFLAGS')
294 ldshared = sysconfig.get_config_var('LDSHARED')
295
296 self.assertIn(ldflags, ldshared)
297
Tarek Ziadéa7514992010-05-25 09:44:36 +0000298
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200299 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
300 def test_platform_in_subprocess(self):
301 my_platform = sysconfig.get_platform()
302
303 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
304
305 env = os.environ.copy()
306 if 'MACOSX_DEPLOYMENT_TARGET' in env:
307 del env['MACOSX_DEPLOYMENT_TARGET']
308
309 with open('/dev/null', 'w') as devnull_fp:
310 p = subprocess.Popen([
311 sys.executable, '-c',
312 'import sysconfig; print(sysconfig.get_platform())',
313 ],
314 stdout=subprocess.PIPE,
315 stderr=devnull_fp,
316 env=env)
317 test_platform = p.communicate()[0].strip()
318 test_platform = test_platform.decode('utf-8')
319 status = p.wait()
320
321 self.assertEqual(status, 0)
322 self.assertEqual(my_platform, test_platform)
323
324
325 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
326 # using a value that is unlikely to be the default one.
327 env = os.environ.copy()
328 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
329
330 p = subprocess.Popen([
331 sys.executable, '-c',
332 'import sysconfig; print(sysconfig.get_platform())',
333 ],
334 stdout=subprocess.PIPE,
335 stderr=open('/dev/null'),
336 env=env)
337 test_platform = p.communicate()[0].strip()
338 test_platform = test_platform.decode('utf-8')
339 status = p.wait()
340
341 self.assertEqual(status, 0)
342 self.assertEqual(my_platform, test_platform)
343
344
345
346
Tarek Ziadéedacea32010-01-29 11:41:03 +0000347def test_main():
348 run_unittest(TestSysConfig)
349
350if __name__ == "__main__":
351 test_main()