blob: aabb6fa72a89c1ab6be47ac09a82484cc17224f8 [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)
Tarek Ziadéedacea32010-01-29 11:41:03 +000018
19class TestSysConfig(unittest.TestCase):
20
21 def setUp(self):
22 """Make a copy of sys.path"""
23 super(TestSysConfig, self).setUp()
24 self.sys_path = sys.path[:]
Tarek Ziadéedacea32010-01-29 11:41:03 +000025 # patching os.uname
26 if hasattr(os, 'uname'):
27 self.uname = os.uname
28 self._uname = os.uname()
29 else:
30 self.uname = None
31 self._uname = None
32 os.uname = self._get_uname
33 # saving the environment
34 self.name = os.name
35 self.platform = sys.platform
36 self.version = sys.version
37 self.sep = os.sep
38 self.join = os.path.join
39 self.isabs = os.path.isabs
40 self.splitdrive = os.path.splitdrive
41 self._config_vars = copy(sysconfig._CONFIG_VARS)
42 self.old_environ = deepcopy(os.environ)
43
44 def tearDown(self):
45 """Restore sys.path"""
46 sys.path[:] = self.sys_path
Tarek Ziadéedacea32010-01-29 11:41:03 +000047 self._cleanup_testfn()
48 if self.uname is not None:
49 os.uname = self.uname
50 else:
51 del os.uname
52 os.name = self.name
53 sys.platform = self.platform
54 sys.version = self.version
55 os.sep = self.sep
56 os.path.join = self.join
57 os.path.isabs = self.isabs
58 os.path.splitdrive = self.splitdrive
59 sysconfig._CONFIG_VARS = copy(self._config_vars)
60 for key, value in self.old_environ.items():
61 if os.environ.get(key) != value:
62 os.environ[key] = value
63
64 for key in list(os.environ.keys()):
65 if key not in self.old_environ:
66 del os.environ[key]
67
68 super(TestSysConfig, self).tearDown()
69
70 def _set_uname(self, uname):
71 self._uname = uname
72
73 def _get_uname(self):
74 return self._uname
75
76 def _cleanup_testfn(self):
77 path = TESTFN
78 if os.path.isfile(path):
79 os.remove(path)
80 elif os.path.isdir(path):
81 shutil.rmtree(path)
82
83 def test_get_path_names(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +000084 self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
Tarek Ziadéedacea32010-01-29 11:41:03 +000085
86 def test_get_paths(self):
87 scheme = get_paths()
88 default_scheme = _get_default_scheme()
89 wanted = _expand_vars(default_scheme, None)
90 wanted = list(wanted.items())
91 wanted.sort()
92 scheme = list(scheme.items())
93 scheme.sort()
Ezio Melottib3aedd42010-11-20 19:04:17 +000094 self.assertEqual(scheme, wanted)
Tarek Ziadéedacea32010-01-29 11:41:03 +000095
96 def test_get_path(self):
97 # xxx make real tests here
98 for scheme in _INSTALL_SCHEMES:
99 for name in _INSTALL_SCHEMES[scheme]:
100 res = get_path(name, scheme)
101
102 def test_get_config_vars(self):
103 cvars = get_config_vars()
104 self.assertTrue(isinstance(cvars, dict))
105 self.assertTrue(cvars)
106
107 def test_get_platform(self):
108 # windows XP, 32bits
109 os.name = 'nt'
110 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
111 '[MSC v.1310 32 bit (Intel)]')
112 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000113 self.assertEqual(get_platform(), 'win32')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000114
115 # windows XP, amd64
116 os.name = 'nt'
117 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
118 '[MSC v.1310 32 bit (Amd64)]')
119 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000120 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000121
122 # windows XP, itanium
123 os.name = 'nt'
124 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
125 '[MSC v.1310 32 bit (Itanium)]')
126 sys.platform = 'win32'
Ezio Melottib3aedd42010-11-20 19:04:17 +0000127 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000128
129 # macbook
130 os.name = 'posix'
131 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
132 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
133 sys.platform = 'darwin'
134 self._set_uname(('Darwin', 'macziade', '8.11.1',
135 ('Darwin Kernel Version 8.11.1: '
136 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000137 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200138
139
140 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
154
155 self._set_uname(('Darwin', 'macziade', '8.11.1',
156 ('Darwin Kernel Version 8.11.1: '
157 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéedacea32010-01-29 11:41:03 +0000158 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
159 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Ronald Oussoren222e89a2011-05-15 16:46:11 +0200160 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéedacea32010-01-29 11:41:03 +0000161
162 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
163 '-fwrapv -O3 -Wall -Wstrict-prototypes')
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000164 maxint = sys.maxsize
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000165 try:
166 sys.maxsize = 2147483647
Ezio Melottib3aedd42010-11-20 19:04:17 +0000167 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000168 sys.maxsize = 9223372036854775807
Ezio Melottib3aedd42010-11-20 19:04:17 +0000169 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadé8b441d02010-01-29 11:46:31 +0000170 finally:
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000171 sys.maxsize = maxint
Tarek Ziadéedacea32010-01-29 11:41:03 +0000172
173 # macbook with fat binaries (fat, universal or fat64)
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
182 get_config_vars()['CFLAGS'] = ('-arch x86_64 -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-intel')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000188
189 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot '
190 '/Developer/SDKs/MacOSX10.4u.sdk '
191 '-fno-strict-aliasing -fno-common '
192 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000193 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000194
195 get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot '
196 '/Developer/SDKs/MacOSX10.4u.sdk '
197 '-fno-strict-aliasing -fno-common '
198 '-dynamic -DNDEBUG -g -O3')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000199 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000200
201 get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot '
202 '/Developer/SDKs/MacOSX10.4u.sdk '
203 '-fno-strict-aliasing -fno-common '
204 '-dynamic -DNDEBUG -g -O3')
205
Ezio Melottib3aedd42010-11-20 19:04:17 +0000206 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000207
208 for arch in ('ppc', 'i386', 'x86_64', 'ppc64'):
209 get_config_vars()['CFLAGS'] = ('-arch %s -isysroot '
210 '/Developer/SDKs/MacOSX10.4u.sdk '
211 '-fno-strict-aliasing -fno-common '
212 '-dynamic -DNDEBUG -g -O3'%(arch,))
213
Ezio Melottib3aedd42010-11-20 19:04:17 +0000214 self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
Tarek Ziadéedacea32010-01-29 11:41:03 +0000215
216 # linux debian sarge
217 os.name = 'posix'
218 sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) '
219 '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]')
220 sys.platform = 'linux2'
221 self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7',
222 '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686'))
223
Ezio Melottib3aedd42010-11-20 19:04:17 +0000224 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadéedacea32010-01-29 11:41:03 +0000225
226 # XXX more platforms to tests here
227
228 def test_get_config_h_filename(self):
229 config_h = sysconfig.get_config_h_filename()
230 self.assertTrue(os.path.isfile(config_h), config_h)
231
Tarek Ziadébd797682010-02-02 23:16:13 +0000232 def test_get_scheme_names(self):
Benjamin Peterson3c451e62010-05-08 15:51:23 +0000233 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
234 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000235 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadébd797682010-02-02 23:16:13 +0000236
Brian Curtin3b4499c2010-12-28 14:31:47 +0000237 @skip_unless_symlink
Florent Xiclunaa4707382010-03-11 00:05:17 +0000238 def test_symlink(self):
Brian Curtind40e6f72010-07-08 21:39:08 +0000239 # On Windows, the EXE needs to know where pythonXY.dll is at so we have
240 # to add the directory to the path.
241 if sys.platform == "win32":
Brian Curtin74e45612010-07-09 15:58:59 +0000242 os.environ["Path"] = "{};{}".format(
243 os.path.dirname(sys.executable), os.environ["Path"])
Brian Curtind40e6f72010-07-08 21:39:08 +0000244
Florent Xiclunaa4707382010-03-11 00:05:17 +0000245 # Issue 7880
Florent Xiclunaa4707382010-03-11 00:05:17 +0000246 def get(python):
247 cmd = [python, '-c',
Florent Xicluna01fe6102010-03-13 15:35:12 +0000248 'import sysconfig; print(sysconfig.get_platform())']
Brian Curtind40e6f72010-07-08 21:39:08 +0000249 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=os.environ)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000250 return p.communicate()
251 real = os.path.realpath(sys.executable)
252 link = os.path.abspath(TESTFN)
Brian Curtind40e6f72010-07-08 21:39:08 +0000253 os.symlink(real, link)
Florent Xiclunaa4707382010-03-11 00:05:17 +0000254 try:
255 self.assertEqual(get(real), get(link))
256 finally:
257 unlink(link)
258
Tarek Ziadé06710a82010-05-19 22:25:00 +0000259 def test_user_similar(self):
Éric Araujo48e484f2011-08-31 16:48:17 +0200260 # Issue #8759: make sure the posix scheme for the users
Tarek Ziadé06710a82010-05-19 22:25:00 +0000261 # is similar to the global posix_prefix one
262 base = get_config_var('base')
263 user = get_config_var('userbase')
Éric Araujode504552011-10-08 01:55:07 +0200264 # the global scheme mirrors the distinction between prefix and
265 # exec-prefix but not the user scheme, so we have to adapt the paths
266 # before comparing (issue #9100)
267 adapt = sys.prefix != sys.exec_prefix
Tarek Ziadé06710a82010-05-19 22:25:00 +0000268 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
269 global_path = get_path(name, 'posix_prefix')
Éric Araujode504552011-10-08 01:55:07 +0200270 if adapt:
271 global_path = global_path.replace(sys.exec_prefix, sys.prefix)
272 base = base.replace(sys.exec_prefix, sys.prefix)
Tarek Ziadé06710a82010-05-19 22:25:00 +0000273 user_path = get_path(name, 'posix_user')
Éric Araujo48e484f2011-08-31 16:48:17 +0200274 self.assertEqual(user_path, global_path.replace(base, user, 1))
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()