blob: 5c84a7574093c872994719239e79e9cc2532c1ab [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[:]
Tarek Ziadéedacea32010-01-29 11:41:03 +000029 # 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)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020046 self._added_envvars = []
47 self._changed_envvars = []
48 for var in ('MACOSX_DEPLOYMENT_TARGET', 'Path'):
49 if var in os.environ:
50 self._changed_envvars.append((var, os.environ[var]))
51 else:
52 self._added_envvars.append(var)
Tarek Ziadéedacea32010-01-29 11:41:03 +000053
54 def tearDown(self):
55 """Restore sys.path"""
56 sys.path[:] = self.sys_path
Tarek Ziadéedacea32010-01-29 11:41:03 +000057 self._cleanup_testfn()
58 if self.uname is not None:
59 os.uname = self.uname
60 else:
61 del os.uname
62 os.name = self.name
63 sys.platform = self.platform
64 sys.version = self.version
65 os.sep = self.sep
66 os.path.join = self.join
67 os.path.isabs = self.isabs
68 os.path.splitdrive = self.splitdrive
69 sysconfig._CONFIG_VARS = copy(self._config_vars)
Tarek Ziade1231a4e2011-05-19 13:07:25 +020070 for var, value in self._changed_envvars:
71 os.environ[var] = value
72 for var in self._added_envvars:
73 os.environ.pop(var, None)
Tarek Ziadéedacea32010-01-29 11:41:03 +000074
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):
Tarek Ziade1231a4e2011-05-19 13:07:25 +020091 self.assertEqual(get_path_names(), _SCHEMES.options('posix_prefix'))
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
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200105 for scheme in _SCHEMES:
106 for name in _SCHEMES[scheme]:
Tarek Ziadéedacea32010-01-29 11:41:03 +0000107 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
Tarek Ziadébd797682010-02-02 23:16:13 +0000239 def test_get_scheme_names(self):
Benjamin Peterson3c451e62010-05-08 15:51:23 +0000240 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
241 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000242 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadébd797682010-02-02 23:16:13 +0000243
Brian Curtin3b4499c2010-12-28 14:31:47 +0000244 @skip_unless_symlink
Florent Xiclunaa4707382010-03-11 00:05:17 +0000245 def test_symlink(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000246 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
247 # to add the directory to the path.
248 if sys.platform == "win32":
Brian Curtin74e45612010-07-09 15:58:59 +0000249 os.environ["Path"] = "{};{}".format(
250 os.path.dirname(sys.executable), os.environ["Path"])
Brian Curtind40e6f72010-07-08 21:39:08 +0000251
Florent Xiclunaa4707382010-03-11 00:05:17 +0000252 # Issue 7880
Florent Xiclunaa4707382010-03-11 00:05:17 +0000253 def get(python):
254 cmd = [python, '-c',
Florent Xicluna01fe6102010-03-13 15:35:12 +0000255 'import sysconfig; print(sysconfig.get_platform())']
Brian Curtind40e6f72010-07-08 21:39:08 +0000256 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000257 return p.communicate()
258 real = os.path.realpath(sys.executable)
259 link = os.path.abspath(TESTFN)
Brian Curtind40e6f72010-07-08 21:39:08 +0000260 os.symlink(real, link)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000261 try:
262 self.assertEqual(get(real), get(link))
263 finally:
264 unlink(link)
265
Tarek Ziadé06710a82010-05-19 22:25:00 +0000266 def test_user_similar(self):
267 # Issue 8759 : make sure the posix scheme for the users
268 # is similar to the global posix_prefix one
269 base = get_config_var('base')
270 user = get_config_var('userbase')
271 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
272 global_path = get_path(name, 'posix_prefix')
273 user_path = get_path(name, 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000274 self.assertEqual(user_path, global_path.replace(base, user))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000275
Tarek Ziadéa7514992010-05-25 09:44:36 +0000276 def test_main(self):
277 # just making sure _main() runs and returns things in the stdout
278 with captured_stdout() as output:
279 _main()
280 self.assertTrue(len(output.getvalue().split('\n')) > 0)
281
Brian Curtindb902ac2010-07-22 15:38:28 +0000282 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows")
Ronald Oussorenf4ebe2e2010-07-19 13:00:36 +0000283 def test_ldshared_value(self):
284 ldflags = sysconfig.get_config_var('LDFLAGS')
285 ldshared = sysconfig.get_config_var('LDSHARED')
286
287 self.assertIn(ldflags, ldshared)
288
Tarek Ziadéa7514992010-05-25 09:44:36 +0000289
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200290 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
291 def test_platform_in_subprocess(self):
292 my_platform = sysconfig.get_platform()
293
294 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
295
296 env = os.environ.copy()
297 if 'MACOSX_DEPLOYMENT_TARGET' in env:
298 del env['MACOSX_DEPLOYMENT_TARGET']
299
300 with open('/dev/null', 'w') as devnull_fp:
301 p = subprocess.Popen([
302 sys.executable, '-c',
303 'import sysconfig; print(sysconfig.get_platform())',
304 ],
305 stdout=subprocess.PIPE,
306 stderr=devnull_fp,
307 env=env)
308 test_platform = p.communicate()[0].strip()
309 test_platform = test_platform.decode('utf-8')
310 status = p.wait()
311
312 self.assertEqual(status, 0)
313 self.assertEqual(my_platform, test_platform)
314
315
316 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
317 # using a value that is unlikely to be the default one.
318 env = os.environ.copy()
319 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
320
321 p = subprocess.Popen([
322 sys.executable, '-c',
323 'import sysconfig; print(sysconfig.get_platform())',
324 ],
325 stdout=subprocess.PIPE,
326 stderr=open('/dev/null'),
327 env=env)
328 test_platform = p.communicate()[0].strip()
329 test_platform = test_platform.decode('utf-8')
330 status = p.wait()
331
332 self.assertEqual(status, 0)
333 self.assertEqual(my_platform, test_platform)
334
335
Victor Stinner1273b7c2011-05-24 23:37:07 +0200336class MakefileTests(unittest.TestCase):
337 @unittest.skipIf(sys.platform.startswith('win'),
338 'Test is not Windows compatible')
339 def test_get_makefile_filename(self):
340 makefile = sysconfig.get_makefile_filename()
341 self.assertTrue(os.path.isfile(makefile), makefile)
342
343 def test_parse_makefile(self):
344 self.addCleanup(unlink, TESTFN)
345 with open(TESTFN, "w") as makefile:
346 print("var1=a$(VAR2)", file=makefile)
347 print("VAR2=b$(var3)", file=makefile)
348 print("var3=42", file=makefile)
349 print("var4=$/invalid", file=makefile)
350 print("var5=dollar$$5", file=makefile)
351 vars = sysconfig._parse_makefile(TESTFN)
352 self.assertEqual(vars, {
353 'var1': 'ab42',
354 'VAR2': 'b42',
355 'var3': 42,
356 'var4': '$/invalid',
357 'var5': 'dollar$5',
358 })
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200359
360
Tarek Ziadéedacea32010-01-29 11:41:03 +0000361def test_main():
Victor Stinner1273b7c2011-05-24 23:37:07 +0200362 run_unittest(TestSysConfig, MakefileTests)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000363
364if __name__ == "__main__":
365 test_main()