blob: 716fa5e40e889272f582305a3466b2ddfce783db [file] [log] [blame]
Éric Araujo1a1a8a02011-10-08 02:49:12 +02001"""Tests for sysconfig."""
Tarek Ziadé5633a802010-01-23 09:23:15 +00002
Tarek Ziadé5633a802010-01-23 09:23:15 +00003import unittest
4import sys
Tarek Ziadé5633a802010-01-23 09:23:15 +00005import os
Georg Brandld1fa76e2010-02-06 23:12:12 +00006import shutil
Florent Xicluna85677612010-03-10 23:58:42 +00007import subprocess
Tarek Ziadé5633a802010-01-23 09:23:15 +00008from copy import copy, deepcopy
9
Florent Xicluna85677612010-03-10 23:58:42 +000010from test.test_support import run_unittest, TESTFN, unlink, get_attribute
Tarek Ziadé5633a802010-01-23 09:23:15 +000011
12import sysconfig
13from sysconfig import (get_paths, get_platform, get_config_vars,
14 get_path, get_path_names, _INSTALL_SCHEMES,
Tarek Ziadée81b0282010-02-02 22:54:28 +000015 _get_default_scheme, _expand_vars,
Tarek Ziadé8f692272010-05-19 22:20:14 +000016 get_scheme_names, get_config_var)
Tarek Ziadé5633a802010-01-23 09:23:15 +000017
18class TestSysConfig(unittest.TestCase):
19
20 def setUp(self):
21 """Make a copy of sys.path"""
22 super(TestSysConfig, self).setUp()
23 self.sys_path = sys.path[:]
24 self.makefile = None
25 # 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
47 if self.makefile is not None:
48 os.unlink(self.makefile)
49 self._cleanup_testfn()
50 if self.uname is not None:
51 os.uname = self.uname
52 else:
53 del os.uname
54 os.name = self.name
55 sys.platform = self.platform
56 sys.version = self.version
57 os.sep = self.sep
58 os.path.join = self.join
59 os.path.isabs = self.isabs
60 os.path.splitdrive = self.splitdrive
61 sysconfig._CONFIG_VARS = copy(self._config_vars)
62 for key, value in self.old_environ.items():
63 if os.environ.get(key) != value:
64 os.environ[key] = value
65
66 for key in os.environ.keys():
67 if key not in self.old_environ:
68 del os.environ[key]
69
70 super(TestSysConfig, self).tearDown()
71
72 def _set_uname(self, uname):
73 self._uname = uname
74
75 def _get_uname(self):
76 return self._uname
77
78 def _cleanup_testfn(self):
Georg Brandla4f46e12010-02-07 17:03:15 +000079 path = TESTFN
Tarek Ziadé5633a802010-01-23 09:23:15 +000080 if os.path.isfile(path):
81 os.remove(path)
82 elif os.path.isdir(path):
83 shutil.rmtree(path)
84
85 def test_get_path_names(self):
Ezio Melotti2623a372010-11-21 13:34:58 +000086 self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS)
Tarek Ziadé5633a802010-01-23 09:23:15 +000087
88 def test_get_paths(self):
89 scheme = get_paths()
90 default_scheme = _get_default_scheme()
91 wanted = _expand_vars(default_scheme, None)
92 wanted = wanted.items()
93 wanted.sort()
94 scheme = scheme.items()
95 scheme.sort()
Ezio Melotti2623a372010-11-21 13:34:58 +000096 self.assertEqual(scheme, wanted)
Tarek Ziadé5633a802010-01-23 09:23:15 +000097
98 def test_get_path(self):
99 # xxx make real tests here
100 for scheme in _INSTALL_SCHEMES:
101 for name in _INSTALL_SCHEMES[scheme]:
102 res = get_path(name, scheme)
103
104 def test_get_config_vars(self):
105 cvars = get_config_vars()
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000106 self.assertIsInstance(cvars, dict)
Tarek Ziadé5633a802010-01-23 09:23:15 +0000107 self.assertTrue(cvars)
108
109 def test_get_platform(self):
110 # windows XP, 32bits
111 os.name = 'nt'
112 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
113 '[MSC v.1310 32 bit (Intel)]')
114 sys.platform = 'win32'
Ezio Melotti2623a372010-11-21 13:34:58 +0000115 self.assertEqual(get_platform(), 'win32')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000116
117 # windows XP, amd64
118 os.name = 'nt'
119 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
120 '[MSC v.1310 32 bit (Amd64)]')
121 sys.platform = 'win32'
Ezio Melotti2623a372010-11-21 13:34:58 +0000122 self.assertEqual(get_platform(), 'win-amd64')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000123
124 # windows XP, itanium
125 os.name = 'nt'
126 sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) '
127 '[MSC v.1310 32 bit (Itanium)]')
128 sys.platform = 'win32'
Ezio Melotti2623a372010-11-21 13:34:58 +0000129 self.assertEqual(get_platform(), 'win-ia64')
Tarek Ziadé5633a802010-01-23 09:23:15 +0000130
131 # macbook
132 os.name = 'posix'
133 sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) '
134 '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]')
135 sys.platform = 'darwin'
136 self._set_uname(('Darwin', 'macziade', '8.11.1',
137 ('Darwin Kernel Version 8.11.1: '
138 'Wed Oct 10 18:23:28 PDT 2007; '
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000139 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC'))
Ronald Oussorena70286b2011-05-15 16:44:27 +0200140 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000141
142 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
143 '-fwrapv -O3 -Wall -Wstrict-prototypes')
144
145 maxint = sys.maxint
146 try:
147 sys.maxint = 2147483647
Ezio Melotti2623a372010-11-21 13:34:58 +0000148 self.assertEqual(get_platform(), 'macosx-10.3-ppc')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000149 sys.maxint = 9223372036854775807
Ezio Melotti2623a372010-11-21 13:34:58 +0000150 self.assertEqual(get_platform(), 'macosx-10.3-ppc64')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000151 finally:
152 sys.maxint = maxint
153
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é5633a802010-01-23 09:23:15 +0000158 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386'))
159 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3'
Tarek Ziadé5633a802010-01-23 09:23:15 +0000160
161 get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g '
162 '-fwrapv -O3 -Wall -Wstrict-prototypes')
163
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000164 maxint = sys.maxint
165 try:
166 sys.maxint = 2147483647
Ezio Melotti2623a372010-11-21 13:34:58 +0000167 self.assertEqual(get_platform(), 'macosx-10.3-i386')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000168 sys.maxint = 9223372036854775807
Ezio Melotti2623a372010-11-21 13:34:58 +0000169 self.assertEqual(get_platform(), 'macosx-10.3-x86_64')
Tarek Ziadéc64614e2010-01-23 17:52:57 +0000170 finally:
171 sys.maxint = maxint
Tarek Ziadé5633a802010-01-23 09:23:15 +0000172
173 # macbook with fat binaries (fat, universal or fat64)
Ronald Oussorena70286b2011-05-15 16:44:27 +0200174 get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.4'
Tarek Ziadé5633a802010-01-23 09:23:15 +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 Melotti2623a372010-11-21 13:34:58 +0000180 self.assertEqual(get_platform(), 'macosx-10.4-fat')
Tarek Ziadé5633a802010-01-23 09:23:15 +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 Melotti2623a372010-11-21 13:34:58 +0000187 self.assertEqual(get_platform(), 'macosx-10.4-intel')
Tarek Ziadé5633a802010-01-23 09:23:15 +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 Melotti2623a372010-11-21 13:34:58 +0000193 self.assertEqual(get_platform(), 'macosx-10.4-fat3')
Tarek Ziadé5633a802010-01-23 09:23:15 +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 Melotti2623a372010-11-21 13:34:58 +0000199 self.assertEqual(get_platform(), 'macosx-10.4-universal')
Tarek Ziadé5633a802010-01-23 09:23:15 +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 Melotti2623a372010-11-21 13:34:58 +0000206 self.assertEqual(get_platform(), 'macosx-10.4-fat64')
Tarek Ziadé5633a802010-01-23 09:23:15 +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 Melotti2623a372010-11-21 13:34:58 +0000214 self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,))
Tarek Ziadé5633a802010-01-23 09:23:15 +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 Melotti2623a372010-11-21 13:34:58 +0000224 self.assertEqual(get_platform(), 'linux-i686')
Tarek Ziadé5633a802010-01-23 09:23:15 +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ée81b0282010-02-02 22:54:28 +0000232 def test_get_scheme_names(self):
Benjamin Peterson7baf8622010-05-08 15:42:29 +0000233 wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user',
Benjamin Petersonac896ed2010-05-08 15:41:44 +0000234 'posix_home', 'posix_prefix', 'posix_user')
Ezio Melotti2623a372010-11-21 13:34:58 +0000235 self.assertEqual(get_scheme_names(), wanted)
Tarek Ziadée81b0282010-02-02 22:54:28 +0000236
Florent Xicluna85677612010-03-10 23:58:42 +0000237 def test_symlink(self):
238 # Issue 7880
239 symlink = get_attribute(os, "symlink")
240 def get(python):
241 cmd = [python, '-c',
242 'import sysconfig; print sysconfig.get_platform()']
Florent Xicluna6602ec62010-03-11 01:39:55 +0000243 p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
Florent Xicluna85677612010-03-10 23:58:42 +0000244 return p.communicate()
245 real = os.path.realpath(sys.executable)
246 link = os.path.abspath(TESTFN)
247 symlink(real, link)
248 try:
249 self.assertEqual(get(real), get(link))
250 finally:
251 unlink(link)
252
Tarek Ziadé8f692272010-05-19 22:20:14 +0000253 def test_user_similar(self):
Éric Araujoa971df32011-08-31 16:48:17 +0200254 # Issue #8759: make sure the posix scheme for the users
Tarek Ziadé8f692272010-05-19 22:20:14 +0000255 # is similar to the global posix_prefix one
256 base = get_config_var('base')
257 user = get_config_var('userbase')
Éric Araujo1a1a8a02011-10-08 02:49:12 +0200258 # the global scheme mirrors the distinction between prefix and
259 # exec-prefix but not the user scheme, so we have to adapt the paths
260 # before comparing (issue #9100)
261 adapt = sys.prefix != sys.exec_prefix
Tarek Ziadé8f692272010-05-19 22:20:14 +0000262 for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'):
263 global_path = get_path(name, 'posix_prefix')
Éric Araujo1a1a8a02011-10-08 02:49:12 +0200264 if adapt:
265 global_path = global_path.replace(sys.exec_prefix, sys.prefix)
266 base = base.replace(sys.exec_prefix, sys.prefix)
Tarek Ziadé8f692272010-05-19 22:20:14 +0000267 user_path = get_path(name, 'posix_user')
Éric Araujoa971df32011-08-31 16:48:17 +0200268 self.assertEqual(user_path, global_path.replace(base, user, 1))
Tarek Ziadé5633a802010-01-23 09:23:15 +0000269
Ronald Oussorena70286b2011-05-15 16:44:27 +0200270 @unittest.skipUnless(sys.platform == "darwin", "test only relevant on MacOSX")
271 def test_platform_in_subprocess(self):
272 my_platform = sysconfig.get_platform()
273
274 # Test without MACOSX_DEPLOYMENT_TARGET in the environment
275
276 env = os.environ.copy()
277 if 'MACOSX_DEPLOYMENT_TARGET' in env:
278 del env['MACOSX_DEPLOYMENT_TARGET']
279
280 with open('/dev/null', 'w') as devnull_fp:
281 p = subprocess.Popen([
282 sys.executable, '-c',
283 'import sysconfig; print(sysconfig.get_platform())',
284 ],
285 stdout=subprocess.PIPE,
286 stderr=devnull_fp,
287 env=env)
288 test_platform = p.communicate()[0].strip()
289 test_platform = test_platform.decode('utf-8')
290 status = p.wait()
291
292 self.assertEqual(status, 0)
293 self.assertEqual(my_platform, test_platform)
294
295
296 # Test with MACOSX_DEPLOYMENT_TARGET in the environment, and
297 # using a value that is unlikely to be the default one.
298 env = os.environ.copy()
299 env['MACOSX_DEPLOYMENT_TARGET'] = '10.1'
300
301 p = subprocess.Popen([
302 sys.executable, '-c',
303 'import sysconfig; print(sysconfig.get_platform())',
304 ],
305 stdout=subprocess.PIPE,
306 stderr=open('/dev/null'),
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
Tarek Ziadé5633a802010-01-23 09:23:15 +0000315def test_main():
316 run_unittest(TestSysConfig)
317
318if __name__ == "__main__":
319 test_main()