blob: 1b6897d34d934d3ce125ce99cf35292e0ec525ec [file] [log] [blame]
Éric Araujode504552011-10-08 01:55:07 +02001"""Tests for sysconfig."""
Tarek Ziadéedacea32010-01-29 11:41:03 +00002
Tarek Ziadéedacea32010-01-29 11:41:03 +00003import unittest
4import sys
Tarek Ziadéedacea32010-01-29 11:41:03 +00005import os
Florent Xiclunaa4707382010-03-11 00:05:17 +00006import subprocess
Georg Brandl89fad142010-03-14 10:23:39 +00007import shutil
Tarek Ziadéedacea32010-01-29 11:41:03 +00008from copy import copy, deepcopy
9
Éric Araujode504552011-10-08 01:55:07 +020010from test.support import (run_unittest, TESTFN, unlink,
Brian Curtin3b4499c2010-12-28 14:31:47 +000011 captured_stdout, skip_unless_symlink)
Tarek Ziadéedacea32010-01-29 11:41:03 +000012
13import sysconfig
14from sysconfig import (get_paths, get_platform, get_config_vars,
15 get_path, get_path_names, _INSTALL_SCHEMES,
Tarek Ziadébd797682010-02-02 23:16:13 +000016 _get_default_scheme, _expand_vars,
Tarek Ziadéa7514992010-05-25 09:44:36 +000017 get_scheme_names, get_config_var, _main)
Ned Deilyfc20d772013-01-31 01:28:23 -080018import _osx_support
Tarek Ziadéedacea32010-01-29 11:41:03 +000019
20class TestSysConfig(unittest.TestCase):
21
22 def setUp(self):
23 """Make a copy of sys.path"""
24 super(TestSysConfig, self).setUp()
25 self.sys_path = sys.path[:]
Tarek Ziadéedacea32010-01-29 11:41:03 +000026 # 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
Tarek Ziadéedacea32010-01-29 11:41:03 +000048 self._cleanup_testfn()
49 if self.uname is not None:
50 os.uname = self.uname
51 else:
52 del os.uname
53 os.name = self.name
54 sys.platform = self.platform
55 sys.version = self.version
56 os.sep = self.sep
57 os.path.join = self.join
58 os.path.isabs = self.isabs
59 os.path.splitdrive = self.splitdrive
60 sysconfig._CONFIG_VARS = copy(self._config_vars)
61 for key, value in self.old_environ.items():
62 if os.environ.get(key) != value:
63 os.environ[key] = value
64
65 for key in list(os.environ.keys()):
66 if key not in self.old_environ:
67 del os.environ[key]
68
69 super(TestSysConfig, self).tearDown()
70
71 def _set_uname(self, uname):
72 self._uname = uname
73
74 def _get_uname(self):
75 return self._uname
76
77 def _cleanup_testfn(self):
78 path = TESTFN
79 if os.path.isfile(path):
80 os.remove(path)
81 elif os.path.isdir(path):
82 shutil.rmtree(path)
83
84 def test_get_path_names(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000085 self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
Tarek Ziadéedacea32010-01-29 11:41:03 +000086
87 def test_get_paths(self):
88 scheme = get_paths()
89 default_scheme = _get_default_scheme()
90 wanted = _expand_vars(default_scheme, None)
91 wanted = list(wanted.items())
92 wanted.sort()
93 scheme = list(scheme.items())
94 scheme.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +000095 self.assertEqual(scheme, wanted)
Tarek Ziadéedacea32010-01-29 11:41:03 +000096
97 def test_get_path(self):
98 # xxx make real tests here
99 for scheme in _INSTALL_SCHEMES:
100 for name in _INSTALL_SCHEMES[scheme]:
101 res = get_path(name, scheme)
102
103 def test_get_config_vars(self):
104 cvars = get_config_vars()
105 self.assertTrue(isinstance(cvars, dict))
106 self.assertTrue(cvars)
107
108 def test_get_platform(self):
109 # windows XP, 32bits
110 os.name = 'nt'
111 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
112 '[MSC v.1310 32 bit (Intel)]')
113 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000114 self.assertEqual(get_platform(), 'win32')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000115
116 # windows XP, amd64
117 os.name = 'nt'
118 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
119 '[MSC v.1310 32 bit (Amd64)]')
120 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000121 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000122
123 # windows XP, itanium
124 os.name = 'nt'
125 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
126 '[MSC v.1310 32 bit (Itanium)]')
127 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000128 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000129
130 # macbook
131 os.name = 'posix'
132 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
133 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
134 sys.platform = 'darwin'
135 self._set_uname(('Darwin', 'macziade', '8.11.1',
136 ('Darwin Kernel Version 8.11.1: '
137 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000138 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ned Deilyfc20d772013-01-31 01:28:23 -0800139 _osx_support._remove_original_values(get_config_vars())
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200140 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000141
142 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
143 '-fwrapv -O3 -Wall -Wstrict-prototypes')
144
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000145 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000146 try:
147 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000148 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000149 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000150 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000151 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000152 sys.maxsize = maxint
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000153
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000154 self._set_uname(('Darwin', 'macziade', '8.11.1',
155 ('Darwin Kernel Version 8.11.1: '
156 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéedacea32010-01-29 11:41:03 +0000157 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
Ned Deilyfc20d772013-01-31 01:28:23 -0800158 _osx_support._remove_original_values(get_config_vars())
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200159 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000160
161 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
162 '-fwrapv -O3 -Wall -Wstrict-prototypes')
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000163 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000164 try:
165 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000166 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000167 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000168 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000169 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000170 sys.maxsize = maxint
Tarek Ziadéedacea32010-01-29 11:41:03 +0000171
172 # macbook with fat binaries (fat, universal or fat64)
Ned Deilyfc20d772013-01-31 01:28:23 -0800173 _osx_support._remove_original_values(get_config_vars())
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200174 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000175 get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot '
176 '/Developer/SDKs/MacOSX10.4u.sdk '
177 '-fno-strict-aliasing -fno-common '
178 '-dynamic -DNDEBUG -g -O3')
179
Ezio Melottib3aedd42010-11-20 19:04:17 +0000180 self.assertEqual(get_platform(), 'macosx-10.4-fat')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000181
Ned Deilyfc20d772013-01-31 01:28:23 -0800182 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadéedacea32010-01-29 11:41:03 +0000183 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot '
184 '/Developer/SDKs/MacOSX10.4u.sdk '
185 '-fno-strict-aliasing -fno-common '
186 '-dynamic -DNDEBUG -g -O3')
187
Ezio Melottib3aedd42010-11-20 19:04:17 +0000188 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000189
Ned Deilyfc20d772013-01-31 01:28:23 -0800190 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadéedacea32010-01-29 11:41:03 +0000191 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
192 '/Developer/SDKs/MacOSX10.4u.sdk '
193 '-fno-strict-aliasing -fno-common '
194 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000195 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000196
Ned Deilyfc20d772013-01-31 01:28:23 -0800197 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadéedacea32010-01-29 11:41:03 +0000198 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
199 '/Developer/SDKs/MacOSX10.4u.sdk '
200 '-fno-strict-aliasing -fno-common '
201 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000202 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000203
Ned Deilyfc20d772013-01-31 01:28:23 -0800204 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadéedacea32010-01-29 11:41:03 +0000205 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 Melottib3aedd42010-11-20 19:04:17 +0000210 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000211
212 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
Ned Deilyfc20d772013-01-31 01:28:23 -0800213 _osx_support._remove_original_values(get_config_vars())
Tarek Ziadéedacea32010-01-29 11:41:03 +0000214 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
215 '/Developer/SDKs/MacOSX10.4u.sdk '
216 '-fno-strict-aliasing -fno-common '
Ned Deilyfc20d772013-01-31 01:28:23 -0800217 '-dynamic -DNDEBUG -g -O3' % arch)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000218
Ned Deilyfc20d772013-01-31 01:28:23 -0800219 self.assertEqual(get_platform(), 'macosx-10.4-%s' % arch)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000220
221 # linux debian sarge
222 os.name = 'posix'
223 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
224 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
225 sys.platform = 'linux2'
226 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
227 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
228
Ezio Melottib3aedd42010-11-20 19:04:17 +0000229 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000230
231 # XXX more platforms to tests here
232
233 def test_get_config_h_filename(self):
234 config_h = sysconfig.get_config_h_filename()
235 self.assertTrue(os.path.isfile(config_h), config_h)
236
Tarek Ziadébd797682010-02-02 23:16:13 +0000237 def test_get_scheme_names(self):
Benjamin Peterson3c451e62010-05-08 15:51:23 +0000238 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
239 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000240 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadébd797682010-02-02 23:16:13 +0000241
Brian Curtin3b4499c2010-12-28 14:31:47 +0000242 @skip_unless_symlink
Florent Xiclunaa4707382010-03-11 00:05:17 +0000243 def test_symlink(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000244 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
245 # to add the directory to the path.
246 if sys.platform == "win32":
Brian Curtin74e45612010-07-09 15:58:59 +0000247 os.environ["Path"] = "{};{}".format(
248 os.path.dirname(sys.executable), os.environ["Path"])
Brian Curtind40e6f72010-07-08 21:39:08 +0000249
Florent Xiclunaa4707382010-03-11 00:05:17 +0000250 # Issue 7880
Florent Xiclunaa4707382010-03-11 00:05:17 +0000251 def get(python):
252 cmd = [python, '-c',
Florent Xicluna01fe6102010-03-13 15:35:12 +0000253 'import sysconfig; print(sysconfig.get_platform())']
Brian Curtind40e6f72010-07-08 21:39:08 +0000254 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000255 return p.communicate()
256 real = os.path.realpath(sys.executable)
257 link = os.path.abspath(TESTFN)
Brian Curtind40e6f72010-07-08 21:39:08 +0000258 os.symlink(real, link)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000259 try:
260 self.assertEqual(get(real), get(link))
261 finally:
262 unlink(link)
263
Tarek Ziadé06710a82010-05-19 22:25:00 +0000264 def test_user_similar(self):
Éric Araujo48e484f2011-08-31 16:48:17 +0200265 # Issue #8759: make sure the posix scheme for the users
Tarek Ziadé06710a82010-05-19 22:25:00 +0000266 # is similar to the global posix_prefix one
267 base = get_config_var('base')
268 user = get_config_var('userbase')
Éric Araujode504552011-10-08 01:55:07 +0200269 # the global scheme mirrors the distinction between prefix and
270 # exec-prefix but not the user scheme, so we have to adapt the paths
271 # before comparing (issue #9100)
272 adapt = sys.prefix != sys.exec_prefix
Tarek Ziadé06710a82010-05-19 22:25:00 +0000273 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
274 global_path = get_path(name, 'posix_prefix')
Éric Araujode504552011-10-08 01:55:07 +0200275 if adapt:
276 global_path = global_path.replace(sys.exec_prefix, sys.prefix)
277 base = base.replace(sys.exec_prefix, sys.prefix)
Tarek Ziadé06710a82010-05-19 22:25:00 +0000278 user_path = get_path(name, 'posix_user')
Éric Araujo48e484f2011-08-31 16:48:17 +0200279 self.assertEqual(user_path, global_path.replace(base, user, 1))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000280
Tarek Ziadéa7514992010-05-25 09:44:36 +0000281 def test_main(self):
282 # just making sure _main() runs and returns things in the stdout
283 with captured_stdout() as output:
284 _main()
285 self.assertTrue(len(output.getvalue().split('\n')) > 0)
286
Brian Curtindb902ac2010-07-22 15:38:28 +0000287 @unittest.skipIf(sys.platform == "win32", "Does not apply to Windows")
Ronald Oussorenf4ebe2e2010-07-19 13:00:36 +0000288 def test_ldshared_value(self):
289 ldflags = sysconfig.get_config_var('LDFLAGS')
290 ldshared = sysconfig.get_config_var('LDSHARED')
291
292 self.assertIn(ldflags, ldshared)
293
Tarek Ziadéa7514992010-05-25 09:44:36 +0000294
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200295 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
296 def test_platform_in_subprocess(self):
297 my_platform = sysconfig.get_platform()
298
299 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
300
301 env = os.environ.copy()
302 if 'MACOSX_DEPLOYMENT_TARGET' in env:
303 del env['MACOSX_DEPLOYMENT_TARGET']
304
305 with open('/dev/null', 'w') as devnull_fp:
306 p = subprocess.Popen([
307 sys.executable, '-c',
308 'import sysconfig; print(sysconfig.get_platform())',
309 ],
310 stdout=subprocess.PIPE,
311 stderr=devnull_fp,
312 env=env)
313 test_platform = p.communicate()[0].strip()
314 test_platform = test_platform.decode('utf-8')
315 status = p.wait()
316
317 self.assertEqual(status, 0)
318 self.assertEqual(my_platform, test_platform)
319
320
321 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
322 # using a value that is unlikely to be the default one.
323 env = os.environ.copy()
324 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
325
326 p = subprocess.Popen([
327 sys.executable, '-c',
328 'import sysconfig; print(sysconfig.get_platform())',
329 ],
330 stdout=subprocess.PIPE,
331 stderr=open('/dev/null'),
332 env=env)
333 test_platform = p.communicate()[0].strip()
334 test_platform = test_platform.decode('utf-8')
335 status = p.wait()
336
337 self.assertEqual(status, 0)
338 self.assertEqual(my_platform, test_platform)
339
340
Victor Stinner1273b7c2011-05-24 23:37:07 +0200341class MakefileTests(unittest.TestCase):
342 @unittest.skipIf(sys.platform.startswith('win'),
343 'Test is not Windows compatible')
344 def test_get_makefile_filename(self):
345 makefile = sysconfig.get_makefile_filename()
346 self.assertTrue(os.path.isfile(makefile), makefile)
347
348 def test_parse_makefile(self):
349 self.addCleanup(unlink, TESTFN)
350 with open(TESTFN, "w") as makefile:
351 print("var1=a$(VAR2)", file=makefile)
352 print("VAR2=b$(var3)", file=makefile)
353 print("var3=42", file=makefile)
354 print("var4=$/invalid", file=makefile)
355 print("var5=dollar$$5", file=makefile)
356 vars = sysconfig._parse_makefile(TESTFN)
357 self.assertEqual(vars, {
358 'var1': 'ab42',
359 'VAR2': 'b42',
360 'var3': 42,
361 'var4': '$/invalid',
362 'var5': 'dollar$5',
363 })
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200364
365
Tarek Ziadéedacea32010-01-29 11:41:03 +0000366def test_main():
Victor Stinner1273b7c2011-05-24 23:37:07 +0200367 run_unittest(TestSysConfig, MakefileTests)
Tarek Ziadéedacea32010-01-29 11:41:03 +0000368
369if __name__ == "__main__":
370 test_main()